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