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

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