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