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 37 kB

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