You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 36 kB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #include "config.h"
  13. #include "strerror_override.h"
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include "debug.h"
  21. #include "printbuf.h"
  22. #include "linkhash.h"
  23. #include "arraylist.h"
  24. #include "json_inttypes.h"
  25. #include "json_object.h"
  26. #include "json_object_private.h"
  27. #include "json_util.h"
  28. #include "math_compat.h"
  29. #include "strdup_compat.h"
  30. #include "snprintf_compat.h"
  31. #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
  32. #error "The long long type isn't 64-bits"
  33. #endif
  34. // Don't define this. It's not thread-safe.
  35. /* #define REFCOUNT_DEBUG 1 */
  36. const char *json_number_chars = "0123456789.+-eE";
  37. const char *json_hex_chars = "0123456789abcdefABCDEF";
  38. static void json_object_generic_delete(struct json_object* jso);
  39. static struct json_object* json_object_new(enum json_type o_type);
  40. static json_object_to_json_string_fn json_object_object_to_json_string;
  41. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  42. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  43. static json_object_to_json_string_fn json_object_int_to_json_string;
  44. static json_object_to_json_string_fn json_object_string_to_json_string;
  45. static json_object_to_json_string_fn json_object_array_to_json_string;
  46. /* ref count debugging */
  47. #ifdef REFCOUNT_DEBUG
  48. static struct lh_table *json_object_table;
  49. static void json_object_init(void) __attribute__ ((constructor));
  50. static void json_object_init(void) {
  51. MC_DEBUG("json_object_init: creating object table\n");
  52. json_object_table = lh_kptr_table_new(128, NULL);
  53. }
  54. static void json_object_fini(void) __attribute__ ((destructor));
  55. static void json_object_fini(void)
  56. {
  57. struct lh_entry *ent;
  58. if (MC_GET_DEBUG())
  59. {
  60. if (json_object_table->count)
  61. {
  62. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  63. json_object_table->count);
  64. lh_foreach(json_object_table, ent)
  65. {
  66. struct json_object* obj =
  67. (struct json_object*) lh_entry_v(ent);
  68. MC_DEBUG("\t%s:%p\n",
  69. json_type_to_name(obj->o_type), obj);
  70. }
  71. }
  72. }
  73. MC_DEBUG("json_object_fini: freeing object table\n");
  74. lh_table_free(json_object_table);
  75. }
  76. #endif /* REFCOUNT_DEBUG */
  77. /* helper for accessing the optimized string data component in json_object
  78. */
  79. static const char *
  80. get_string_component(const struct json_object *jso)
  81. {
  82. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  83. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  84. }
  85. /* string escaping */
  86. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  87. {
  88. int pos = 0, start_offset = 0;
  89. unsigned char c;
  90. while (len--)
  91. {
  92. c = str[pos];
  93. switch(c)
  94. {
  95. case '\b':
  96. case '\n':
  97. case '\r':
  98. case '\t':
  99. case '\f':
  100. case '"':
  101. case '\\':
  102. case '/':
  103. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  104. {
  105. pos++;
  106. break;
  107. }
  108. if(pos - start_offset > 0)
  109. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  110. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  111. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  112. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  113. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  114. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  115. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  116. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  117. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  118. start_offset = ++pos;
  119. break;
  120. default:
  121. if(c < ' ')
  122. {
  123. char sbuf[7];
  124. if(pos - start_offset > 0)
  125. printbuf_memappend(pb,
  126. str + start_offset,
  127. pos - start_offset);
  128. snprintf(sbuf, sizeof(sbuf),
  129. "\\u00%c%c",
  130. json_hex_chars[c >> 4],
  131. json_hex_chars[c & 0xf]);
  132. printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1);
  133. start_offset = ++pos;
  134. } else
  135. pos++;
  136. }
  137. }
  138. if (pos - start_offset > 0)
  139. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  140. return 0;
  141. }
  142. /* reference counting */
  143. extern struct json_object* json_object_get(struct json_object *jso)
  144. {
  145. if (!jso) return jso;
  146. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  147. __sync_add_and_fetch(&jso->_ref_count, 1);
  148. #else
  149. ++jso->_ref_count;
  150. #endif
  151. return jso;
  152. }
  153. int json_object_put(struct json_object *jso)
  154. {
  155. if(!jso) return 0;
  156. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  157. /* Note: this only allow the refcount to remain correct
  158. * when multiple threads are adjusting it. It is still an error
  159. * for a thread to decrement the refcount if it doesn't "own" it,
  160. * as that can result in the thread that loses the race to 0
  161. * operating on an already-freed object.
  162. */
  163. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
  164. #else
  165. if (--jso->_ref_count > 0) return 0;
  166. #endif
  167. if (jso->_user_delete)
  168. jso->_user_delete(jso, jso->_userdata);
  169. jso->_delete(jso);
  170. return 1;
  171. }
  172. /* generic object construction and destruction parts */
  173. static void json_object_generic_delete(struct json_object* jso)
  174. {
  175. #ifdef REFCOUNT_DEBUG
  176. MC_DEBUG("json_object_delete_%s: %p\n",
  177. json_type_to_name(jso->o_type), jso);
  178. lh_table_delete(json_object_table, jso);
  179. #endif /* REFCOUNT_DEBUG */
  180. printbuf_free(jso->_pb);
  181. free(jso);
  182. }
  183. static struct json_object* json_object_new(enum json_type o_type)
  184. {
  185. struct json_object *jso;
  186. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  187. if (!jso)
  188. return NULL;
  189. jso->o_type = o_type;
  190. jso->_ref_count = 1;
  191. jso->_delete = &json_object_generic_delete;
  192. #ifdef REFCOUNT_DEBUG
  193. lh_table_insert(json_object_table, jso, jso);
  194. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  195. #endif /* REFCOUNT_DEBUG */
  196. return jso;
  197. }
  198. /* type checking functions */
  199. int json_object_is_type(const struct json_object *jso, enum json_type type)
  200. {
  201. if (!jso)
  202. return (type == json_type_null);
  203. return (jso->o_type == type);
  204. }
  205. enum json_type json_object_get_type(const struct json_object *jso)
  206. {
  207. if (!jso)
  208. return json_type_null;
  209. return jso->o_type;
  210. }
  211. void* json_object_get_userdata(json_object *jso) {
  212. return jso ? jso->_userdata : NULL;
  213. }
  214. void json_object_set_userdata(json_object *jso, void *userdata,
  215. json_object_delete_fn *user_delete)
  216. {
  217. // Can't return failure, so abort if we can't perform the operation.
  218. assert(jso != NULL);
  219. // First, clean up any previously existing user info
  220. if (jso->_user_delete)
  221. jso->_user_delete(jso, jso->_userdata);
  222. jso->_userdata = userdata;
  223. jso->_user_delete = user_delete;
  224. }
  225. /* set a custom conversion to string */
  226. void json_object_set_serializer(json_object *jso,
  227. json_object_to_json_string_fn *to_string_func,
  228. void *userdata,
  229. json_object_delete_fn *user_delete)
  230. {
  231. json_object_set_userdata(jso, userdata, user_delete);
  232. if (to_string_func == NULL)
  233. {
  234. // Reset to the standard serialization function
  235. switch(jso->o_type)
  236. {
  237. case json_type_null:
  238. jso->_to_json_string = NULL;
  239. break;
  240. case json_type_boolean:
  241. jso->_to_json_string = &json_object_boolean_to_json_string;
  242. break;
  243. case json_type_double:
  244. jso->_to_json_string = &json_object_double_to_json_string_default;
  245. break;
  246. case json_type_int:
  247. jso->_to_json_string = &json_object_int_to_json_string;
  248. break;
  249. case json_type_object:
  250. jso->_to_json_string = &json_object_object_to_json_string;
  251. break;
  252. case json_type_array:
  253. jso->_to_json_string = &json_object_array_to_json_string;
  254. break;
  255. case json_type_string:
  256. jso->_to_json_string = &json_object_string_to_json_string;
  257. break;
  258. }
  259. return;
  260. }
  261. jso->_to_json_string = to_string_func;
  262. }
  263. /* extended conversion to string */
  264. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  265. {
  266. const char *r = NULL;
  267. size_t s = 0;
  268. if (!jso)
  269. {
  270. s = 4;
  271. r = "null";
  272. }
  273. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  274. {
  275. printbuf_reset(jso->_pb);
  276. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  277. {
  278. s = (size_t)jso->_pb->bpos;
  279. r = jso->_pb->buf;
  280. }
  281. }
  282. if (length)
  283. *length = s;
  284. return r;
  285. }
  286. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  287. {
  288. return json_object_to_json_string_length(jso, flags, NULL);
  289. }
  290. /* backwards-compatible conversion to string */
  291. const char* json_object_to_json_string(struct json_object *jso)
  292. {
  293. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  294. }
  295. static void indent(struct printbuf *pb, int level, int flags)
  296. {
  297. if (flags & JSON_C_TO_STRING_PRETTY)
  298. {
  299. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  300. {
  301. printbuf_memset(pb, -1, '\t', level);
  302. }
  303. else
  304. {
  305. printbuf_memset(pb, -1, ' ', level * 2);
  306. }
  307. }
  308. }
  309. /* json_object_object */
  310. static int json_object_object_to_json_string(struct json_object* jso,
  311. struct printbuf *pb,
  312. int level,
  313. int flags)
  314. {
  315. int had_children = 0;
  316. struct json_object_iter iter;
  317. printbuf_strappend(pb, "{" /*}*/);
  318. if (flags & JSON_C_TO_STRING_PRETTY)
  319. printbuf_strappend(pb, "\n");
  320. json_object_object_foreachC(jso, iter)
  321. {
  322. if (had_children)
  323. {
  324. printbuf_strappend(pb, ",");
  325. if (flags & JSON_C_TO_STRING_PRETTY)
  326. printbuf_strappend(pb, "\n");
  327. }
  328. had_children = 1;
  329. if (flags & JSON_C_TO_STRING_SPACED)
  330. printbuf_strappend(pb, " ");
  331. indent(pb, level+1, flags);
  332. printbuf_strappend(pb, "\"");
  333. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  334. if (flags & JSON_C_TO_STRING_SPACED)
  335. printbuf_strappend(pb, "\": ");
  336. else
  337. printbuf_strappend(pb, "\":");
  338. if(iter.val == NULL)
  339. printbuf_strappend(pb, "null");
  340. else
  341. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  342. return -1;
  343. }
  344. if (flags & JSON_C_TO_STRING_PRETTY)
  345. {
  346. if (had_children)
  347. printbuf_strappend(pb, "\n");
  348. indent(pb,level,flags);
  349. }
  350. if (flags & JSON_C_TO_STRING_SPACED)
  351. return printbuf_strappend(pb, /*{*/ " }");
  352. else
  353. return printbuf_strappend(pb, /*{*/ "}");
  354. }
  355. static void json_object_lh_entry_free(struct lh_entry *ent)
  356. {
  357. if (!ent->k_is_constant)
  358. free(lh_entry_k(ent));
  359. json_object_put((struct json_object*)lh_entry_v(ent));
  360. }
  361. static void json_object_object_delete(struct json_object* jso)
  362. {
  363. lh_table_free(jso->o.c_object);
  364. json_object_generic_delete(jso);
  365. }
  366. struct json_object* json_object_new_object(void)
  367. {
  368. struct json_object *jso = json_object_new(json_type_object);
  369. if (!jso)
  370. return NULL;
  371. jso->_delete = &json_object_object_delete;
  372. jso->_to_json_string = &json_object_object_to_json_string;
  373. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  374. &json_object_lh_entry_free);
  375. if (!jso->o.c_object)
  376. {
  377. json_object_generic_delete(jso);
  378. errno = ENOMEM;
  379. return NULL;
  380. }
  381. return jso;
  382. }
  383. struct lh_table* json_object_get_object(const struct json_object *jso)
  384. {
  385. if (!jso)
  386. return NULL;
  387. switch(jso->o_type)
  388. {
  389. case json_type_object:
  390. return jso->o.c_object;
  391. default:
  392. return NULL;
  393. }
  394. }
  395. int json_object_object_add_ex(struct json_object* jso,
  396. const char *const key,
  397. struct json_object *const val,
  398. const unsigned opts)
  399. {
  400. struct json_object *existing_value = NULL;
  401. struct lh_entry *existing_entry;
  402. unsigned long hash;
  403. assert(json_object_get_type(jso) == json_type_object);
  404. // We lookup the entry and replace the value, rather than just deleting
  405. // and re-adding it, so the existing key remains valid.
  406. hash = lh_get_hash(jso->o.c_object, (const void *)key);
  407. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  408. lh_table_lookup_entry_w_hash(jso->o.c_object,
  409. (const void *)key, hash);
  410. // The caller must avoid creating loops in the object tree, but do a
  411. // quick check anyway to make sure we're not creating a trivial loop.
  412. if (jso == val)
  413. return -1;
  414. if (!existing_entry)
  415. {
  416. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  417. (const void *)key : strdup(key);
  418. if (k == NULL)
  419. return -1;
  420. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  421. }
  422. existing_value = (json_object *) lh_entry_v(existing_entry);
  423. if (existing_value)
  424. json_object_put(existing_value);
  425. existing_entry->v = val;
  426. return 0;
  427. }
  428. int json_object_object_add(struct json_object* jso, const char *key,
  429. struct json_object *val)
  430. {
  431. return json_object_object_add_ex(jso, key, val, 0);
  432. }
  433. int json_object_object_length(const struct json_object *jso)
  434. {
  435. assert(json_object_get_type(jso) == json_type_object);
  436. return lh_table_length(jso->o.c_object);
  437. }
  438. struct json_object* json_object_object_get(const struct json_object* jso,
  439. const char *key)
  440. {
  441. struct json_object *result = NULL;
  442. json_object_object_get_ex(jso, key, &result);
  443. return result;
  444. }
  445. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  446. struct json_object **value)
  447. {
  448. if (value != NULL)
  449. *value = NULL;
  450. if (NULL == jso)
  451. return FALSE;
  452. switch(jso->o_type)
  453. {
  454. case json_type_object:
  455. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  456. (void**) value);
  457. default:
  458. if (value != NULL)
  459. *value = NULL;
  460. return FALSE;
  461. }
  462. }
  463. void json_object_object_del(struct json_object* jso, const char *key)
  464. {
  465. assert(json_object_get_type(jso) == json_type_object);
  466. lh_table_delete(jso->o.c_object, key);
  467. }
  468. /* json_object_boolean */
  469. static int json_object_boolean_to_json_string(struct json_object* jso,
  470. struct printbuf *pb,
  471. int level,
  472. int flags)
  473. {
  474. if (jso->o.c_boolean)
  475. return printbuf_strappend(pb, "true");
  476. return printbuf_strappend(pb, "false");
  477. }
  478. struct json_object* json_object_new_boolean(json_bool b)
  479. {
  480. struct json_object *jso = json_object_new(json_type_boolean);
  481. if (!jso)
  482. return NULL;
  483. jso->_to_json_string = &json_object_boolean_to_json_string;
  484. jso->o.c_boolean = b;
  485. return jso;
  486. }
  487. json_bool json_object_get_boolean(const struct json_object *jso)
  488. {
  489. if (!jso)
  490. return FALSE;
  491. switch(jso->o_type)
  492. {
  493. case json_type_boolean:
  494. return jso->o.c_boolean;
  495. case json_type_int:
  496. return (jso->o.c_int64 != 0);
  497. case json_type_double:
  498. return (jso->o.c_double != 0);
  499. case json_type_string:
  500. return (jso->o.c_string.len != 0);
  501. default:
  502. return FALSE;
  503. }
  504. }
  505. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  506. if (!jso || jso->o_type!=json_type_boolean)
  507. return 0;
  508. jso->o.c_boolean=new_value;
  509. return 1;
  510. }
  511. /* json_object_int */
  512. static int json_object_int_to_json_string(struct json_object* jso,
  513. struct printbuf *pb,
  514. int level,
  515. int flags)
  516. {
  517. /* room for 19 digits, the sign char, and a null term */
  518. char sbuf[21];
  519. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
  520. return printbuf_memappend (pb, sbuf, strlen(sbuf));
  521. }
  522. struct json_object* json_object_new_int(int32_t i)
  523. {
  524. struct json_object *jso = json_object_new(json_type_int);
  525. if (!jso)
  526. return NULL;
  527. jso->_to_json_string = &json_object_int_to_json_string;
  528. jso->o.c_int64 = i;
  529. return jso;
  530. }
  531. int32_t json_object_get_int(const struct json_object *jso)
  532. {
  533. int64_t cint64;
  534. enum json_type o_type;
  535. if(!jso) return 0;
  536. o_type = jso->o_type;
  537. cint64 = jso->o.c_int64;
  538. if (o_type == json_type_string)
  539. {
  540. /*
  541. * Parse strings into 64-bit numbers, then use the
  542. * 64-to-32-bit number handling below.
  543. */
  544. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  545. return 0; /* whoops, it didn't work. */
  546. o_type = json_type_int;
  547. }
  548. switch(o_type) {
  549. case json_type_int:
  550. /* Make sure we return the correct values for out of range numbers. */
  551. if (cint64 <= INT32_MIN)
  552. return INT32_MIN;
  553. if (cint64 >= INT32_MAX)
  554. return INT32_MAX;
  555. return (int32_t) cint64;
  556. case json_type_double:
  557. if (jso->o.c_double <= INT32_MIN)
  558. return INT32_MIN;
  559. if (jso->o.c_double >= INT32_MAX)
  560. return INT32_MAX;
  561. return (int32_t)jso->o.c_double;
  562. case json_type_boolean:
  563. return jso->o.c_boolean;
  564. default:
  565. return 0;
  566. }
  567. }
  568. int json_object_set_int(struct json_object *jso,int new_value){
  569. if (!jso || jso->o_type!=json_type_int)
  570. return 0;
  571. jso->o.c_int64=new_value;
  572. return 1;
  573. }
  574. struct json_object* json_object_new_int64(int64_t i)
  575. {
  576. struct json_object *jso = json_object_new(json_type_int);
  577. if (!jso)
  578. return NULL;
  579. jso->_to_json_string = &json_object_int_to_json_string;
  580. jso->o.c_int64 = i;
  581. return jso;
  582. }
  583. int64_t json_object_get_int64(const struct json_object *jso)
  584. {
  585. int64_t cint;
  586. if (!jso)
  587. return 0;
  588. switch(jso->o_type)
  589. {
  590. case json_type_int:
  591. return jso->o.c_int64;
  592. case json_type_double:
  593. if (jso->o.c_double >= INT64_MAX)
  594. return INT64_MAX;
  595. if (jso->o.c_double <= INT64_MIN)
  596. return INT64_MIN;
  597. return (int64_t)jso->o.c_double;
  598. case json_type_boolean:
  599. return jso->o.c_boolean;
  600. case json_type_string:
  601. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  602. return cint;
  603. /* FALLTHRU */
  604. default:
  605. return 0;
  606. }
  607. }
  608. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  609. if (!jso || jso->o_type!=json_type_int)
  610. return 0;
  611. jso->o.c_int64=new_value;
  612. return 1;
  613. }
  614. int json_object_int_inc(struct json_object *jso, int64_t val) {
  615. if (!jso || jso->o_type != json_type_int)
  616. return 0;
  617. if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
  618. jso->o.c_int64 = INT64_MAX;
  619. } else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
  620. jso->o.c_int64 = INT64_MIN;
  621. } else {
  622. jso->o.c_int64 += val;
  623. }
  624. return 1;
  625. }
  626. /* json_object_double */
  627. #if defined(HAVE___THREAD)
  628. // i.e. __thread or __declspec(thread)
  629. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  630. #endif
  631. static char *global_serialization_float_format = NULL;
  632. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  633. {
  634. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  635. {
  636. #if defined(HAVE___THREAD)
  637. if (tls_serialization_float_format)
  638. {
  639. free(tls_serialization_float_format);
  640. tls_serialization_float_format = NULL;
  641. }
  642. #endif
  643. if (global_serialization_float_format)
  644. free(global_serialization_float_format);
  645. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  646. }
  647. else if (global_or_thread == JSON_C_OPTION_THREAD)
  648. {
  649. #if defined(HAVE___THREAD)
  650. if (tls_serialization_float_format)
  651. {
  652. free(tls_serialization_float_format);
  653. tls_serialization_float_format = NULL;
  654. }
  655. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  656. #else
  657. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  658. return -1;
  659. #endif
  660. }
  661. else
  662. {
  663. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  664. return -1;
  665. }
  666. return 0;
  667. }
  668. static int json_object_double_to_json_string_format(struct json_object* jso,
  669. struct printbuf *pb,
  670. int level,
  671. int flags,
  672. const char *format)
  673. {
  674. char buf[128], *p, *q;
  675. int size;
  676. double dummy; /* needed for modf() */
  677. /* Although JSON RFC does not support
  678. NaN or Infinity as numeric values
  679. ECMA 262 section 9.8.1 defines
  680. how to handle these cases as strings */
  681. if (isnan(jso->o.c_double))
  682. {
  683. size = snprintf(buf, sizeof(buf), "NaN");
  684. }
  685. else if (isinf(jso->o.c_double))
  686. {
  687. if(jso->o.c_double > 0)
  688. size = snprintf(buf, sizeof(buf), "Infinity");
  689. else
  690. size = snprintf(buf, sizeof(buf), "-Infinity");
  691. }
  692. else
  693. {
  694. const char *std_format = "%.17g";
  695. #if defined(HAVE___THREAD)
  696. if (tls_serialization_float_format)
  697. std_format = tls_serialization_float_format;
  698. else
  699. #endif
  700. if (global_serialization_float_format)
  701. std_format = global_serialization_float_format;
  702. if (!format)
  703. format = std_format;
  704. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  705. if (modf(jso->o.c_double, &dummy) == 0 && size >= 0 && size < (int)sizeof(buf) - 2)
  706. {
  707. // Ensure it looks like a float, even if snprintf didn't.
  708. strcat(buf, ".0");
  709. size += 2;
  710. }
  711. }
  712. // although unlikely, snprintf can fail
  713. if (size < 0)
  714. return -1;
  715. p = strchr(buf, ',');
  716. if (p)
  717. *p = '.';
  718. else
  719. p = strchr(buf, '.');
  720. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  721. {
  722. /* last useful digit, always keep 1 zero */
  723. p++;
  724. for (q=p ; *q ; q++) {
  725. if (*q!='0') p=q;
  726. }
  727. /* drop trailing zeroes */
  728. *(++p) = 0;
  729. size = p-buf;
  730. }
  731. if (size >= (int)sizeof(buf))
  732. // The standard formats are guaranteed not to overrun the buffer,
  733. // but if a custom one happens to do so, just silently truncate.
  734. size = sizeof(buf) - 1;
  735. printbuf_memappend(pb, buf, size);
  736. return size;
  737. }
  738. static int json_object_double_to_json_string_default(struct json_object* jso,
  739. struct printbuf *pb,
  740. int level,
  741. int flags)
  742. {
  743. return json_object_double_to_json_string_format(jso, pb, level, flags,
  744. NULL);
  745. }
  746. int json_object_double_to_json_string(struct json_object* jso,
  747. struct printbuf *pb,
  748. int level,
  749. int flags)
  750. {
  751. return json_object_double_to_json_string_format(jso, pb, level, flags,
  752. (const char *)jso->_userdata);
  753. }
  754. struct json_object* json_object_new_double(double d)
  755. {
  756. struct json_object *jso = json_object_new(json_type_double);
  757. if (!jso)
  758. return NULL;
  759. jso->_to_json_string = &json_object_double_to_json_string_default;
  760. jso->o.c_double = d;
  761. return jso;
  762. }
  763. struct json_object* json_object_new_double_s(double d, const char *ds)
  764. {
  765. char *new_ds;
  766. struct json_object *jso = json_object_new_double(d);
  767. if (!jso)
  768. return NULL;
  769. new_ds = strdup(ds);
  770. if (!new_ds)
  771. {
  772. json_object_generic_delete(jso);
  773. errno = ENOMEM;
  774. return NULL;
  775. }
  776. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  777. new_ds, json_object_free_userdata);
  778. return jso;
  779. }
  780. int json_object_userdata_to_json_string(struct json_object *jso,
  781. struct printbuf *pb, int level, int flags)
  782. {
  783. int userdata_len = strlen((const char *)jso->_userdata);
  784. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  785. return userdata_len;
  786. }
  787. void json_object_free_userdata(struct json_object *jso, void *userdata)
  788. {
  789. free(userdata);
  790. }
  791. double json_object_get_double(const struct json_object *jso)
  792. {
  793. double cdouble;
  794. char *errPtr = NULL;
  795. if(!jso) return 0.0;
  796. switch(jso->o_type) {
  797. case json_type_double:
  798. return jso->o.c_double;
  799. case json_type_int:
  800. return jso->o.c_int64;
  801. case json_type_boolean:
  802. return jso->o.c_boolean;
  803. case json_type_string:
  804. errno = 0;
  805. cdouble = strtod(get_string_component(jso), &errPtr);
  806. /* if conversion stopped at the first character, return 0.0 */
  807. if (errPtr == get_string_component(jso))
  808. return 0.0;
  809. /*
  810. * Check that the conversion terminated on something sensible
  811. *
  812. * For example, { "pay" : 123AB } would parse as 123.
  813. */
  814. if (*errPtr != '\0')
  815. return 0.0;
  816. /*
  817. * If strtod encounters a string which would exceed the
  818. * capacity of a double, it returns +/- HUGE_VAL and sets
  819. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  820. * from a conversion, so we need to check errno.
  821. *
  822. * Underflow also sets errno to ERANGE, but it returns 0 in
  823. * that case, which is what we will return anyway.
  824. *
  825. * See CERT guideline ERR30-C
  826. */
  827. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  828. (ERANGE == errno))
  829. cdouble = 0.0;
  830. return cdouble;
  831. default:
  832. return 0.0;
  833. }
  834. }
  835. int json_object_set_double(struct json_object *jso,double new_value){
  836. if (!jso || jso->o_type!=json_type_double)
  837. return 0;
  838. jso->o.c_double=new_value;
  839. return 1;
  840. }
  841. /* json_object_string */
  842. static int json_object_string_to_json_string(struct json_object* jso,
  843. struct printbuf *pb,
  844. int level,
  845. int flags)
  846. {
  847. printbuf_strappend(pb, "\"");
  848. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  849. printbuf_strappend(pb, "\"");
  850. return 0;
  851. }
  852. static void json_object_string_delete(struct json_object* jso)
  853. {
  854. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  855. free(jso->o.c_string.str.ptr);
  856. json_object_generic_delete(jso);
  857. }
  858. struct json_object* json_object_new_string(const char *s)
  859. {
  860. struct json_object *jso = json_object_new(json_type_string);
  861. if (!jso)
  862. return NULL;
  863. jso->_delete = &json_object_string_delete;
  864. jso->_to_json_string = &json_object_string_to_json_string;
  865. jso->o.c_string.len = strlen(s);
  866. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  867. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  868. } else {
  869. jso->o.c_string.str.ptr = strdup(s);
  870. if (!jso->o.c_string.str.ptr)
  871. {
  872. json_object_generic_delete(jso);
  873. errno = ENOMEM;
  874. return NULL;
  875. }
  876. }
  877. return jso;
  878. }
  879. struct json_object* json_object_new_string_len(const char *s, int len)
  880. {
  881. char *dstbuf;
  882. struct json_object *jso = json_object_new(json_type_string);
  883. if (!jso)
  884. return NULL;
  885. jso->_delete = &json_object_string_delete;
  886. jso->_to_json_string = &json_object_string_to_json_string;
  887. if(len < LEN_DIRECT_STRING_DATA) {
  888. dstbuf = jso->o.c_string.str.data;
  889. } else {
  890. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  891. if (!jso->o.c_string.str.ptr)
  892. {
  893. json_object_generic_delete(jso);
  894. errno = ENOMEM;
  895. return NULL;
  896. }
  897. dstbuf = jso->o.c_string.str.ptr;
  898. }
  899. memcpy(dstbuf, (const void *)s, len);
  900. dstbuf[len] = '\0';
  901. jso->o.c_string.len = len;
  902. return jso;
  903. }
  904. const char* json_object_get_string(struct json_object *jso)
  905. {
  906. if (!jso)
  907. return NULL;
  908. switch(jso->o_type)
  909. {
  910. case json_type_string:
  911. return get_string_component(jso);
  912. default:
  913. return json_object_to_json_string(jso);
  914. }
  915. }
  916. int json_object_get_string_len(const struct json_object *jso)
  917. {
  918. if (!jso)
  919. return 0;
  920. switch(jso->o_type)
  921. {
  922. case json_type_string:
  923. return jso->o.c_string.len;
  924. default:
  925. return 0;
  926. }
  927. }
  928. int json_object_set_string(json_object* jso, const char* s) {
  929. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  930. }
  931. int json_object_set_string_len(json_object* jso, const char* s, int len){
  932. char *dstbuf;
  933. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  934. if (len<LEN_DIRECT_STRING_DATA) {
  935. dstbuf=jso->o.c_string.str.data;
  936. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  937. } else {
  938. dstbuf=(char *)malloc(len+1);
  939. if (dstbuf==NULL) return 0;
  940. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  941. jso->o.c_string.str.ptr=dstbuf;
  942. }
  943. jso->o.c_string.len=len;
  944. memcpy(dstbuf, (const void *)s, len);
  945. dstbuf[len] = '\0';
  946. return 1;
  947. }
  948. /* json_object_array */
  949. static int json_object_array_to_json_string(struct json_object* jso,
  950. struct printbuf *pb,
  951. int level,
  952. int flags)
  953. {
  954. int had_children = 0;
  955. size_t ii;
  956. printbuf_strappend(pb, "[");
  957. if (flags & JSON_C_TO_STRING_PRETTY)
  958. printbuf_strappend(pb, "\n");
  959. for(ii=0; ii < json_object_array_length(jso); ii++)
  960. {
  961. struct json_object *val;
  962. if (had_children)
  963. {
  964. printbuf_strappend(pb, ",");
  965. if (flags & JSON_C_TO_STRING_PRETTY)
  966. printbuf_strappend(pb, "\n");
  967. }
  968. had_children = 1;
  969. if (flags & JSON_C_TO_STRING_SPACED)
  970. printbuf_strappend(pb, " ");
  971. indent(pb, level + 1, flags);
  972. val = json_object_array_get_idx(jso, ii);
  973. if(val == NULL)
  974. printbuf_strappend(pb, "null");
  975. else
  976. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  977. return -1;
  978. }
  979. if (flags & JSON_C_TO_STRING_PRETTY)
  980. {
  981. if (had_children)
  982. printbuf_strappend(pb, "\n");
  983. indent(pb,level,flags);
  984. }
  985. if (flags & JSON_C_TO_STRING_SPACED)
  986. return printbuf_strappend(pb, " ]");
  987. return printbuf_strappend(pb, "]");
  988. }
  989. static void json_object_array_entry_free(void *data)
  990. {
  991. json_object_put((struct json_object*)data);
  992. }
  993. static void json_object_array_delete(struct json_object* jso)
  994. {
  995. array_list_free(jso->o.c_array);
  996. json_object_generic_delete(jso);
  997. }
  998. struct json_object* json_object_new_array(void)
  999. {
  1000. struct json_object *jso = json_object_new(json_type_array);
  1001. if (!jso)
  1002. return NULL;
  1003. jso->_delete = &json_object_array_delete;
  1004. jso->_to_json_string = &json_object_array_to_json_string;
  1005. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1006. if(jso->o.c_array == NULL)
  1007. {
  1008. free(jso);
  1009. return NULL;
  1010. }
  1011. return jso;
  1012. }
  1013. struct array_list* json_object_get_array(const struct json_object *jso)
  1014. {
  1015. if (!jso)
  1016. return NULL;
  1017. switch(jso->o_type)
  1018. {
  1019. case json_type_array:
  1020. return jso->o.c_array;
  1021. default:
  1022. return NULL;
  1023. }
  1024. }
  1025. void json_object_array_sort(struct json_object *jso,
  1026. int(*sort_fn)(const void *, const void *))
  1027. {
  1028. assert(json_object_get_type(jso) == json_type_array);
  1029. array_list_sort(jso->o.c_array, sort_fn);
  1030. }
  1031. struct json_object* json_object_array_bsearch(
  1032. const struct json_object *key,
  1033. const struct json_object *jso,
  1034. int (*sort_fn)(const void *, const void *))
  1035. {
  1036. struct json_object **result;
  1037. assert(json_object_get_type(jso) == json_type_array);
  1038. result = (struct json_object **)array_list_bsearch(
  1039. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1040. if (!result)
  1041. return NULL;
  1042. return *result;
  1043. }
  1044. size_t json_object_array_length(const struct json_object *jso)
  1045. {
  1046. assert(json_object_get_type(jso) == json_type_array);
  1047. return array_list_length(jso->o.c_array);
  1048. }
  1049. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1050. {
  1051. assert(json_object_get_type(jso) == json_type_array);
  1052. return array_list_add(jso->o.c_array, val);
  1053. }
  1054. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1055. struct json_object *val)
  1056. {
  1057. assert(json_object_get_type(jso) == json_type_array);
  1058. return array_list_put_idx(jso->o.c_array, idx, val);
  1059. }
  1060. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1061. {
  1062. assert(json_object_get_type(jso) == json_type_array);
  1063. return array_list_del_idx(jso->o.c_array, idx, count);
  1064. }
  1065. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1066. size_t idx)
  1067. {
  1068. assert(json_object_get_type(jso) == json_type_array);
  1069. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1070. }
  1071. static int json_array_equal(struct json_object* jso1,
  1072. struct json_object* jso2)
  1073. {
  1074. size_t len, i;
  1075. len = json_object_array_length(jso1);
  1076. if (len != json_object_array_length(jso2))
  1077. return 0;
  1078. for (i = 0; i < len; i++) {
  1079. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1080. json_object_array_get_idx(jso2, i)))
  1081. return 0;
  1082. }
  1083. return 1;
  1084. }
  1085. static int json_object_all_values_equal(struct json_object* jso1,
  1086. struct json_object* jso2)
  1087. {
  1088. struct json_object_iter iter;
  1089. struct json_object *sub;
  1090. assert(json_object_get_type(jso1) == json_type_object);
  1091. assert(json_object_get_type(jso2) == json_type_object);
  1092. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1093. json_object_object_foreachC(jso1, iter) {
  1094. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1095. (void**)(void *)&sub))
  1096. return 0;
  1097. if (!json_object_equal(iter.val, sub))
  1098. return 0;
  1099. }
  1100. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1101. json_object_object_foreachC(jso2, iter) {
  1102. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1103. (void**)(void *)&sub))
  1104. return 0;
  1105. }
  1106. return 1;
  1107. }
  1108. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1109. {
  1110. if (jso1 == jso2)
  1111. return 1;
  1112. if (!jso1 || !jso2)
  1113. return 0;
  1114. if (jso1->o_type != jso2->o_type)
  1115. return 0;
  1116. switch(jso1->o_type) {
  1117. case json_type_boolean:
  1118. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1119. case json_type_double:
  1120. return (jso1->o.c_double == jso2->o.c_double);
  1121. case json_type_int:
  1122. return (jso1->o.c_int64 == jso2->o.c_int64);
  1123. case json_type_string:
  1124. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1125. memcmp(get_string_component(jso1),
  1126. get_string_component(jso2),
  1127. jso1->o.c_string.len) == 0);
  1128. case json_type_object:
  1129. return json_object_all_values_equal(jso1, jso2);
  1130. case json_type_array:
  1131. return json_array_equal(jso1, jso2);
  1132. case json_type_null:
  1133. return 1;
  1134. };
  1135. return 0;
  1136. }
  1137. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1138. {
  1139. if (!src->_userdata && !src->_user_delete)
  1140. return 0;
  1141. if (dst->_to_json_string == json_object_userdata_to_json_string)
  1142. {
  1143. dst->_userdata = strdup(src->_userdata);
  1144. }
  1145. // else if ... other supported serializers ...
  1146. else
  1147. {
  1148. _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
  1149. return -1;
  1150. }
  1151. dst->_user_delete = src->_user_delete;
  1152. return 0;
  1153. }
  1154. /**
  1155. * The default shallow copy implementation. Simply creates a new object of the same
  1156. * type but does *not* copy over _userdata nor retain any custom serializer.
  1157. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1158. * implementation that is aware of how to copy them.
  1159. *
  1160. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1161. */
  1162. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  1163. {
  1164. switch (src->o_type) {
  1165. case json_type_boolean:
  1166. *dst = json_object_new_boolean(src->o.c_boolean);
  1167. break;
  1168. case json_type_double:
  1169. *dst = json_object_new_double(src->o.c_double);
  1170. break;
  1171. case json_type_int:
  1172. *dst = json_object_new_int64(src->o.c_int64);
  1173. break;
  1174. case json_type_string:
  1175. *dst = json_object_new_string(get_string_component(src));
  1176. break;
  1177. case json_type_object:
  1178. *dst = json_object_new_object();
  1179. break;
  1180. case json_type_array:
  1181. *dst = json_object_new_array();
  1182. break;
  1183. default:
  1184. errno = EINVAL;
  1185. return -1;
  1186. }
  1187. if (!*dst) {
  1188. errno = ENOMEM;
  1189. return -1;
  1190. }
  1191. (*dst)->_to_json_string = src->_to_json_string;
  1192. // _userdata and _user_delete are copied later
  1193. return 1;
  1194. }
  1195. /*
  1196. * The actual guts of json_object_deep_copy(), with a few additional args
  1197. * needed so we can keep track of where we are within the object tree.
  1198. *
  1199. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1200. */
  1201. static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, const char *key_in_parent, size_t index_in_parent, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1202. {
  1203. struct json_object_iter iter;
  1204. size_t src_array_len, ii;
  1205. int shallow_copy_rc = 0;
  1206. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1207. /* -1=error, 1=object created ok, 2=userdata set */
  1208. if (shallow_copy_rc < 1)
  1209. {
  1210. errno = EINVAL;
  1211. return -1;
  1212. }
  1213. assert(*dst != NULL);
  1214. switch (src->o_type) {
  1215. case json_type_object:
  1216. json_object_object_foreachC(src, iter) {
  1217. struct json_object *jso = NULL;
  1218. /* This handles the `json_type_null` case */
  1219. if (!iter.val)
  1220. jso = NULL;
  1221. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
  1222. {
  1223. json_object_put(jso);
  1224. return -1;
  1225. }
  1226. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1227. {
  1228. json_object_put(jso);
  1229. return -1;
  1230. }
  1231. }
  1232. break;
  1233. case json_type_array:
  1234. src_array_len = json_object_array_length(src);
  1235. for (ii = 0; ii < src_array_len; ii++) {
  1236. struct json_object *jso = NULL;
  1237. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1238. /* This handles the `json_type_null` case */
  1239. if (!jso1)
  1240. jso = NULL;
  1241. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
  1242. {
  1243. json_object_put(jso);
  1244. return -1;
  1245. }
  1246. if (json_object_array_add(*dst, jso) < 0)
  1247. {
  1248. json_object_put(jso);
  1249. return -1;
  1250. }
  1251. }
  1252. break;
  1253. default:
  1254. break;
  1255. /* else, nothing to do, shallow_copy already did. */
  1256. }
  1257. if (shallow_copy_rc != 2)
  1258. return json_object_copy_serializer_data(src, *dst);
  1259. return 0;
  1260. }
  1261. int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1262. {
  1263. int rc;
  1264. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1265. if (!src || !dst || *dst) {
  1266. errno = EINVAL;
  1267. return -1;
  1268. }
  1269. if (shallow_copy == NULL)
  1270. shallow_copy = json_c_shallow_copy_default;
  1271. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1272. if (rc < 0) {
  1273. json_object_put(*dst);
  1274. *dst = NULL;
  1275. }
  1276. return rc;
  1277. }