a0f88d3b5b179bacf8104107511164aadd5b10cc
[platform/core/base/bundle.git] / src / stub.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <tizen.h>
18 #include <glib.h>
19
20 #include "include/bundle.h"
21 #include "include/bundle_internal.h"
22
23 #include "bundle-internal.h"
24 #include "exception-internal.h"
25 #include "export-api-internal.h"
26 #include "json-internal.h"
27
28 using namespace tizen_base::internal;
29
30 extern "C" EXPORT_API bundle* bundle_create(void) {
31   auto* h = new (std::nothrow) Bundle();
32   if (h == nullptr) {
33     set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
34     return nullptr;
35   }
36
37   set_last_result(BUNDLE_ERROR_NONE);
38   return reinterpret_cast<bundle*>(h);
39 }
40
41 extern "C" EXPORT_API int bundle_free(bundle* b) {
42   if (b == nullptr)
43     return BUNDLE_ERROR_INVALID_PARAMETER;
44
45   auto* h = reinterpret_cast<Bundle*>(b);
46   delete h;
47   return BUNDLE_ERROR_NONE;
48 }
49
50 extern "C" EXPORT_API int bundle_add_str(bundle* b,
51     const char* key, const char* str) {
52   if (b == nullptr || key == nullptr || str == nullptr)
53     return BUNDLE_ERROR_INVALID_PARAMETER;
54
55   auto* h = reinterpret_cast<Bundle*>(b);
56   std::vector<unsigned char> value(str, str + (strlen(str) + 1));
57   std::shared_ptr<KeyInfo> key_info(
58       new (std::nothrow) KeyInfo(BUNDLE_TYPE_STR, key, value));
59   if (key_info.get() == nullptr)
60     return BUNDLE_ERROR_OUT_OF_MEMORY;
61
62   try {
63     h->Add(key_info);
64   } catch (Exception& e) {
65     return e.GetErrorCode();
66   }
67
68   return BUNDLE_ERROR_NONE;
69 }
70
71 extern "C" EXPORT_API int bundle_get_str(bundle* b,
72     const char* key, char** str) {
73   if (b == nullptr || key == nullptr || str == nullptr)
74     return BUNDLE_ERROR_INVALID_PARAMETER;
75
76   auto* h = reinterpret_cast<Bundle*>(b);
77   std::shared_ptr<KeyInfo> key_info;
78   try {
79     key_info = h->Get(key);
80   } catch (Exception& e) {
81     return e.GetErrorCode();
82   }
83
84   if (key_info->GetType() != BUNDLE_TYPE_STR)
85     return BUNDLE_ERROR_INVALID_PARAMETER;
86
87   auto& values = key_info->GetValues();
88   auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
89   *str = reinterpret_cast<char*>(&value[0]);
90
91   return BUNDLE_ERROR_NONE;
92 }
93
94 extern "C" EXPORT_API int bundle_add(bundle* b, const char* key,
95     const char* val) {
96   return bundle_add_str(b, key, val);
97 }
98
99 extern "C" EXPORT_API int bundle_del(bundle* b, const char* key) {
100   if (b == nullptr || key == nullptr)
101     return BUNDLE_ERROR_INVALID_PARAMETER;
102
103   auto* h = reinterpret_cast<Bundle*>(b);
104   try {
105     h->Remove(key);
106   } catch (Exception& e) {
107     return e.GetErrorCode();
108   }
109
110   return BUNDLE_ERROR_NONE;
111 }
112
113 extern "C" EXPORT_API const char* bundle_get_val(bundle* b, const char* key) {
114   char* val = nullptr;
115   int ret = bundle_get_str(b, key, &val);
116   set_last_result(ret);
117   return val;
118 }
119
120 extern "C" EXPORT_API int bundle_get_count(bundle* b) {
121   if (b == nullptr)
122     return 0;
123
124   auto* h = reinterpret_cast<Bundle*>(b);
125   set_last_result(BUNDLE_ERROR_NONE);
126   return h->GetSize();
127 }
128
129 extern "C" EXPORT_API void bundle_iterate(bundle* b,
130     bundle_iterate_cb_t callback, void* user_data) {
131   if (b == nullptr || callback == nullptr) {
132     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
133     return;
134   }
135
136   auto* h = reinterpret_cast<Bundle*>(b);
137   for (const auto& kv : h->GetMap()) {
138     auto& key_info = kv.second;
139     auto& values = key_info->GetValues();
140     auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
141     auto* val = reinterpret_cast<char*>(&value[0]);
142     callback(key_info->GetKey().c_str(), val, user_data);
143   }
144
145   set_last_result(BUNDLE_ERROR_NONE);
146 }
147
148 extern "C" EXPORT_API void bundle_foreach(bundle* b,
149     bundle_iterator_t callback, void* user_data) {
150   if (b == nullptr || callback == nullptr) {
151     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
152     return;
153   }
154
155   auto* h = reinterpret_cast<Bundle*>(b);
156   for (const auto& kv : h->GetMap()) {
157     auto& key_info = kv.second;
158     callback(key_info->GetKey().c_str(), key_info->GetType(),
159         reinterpret_cast<bundle_keyval_t*>(key_info.get()), user_data);
160   }
161
162   set_last_result(BUNDLE_ERROR_NONE);
163 }
164
165 extern "C" EXPORT_API int bundle_keyval_get_type(bundle_keyval_t* kv) {
166   if (kv == nullptr) {
167     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
168     return -1;
169   }
170
171   auto* key_info = reinterpret_cast<KeyInfo*>(kv);
172   set_last_result(BUNDLE_ERROR_NONE);
173   return key_info->GetType();
174 }
175
176 extern "C" EXPORT_API int bundle_keyval_type_is_array(bundle_keyval_t* kv) {
177   int type = bundle_keyval_get_type(kv);
178   if (type == -1)
179     return -1;
180
181   set_last_result(BUNDLE_ERROR_NONE);
182   if (type & BUNDLE_TYPE_ARRAY)
183     return 1;
184
185   return 0;
186 }
187
188 extern "C" EXPORT_API int bundle_keyval_type_is_measurable(
189     bundle_keyval_t* kv) {
190   int type = bundle_keyval_get_type(kv);
191   if (type == -1)
192     return -1;
193
194   set_last_result(BUNDLE_ERROR_NONE);
195   if (type & BUNDLE_TYPE_MEASURABLE)
196     return 1;
197
198   return 0;
199 }
200
201 extern "C" EXPORT_API int bundle_keyval_get_basic_val(bundle_keyval_t* kv,
202     void** val, size_t* size) {
203   if (kv == nullptr || val == nullptr || size == nullptr)
204     return BUNDLE_ERROR_INVALID_PARAMETER;
205
206   auto* key_info = reinterpret_cast<KeyInfo*>(kv);
207   auto& values = key_info->GetValues();
208   auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
209   *val = reinterpret_cast<void*>(&value[0]);
210   auto& values_size = key_info->GetValuesSize();
211   *size = reinterpret_cast<size_t>(values_size[0]);
212   return BUNDLE_ERROR_NONE;
213 }
214
215 extern "C" EXPORT_API int bundle_keyval_get_array_val(bundle_keyval_t* kv,
216     void*** array_val, unsigned int* array_len, size_t** array_item_size) {
217   if (kv == nullptr || array_val == nullptr || array_len == nullptr ||
218       array_item_size == nullptr)
219     return BUNDLE_ERROR_INVALID_PARAMETER;
220
221   auto* key_info = reinterpret_cast<KeyInfo*>(kv);
222   auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
223       key_info->GetValues());
224   auto& values_size = const_cast<std::vector<std::size_t>&>(
225       key_info->GetValuesSize());
226   *array_val = reinterpret_cast<void**>(&values[0]);
227   *array_len = static_cast<unsigned int>(values.size());
228   *array_item_size = reinterpret_cast<size_t*>(&values_size[0]);
229   return BUNDLE_ERROR_NONE;
230 }
231
232 extern "C" EXPORT_API bundle_keyval_t* bundle_keyval_dup(
233     const bundle_keyval_t* kv) {
234   if (kv == nullptr) {
235     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
236     return nullptr;
237   }
238
239   auto* keyval = const_cast<bundle_keyval_t*>(kv);
240   auto* key_info = reinterpret_cast<KeyInfo*>(keyval);
241   auto* k = new (std::nothrow) KeyInfo(*key_info);
242   if (k == nullptr) {
243     set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
244     return nullptr;
245   }
246
247   set_last_result(BUNDLE_ERROR_NONE);
248   return reinterpret_cast<bundle_keyval_t*>(k);
249 }
250
251 extern "C" EXPORT_API int bundle_keyval_free(bundle_keyval_t* kv) {
252   if (kv == nullptr)
253     return BUNDLE_ERROR_INVALID_PARAMETER;
254
255   auto* key_info = reinterpret_cast<KeyInfo*>(kv);
256   delete key_info;
257   return BUNDLE_ERROR_NONE;
258 }
259
260 extern "C" EXPORT_API bundle* bundle_dup(bundle* b_from) {
261   if (b_from == nullptr) {
262     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
263     return nullptr;
264   }
265
266   auto* h = reinterpret_cast<Bundle*>(b_from);
267   auto* b = new (std::nothrow) Bundle(*h);
268   if (b == nullptr) {
269     set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
270     return nullptr;
271   }
272
273   set_last_result(BUNDLE_ERROR_NONE);
274   return reinterpret_cast<bundle*>(b);
275 }
276
277 extern "C" EXPORT_API int bundle_encode(bundle *b, bundle_raw** raw, int* len) {
278   if (b == nullptr || raw == nullptr || len == nullptr)
279     return BUNDLE_ERROR_INVALID_PARAMETER;
280
281   auto* h = reinterpret_cast<Bundle*>(b);
282   *raw = reinterpret_cast<bundle_raw*>(h->Encode());
283   *len = strlen(reinterpret_cast<char*>(*raw));
284   return BUNDLE_ERROR_NONE;
285 }
286
287 extern "C" EXPORT_API int bundle_free_encoded_rawdata(bundle_raw **r) {
288   if (r == nullptr || *r == nullptr)
289     return BUNDLE_ERROR_INVALID_PARAMETER;
290
291   free(*r);
292   *r = nullptr;
293   return BUNDLE_ERROR_NONE;
294 }
295
296 extern "C" EXPORT_API bundle* bundle_decode(const bundle_raw* r,
297     const int data_size) {
298   if (r == nullptr) {
299     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
300     return nullptr;
301   }
302
303   Bundle* b = nullptr;
304   try {
305     auto* raw = const_cast<bundle_raw*>(r);
306     b = new (std::nothrow) Bundle(static_cast<unsigned char*>(raw), data_size);
307   } catch (Exception& e) {
308     set_last_result(e.GetErrorCode());
309     return nullptr;
310   }
311
312   set_last_result(BUNDLE_ERROR_NONE);
313   return reinterpret_cast<bundle*>(b);
314 }
315
316 extern "C" EXPORT_API int bundle_encode_raw(bundle* b, bundle_raw** r,
317     int* len) {
318   return bundle_encode(b, r, len);
319 }
320
321 extern "C" EXPORT_API bundle* bundle_decode_raw(const bundle_raw* r,
322     const int data_size) {
323   return bundle_decode(r, data_size);
324 }
325
326 extern "C" EXPORT_API int bundle_get_type(bundle* b, const char* key) {
327   if (b == nullptr || key == nullptr) {
328     set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
329     return BUNDLE_TYPE_NONE;
330   }
331
332   auto* h = reinterpret_cast<Bundle*>(b);
333   std::shared_ptr<KeyInfo> key_info;
334   try {
335     key_info = h->Get(key);
336   } catch (Exception& e) {
337     set_last_result(e.GetErrorCode());
338     return BUNDLE_TYPE_NONE;
339   }
340
341   set_last_result(BUNDLE_ERROR_NONE);
342   return key_info->GetType();
343 }
344
345 extern "C" EXPORT_API int bundle_add_str_array(bundle* b, const char* key,
346     const char** str_array, const int len) {
347   if (b == nullptr || key == nullptr || len <= 0)
348     return BUNDLE_ERROR_INVALID_PARAMETER;
349
350   auto* h = reinterpret_cast<Bundle*>(b);
351   std::vector<std::vector<unsigned char>> values(len);
352   if (str_array) {
353     for (int i = 0; i < len; ++i) {
354       std::vector<unsigned char> value(str_array[i],
355           str_array[i] + (strlen(str_array[i]) + 1));
356       values[i] = std::move(value);
357     }
358   }
359
360   std::shared_ptr<KeyInfo> key_info(
361       new (std::nothrow) KeyInfo(
362         (BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY), key, values));
363   if (key_info.get() == nullptr)
364     return BUNDLE_ERROR_OUT_OF_MEMORY;
365
366   try {
367     h->Add(std::move(key_info));
368   } catch (Exception& e) {
369     return e.GetErrorCode();
370   }
371
372   return BUNDLE_ERROR_NONE;
373 }
374
375 extern "C" EXPORT_API const char** bundle_get_str_array(bundle* b,
376     const char* key, int* len) {
377   if (b == nullptr || key == nullptr || len == nullptr) {
378     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
379     return nullptr;
380   }
381
382   auto* h = reinterpret_cast<Bundle*>(b);
383   std::shared_ptr<KeyInfo> key_info;
384   try {
385     key_info = h->Get(key);
386   } catch (Exception& e) {
387     set_last_result(e.GetErrorCode());
388     return nullptr;
389   }
390
391   if (key_info->GetType() != BUNDLE_TYPE_STR_ARRAY) {
392     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
393     return nullptr;
394   }
395
396   auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
397       key_info->GetValues());
398   auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
399   *len = static_cast<int>(raw_values.size());
400
401   set_last_result(BUNDLE_ERROR_NONE);
402   return const_cast<const char**>(reinterpret_cast<char**>(values));
403 }
404
405 extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key,
406     const void* bytes, const size_t size) {
407   if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
408     return BUNDLE_ERROR_INVALID_PARAMETER;
409
410   auto* h = reinterpret_cast<Bundle*>(b);
411   auto* p = reinterpret_cast<const unsigned char*>(bytes);
412   std::vector<unsigned char> value(p, p + size);
413
414   std::shared_ptr<KeyInfo> key_info(
415       new (std::nothrow) KeyInfo(BUNDLE_TYPE_BYTE, key, value));
416   if (key_info.get() == nullptr)
417     return BUNDLE_ERROR_OUT_OF_MEMORY;
418
419   try {
420     h->Add(std::move(key_info));
421   } catch (Exception& e) {
422     return e.GetErrorCode();
423   }
424
425   return BUNDLE_ERROR_NONE;
426 }
427
428 extern "C" EXPORT_API int bundle_get_byte(bundle* b, const char* key,
429     void** bytes, size_t* size) {
430   if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
431     return BUNDLE_ERROR_INVALID_PARAMETER;
432
433   auto* h = reinterpret_cast<Bundle*>(b);
434   std::shared_ptr<KeyInfo> key_info;
435   try {
436     key_info = h->Get(key);
437   } catch (Exception& e) {
438     return e.GetErrorCode();
439   }
440
441   if (key_info->GetType() != BUNDLE_TYPE_BYTE)
442     return BUNDLE_ERROR_INVALID_PARAMETER;
443
444   auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
445       key_info->GetValues());
446   auto& value = values[0];
447   *bytes = reinterpret_cast<void*>(&value[0]);
448   auto& values_size = key_info->GetValuesSize();
449   *size = reinterpret_cast<size_t>(values_size[0]);
450
451   return BUNDLE_ERROR_NONE;
452 }
453
454 extern "C" EXPORT_API int bundle_export_to_argv(bundle* b, char*** argv) {
455   if (b == nullptr || argv == nullptr) {
456     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
457     return -1;
458   }
459
460   auto* h = reinterpret_cast<Bundle*>(b);
461   std::vector<std::string> exported_vt;
462   try {
463     exported_vt = h->Export();
464   } catch (Exception& e) {
465     set_last_result(e.GetErrorCode());
466     return -1;
467   }
468
469   int argc = exported_vt.size();
470   auto** exported_argv = reinterpret_cast<char**>(
471       calloc(argc + 1, sizeof(char*)));
472   if (exported_argv == nullptr) {
473     set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
474     return -1;
475   }
476
477   exported_argv[1] = const_cast<char*>(TAG_IMPORT_EXPORT_CHECK);
478   for (unsigned int idx = 2; idx < exported_vt.size(); idx += 2) {
479     exported_argv[idx] = strdup(exported_vt[idx].c_str());
480     if (exported_argv[idx] == nullptr) {
481       set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
482       bundle_free_exported_argv(argc, &exported_argv);
483       return -1;
484     }
485
486     exported_argv[idx + 1] = strdup(exported_vt[idx + 1].c_str());
487     if (exported_argv[idx + 1] == nullptr) {
488       set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
489       bundle_free_exported_argv(argc, &exported_argv);
490       return -1;
491     }
492   }
493
494   *argv = exported_argv;
495   set_last_result(BUNDLE_ERROR_NONE);
496   return argc;
497 }
498
499 extern "C" EXPORT_API int bundle_free_exported_argv(int argc, char*** argv) {
500   if (argc < 2 || !argv || !*argv)
501     return BUNDLE_ERROR_INVALID_PARAMETER;
502
503   for (int i = 2; i < argc; i++)
504     std::free((*argv)[i]);
505
506   std::free(*argv);
507   *argv = nullptr;
508
509   return BUNDLE_ERROR_NONE;
510 }
511
512 extern "C" EXPORT_API bundle* bundle_import_from_argv(int argc, char** argv) {
513   if (argv == nullptr) {
514     set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
515     return nullptr;
516   }
517
518   Bundle* b = nullptr;
519   try {
520     b = new (std::nothrow) Bundle(argc, argv);
521   } catch (Exception& e) {
522     set_last_result(e.GetErrorCode());
523     return nullptr;
524   }
525
526   set_last_result(BUNDLE_ERROR_NONE);
527   return reinterpret_cast<bundle*>(b);
528 }
529
530 extern "C" EXPORT_API int bundle_compare(bundle* b1, bundle* b2) {
531   if (b1 == nullptr || b2 == nullptr)
532     return -1;
533
534   auto* h1 = reinterpret_cast<Bundle*>(b1);
535   auto* h2 = reinterpret_cast<Bundle*>(b2);
536   if (*h1 == *h2)
537     return 0;
538
539   return 1;
540 }
541
542 extern "C" EXPORT_API int bundle_set_str_array_element(bundle* b,
543     const char* key, const unsigned int idx, const char* val) {
544   if (b == nullptr || key == nullptr || val == nullptr)
545     return BUNDLE_ERROR_INVALID_PARAMETER;
546
547   auto len = strlen(val) + 1;
548   auto* p = reinterpret_cast<unsigned char*>(const_cast<char*>(val));
549   std::vector<unsigned char> value(p, p + len);
550
551   auto* h = reinterpret_cast<Bundle*>(b);
552   try {
553     h->Set(key, idx, value);
554   } catch (Exception& e) {
555     return e.GetErrorCode();
556   }
557
558   return BUNDLE_ERROR_NONE;
559 }
560
561 extern "C" EXPORT_API int bundle_add_byte_array(bundle* b,
562     const char* key, const unsigned int len) {
563   return bundle_init_byte_array(b, key, len);
564 }
565
566 extern "C" EXPORT_API int bundle_init_byte_array(bundle* b,
567     const char* key, const unsigned int len) {
568   if (b == nullptr || key == nullptr || len <= 0)
569     return BUNDLE_ERROR_INVALID_PARAMETER;
570
571   auto* h = reinterpret_cast<Bundle*>(b);
572   std::vector<std::vector<unsigned char>> values(len);
573   auto key_info = std::shared_ptr<KeyInfo>(
574       new (std::nothrow) KeyInfo(Bundle::Type::ByteArray, key, values));
575   if (key_info.get() == nullptr)
576     return BUNDLE_ERROR_OUT_OF_MEMORY;
577
578   try {
579     h->Add(key_info);
580   } catch (Exception& e) {
581     return e.GetErrorCode();
582   }
583
584   return BUNDLE_ERROR_NONE;
585 }
586
587 extern "C" EXPORT_API int bundle_get_byte_array(bundle* b,
588     const char* key, void*** bytes_array, unsigned int* len,
589     unsigned int** array_element_size) {
590   if (b == nullptr || key == nullptr || bytes_array == nullptr ||
591       len == nullptr || array_element_size == nullptr)
592     return BUNDLE_ERROR_INVALID_PARAMETER;
593
594   auto* h = reinterpret_cast<Bundle*>(b);
595   std::shared_ptr<KeyInfo> key_info;
596   try {
597     key_info = h->Get(key);
598   } catch (Exception& e) {
599     return e.GetErrorCode();
600   }
601
602   if (key_info->GetType() != BUNDLE_TYPE_BYTE_ARRAY)
603     return BUNDLE_ERROR_INVALID_PARAMETER;
604
605   auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
606       key_info->GetValues());
607   auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
608   *bytes_array = reinterpret_cast<void**>(values);
609   *len = static_cast<unsigned int>(raw_values.size());
610
611   auto& raw_values_size = const_cast<std::vector<unsigned int>&>(
612       key_info->GetUValuesSize());
613   *array_element_size = reinterpret_cast<unsigned int*>(&raw_values_size[0]);
614
615   return BUNDLE_ERROR_NONE;
616 }
617
618 extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b,
619     const char* key, const unsigned int idx,
620     const void* bytes, const size_t size) {
621   if (b == nullptr || key == nullptr || bytes == nullptr || size <= 0)
622     return BUNDLE_ERROR_INVALID_PARAMETER;
623
624   auto* p = reinterpret_cast<unsigned char*>(const_cast<void*>(bytes));
625   std::vector<unsigned char> value(p, p + size);
626
627   auto* h = reinterpret_cast<Bundle*>(b);
628   try {
629     h->Set(key, idx, value);
630   } catch (Exception& e) {
631     return e.GetErrorCode();
632   }
633
634   return BUNDLE_ERROR_NONE;
635 }
636
637 extern "C" EXPORT_API int bundle_to_json(bundle* b, char** json) {
638   if (b == nullptr || json == nullptr)
639     return BUNDLE_ERROR_INVALID_PARAMETER;
640
641   auto* h = reinterpret_cast<Bundle*>(b);
642   Json js(h);
643   try {
644     *json = strdup(js.ToString().c_str());
645   } catch (Exception& e) {
646     return e.GetErrorCode();
647   }
648
649   return BUNDLE_ERROR_NONE;
650 }
651
652 extern "C" EXPORT_API int bundle_from_json(const char* json, bundle** b) {
653   if (json == nullptr || b == nullptr)
654     return BUNDLE_ERROR_INVALID_PARAMETER;
655
656   Json js(json);
657   try {
658     *b = reinterpret_cast<bundle*>(js.ToBundle());
659   } catch (Exception& e) {
660     return e.GetErrorCode();
661   }
662
663   return BUNDLE_ERROR_NONE;
664 }