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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. // INT64_MAX can't be exactly represented as a double
  605. // so cast to tell the compiler it's ok to round up.
  606. if (jso->o.c_double >= (double)INT64_MAX)
  607. return INT64_MAX;
  608. if (jso->o.c_double <= INT64_MIN)
  609. return INT64_MIN;
  610. return (int64_t)jso->o.c_double;
  611. case json_type_boolean:
  612. return jso->o.c_boolean;
  613. case json_type_string:
  614. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  615. return cint;
  616. /* FALLTHRU */
  617. default:
  618. return 0;
  619. }
  620. }
  621. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  622. if (!jso || jso->o_type!=json_type_int)
  623. return 0;
  624. jso->o.c_int64=new_value;
  625. return 1;
  626. }
  627. int json_object_int_inc(struct json_object *jso, int64_t val) {
  628. if (!jso || jso->o_type != json_type_int)
  629. return 0;
  630. if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
  631. jso->o.c_int64 = INT64_MAX;
  632. } else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
  633. jso->o.c_int64 = INT64_MIN;
  634. } else {
  635. jso->o.c_int64 += val;
  636. }
  637. return 1;
  638. }
  639. /* json_object_double */
  640. #if defined(HAVE___THREAD)
  641. // i.e. __thread or __declspec(thread)
  642. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  643. #endif
  644. static char *global_serialization_float_format = NULL;
  645. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  646. {
  647. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  648. {
  649. #if defined(HAVE___THREAD)
  650. if (tls_serialization_float_format)
  651. {
  652. free(tls_serialization_float_format);
  653. tls_serialization_float_format = NULL;
  654. }
  655. #endif
  656. if (global_serialization_float_format)
  657. free(global_serialization_float_format);
  658. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  659. }
  660. else if (global_or_thread == JSON_C_OPTION_THREAD)
  661. {
  662. #if defined(HAVE___THREAD)
  663. if (tls_serialization_float_format)
  664. {
  665. free(tls_serialization_float_format);
  666. tls_serialization_float_format = NULL;
  667. }
  668. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  669. #else
  670. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  671. return -1;
  672. #endif
  673. }
  674. else
  675. {
  676. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. static int json_object_double_to_json_string_format(struct json_object* jso,
  682. struct printbuf *pb,
  683. int level,
  684. int flags,
  685. const char *format)
  686. {
  687. char buf[128], *p, *q;
  688. int size;
  689. /* Although JSON RFC does not support
  690. NaN or Infinity as numeric values
  691. ECMA 262 section 9.8.1 defines
  692. how to handle these cases as strings */
  693. if (isnan(jso->o.c_double))
  694. {
  695. size = snprintf(buf, sizeof(buf), "NaN");
  696. }
  697. else if (isinf(jso->o.c_double))
  698. {
  699. if(jso->o.c_double > 0)
  700. size = snprintf(buf, sizeof(buf), "Infinity");
  701. else
  702. size = snprintf(buf, sizeof(buf), "-Infinity");
  703. }
  704. else
  705. {
  706. const char *std_format = "%.17g";
  707. int format_drops_decimals = 0;
  708. int looks_numeric = 0;
  709. if (!format)
  710. {
  711. #if defined(HAVE___THREAD)
  712. if (tls_serialization_float_format)
  713. format = tls_serialization_float_format;
  714. else
  715. #endif
  716. if (global_serialization_float_format)
  717. format = global_serialization_float_format;
  718. else
  719. format = std_format;
  720. }
  721. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  722. if (size < 0)
  723. return -1;
  724. p = strchr(buf, ',');
  725. if (p)
  726. *p = '.';
  727. else
  728. p = strchr(buf, '.');
  729. if (format == std_format || strstr(format, ".0f") == NULL)
  730. format_drops_decimals = 1;
  731. looks_numeric = /* Looks like *some* kind of number */
  732. isdigit((unsigned char)buf[0]) ||
  733. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  734. if (size < (int)sizeof(buf) - 2 &&
  735. looks_numeric &&
  736. !p && /* Has no decimal point */
  737. strchr(buf, 'e') == NULL && /* Not scientific notation */
  738. format_drops_decimals)
  739. {
  740. // Ensure it looks like a float, even if snprintf didn't,
  741. // unless a custom format is set to omit the decimal.
  742. strcat(buf, ".0");
  743. size += 2;
  744. }
  745. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  746. {
  747. /* last useful digit, always keep 1 zero */
  748. p++;
  749. for (q=p ; *q ; q++) {
  750. if (*q!='0') p=q;
  751. }
  752. /* drop trailing zeroes */
  753. *(++p) = 0;
  754. size = p-buf;
  755. }
  756. }
  757. // although unlikely, snprintf can fail
  758. if (size < 0)
  759. return -1;
  760. if (size >= (int)sizeof(buf))
  761. // The standard formats are guaranteed not to overrun the buffer,
  762. // but if a custom one happens to do so, just silently truncate.
  763. size = sizeof(buf) - 1;
  764. printbuf_memappend(pb, buf, size);
  765. return size;
  766. }
  767. static int json_object_double_to_json_string_default(struct json_object* jso,
  768. struct printbuf *pb,
  769. int level,
  770. int flags)
  771. {
  772. return json_object_double_to_json_string_format(jso, pb, level, flags,
  773. NULL);
  774. }
  775. int json_object_double_to_json_string(struct json_object* jso,
  776. struct printbuf *pb,
  777. int level,
  778. int flags)
  779. {
  780. return json_object_double_to_json_string_format(jso, pb, level, flags,
  781. (const char *)jso->_userdata);
  782. }
  783. struct json_object* json_object_new_double(double d)
  784. {
  785. struct json_object *jso = json_object_new(json_type_double);
  786. if (!jso)
  787. return NULL;
  788. jso->_to_json_string = &json_object_double_to_json_string_default;
  789. jso->o.c_double = d;
  790. return jso;
  791. }
  792. struct json_object* json_object_new_double_s(double d, const char *ds)
  793. {
  794. char *new_ds;
  795. struct json_object *jso = json_object_new_double(d);
  796. if (!jso)
  797. return NULL;
  798. new_ds = strdup(ds);
  799. if (!new_ds)
  800. {
  801. json_object_generic_delete(jso);
  802. errno = ENOMEM;
  803. return NULL;
  804. }
  805. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  806. new_ds, json_object_free_userdata);
  807. return jso;
  808. }
  809. int json_object_userdata_to_json_string(struct json_object *jso,
  810. struct printbuf *pb, int level, int flags)
  811. {
  812. int userdata_len = strlen((const char *)jso->_userdata);
  813. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  814. return userdata_len;
  815. }
  816. void json_object_free_userdata(struct json_object *jso, void *userdata)
  817. {
  818. free(userdata);
  819. }
  820. double json_object_get_double(const struct json_object *jso)
  821. {
  822. double cdouble;
  823. char *errPtr = NULL;
  824. if(!jso) return 0.0;
  825. switch(jso->o_type) {
  826. case json_type_double:
  827. return jso->o.c_double;
  828. case json_type_int:
  829. return jso->o.c_int64;
  830. case json_type_boolean:
  831. return jso->o.c_boolean;
  832. case json_type_string:
  833. errno = 0;
  834. cdouble = strtod(get_string_component(jso), &errPtr);
  835. /* if conversion stopped at the first character, return 0.0 */
  836. if (errPtr == get_string_component(jso))
  837. {
  838. errno = EINVAL;
  839. return 0.0;
  840. }
  841. /*
  842. * Check that the conversion terminated on something sensible
  843. *
  844. * For example, { "pay" : 123AB } would parse as 123.
  845. */
  846. if (*errPtr != '\0')
  847. {
  848. errno = EINVAL;
  849. return 0.0;
  850. }
  851. /*
  852. * If strtod encounters a string which would exceed the
  853. * capacity of a double, it returns +/- HUGE_VAL and sets
  854. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  855. * from a conversion, so we need to check errno.
  856. *
  857. * Underflow also sets errno to ERANGE, but it returns 0 in
  858. * that case, which is what we will return anyway.
  859. *
  860. * See CERT guideline ERR30-C
  861. */
  862. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  863. (ERANGE == errno))
  864. cdouble = 0.0;
  865. return cdouble;
  866. default:
  867. errno = EINVAL;
  868. return 0.0;
  869. }
  870. }
  871. int json_object_set_double(struct json_object *jso,double new_value){
  872. if (!jso || jso->o_type!=json_type_double)
  873. return 0;
  874. jso->o.c_double=new_value;
  875. return 1;
  876. }
  877. /* json_object_string */
  878. static int json_object_string_to_json_string(struct json_object* jso,
  879. struct printbuf *pb,
  880. int level,
  881. int flags)
  882. {
  883. printbuf_strappend(pb, "\"");
  884. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  885. printbuf_strappend(pb, "\"");
  886. return 0;
  887. }
  888. static void json_object_string_delete(struct json_object* jso)
  889. {
  890. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  891. free(jso->o.c_string.str.ptr);
  892. json_object_generic_delete(jso);
  893. }
  894. struct json_object* json_object_new_string(const char *s)
  895. {
  896. struct json_object *jso = json_object_new(json_type_string);
  897. if (!jso)
  898. return NULL;
  899. jso->_delete = &json_object_string_delete;
  900. jso->_to_json_string = &json_object_string_to_json_string;
  901. jso->o.c_string.len = strlen(s);
  902. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  903. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  904. } else {
  905. jso->o.c_string.str.ptr = strdup(s);
  906. if (!jso->o.c_string.str.ptr)
  907. {
  908. json_object_generic_delete(jso);
  909. errno = ENOMEM;
  910. return NULL;
  911. }
  912. }
  913. return jso;
  914. }
  915. struct json_object* json_object_new_string_len(const char *s, const int len)
  916. {
  917. char *dstbuf;
  918. struct json_object *jso = json_object_new(json_type_string);
  919. if (!jso)
  920. return NULL;
  921. jso->_delete = &json_object_string_delete;
  922. jso->_to_json_string = &json_object_string_to_json_string;
  923. if(len < LEN_DIRECT_STRING_DATA) {
  924. dstbuf = jso->o.c_string.str.data;
  925. } else {
  926. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  927. if (!jso->o.c_string.str.ptr)
  928. {
  929. json_object_generic_delete(jso);
  930. errno = ENOMEM;
  931. return NULL;
  932. }
  933. dstbuf = jso->o.c_string.str.ptr;
  934. }
  935. memcpy(dstbuf, (const void *)s, len);
  936. dstbuf[len] = '\0';
  937. jso->o.c_string.len = len;
  938. return jso;
  939. }
  940. const char* json_object_get_string(struct json_object *jso)
  941. {
  942. if (!jso)
  943. return NULL;
  944. switch(jso->o_type)
  945. {
  946. case json_type_string:
  947. return get_string_component(jso);
  948. default:
  949. return json_object_to_json_string(jso);
  950. }
  951. }
  952. int json_object_get_string_len(const struct json_object *jso)
  953. {
  954. if (!jso)
  955. return 0;
  956. switch(jso->o_type)
  957. {
  958. case json_type_string:
  959. return jso->o.c_string.len;
  960. default:
  961. return 0;
  962. }
  963. }
  964. int json_object_set_string(json_object* jso, const char* s) {
  965. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  966. }
  967. int json_object_set_string_len(json_object* jso, const char* s, int len){
  968. char *dstbuf;
  969. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  970. if (len<LEN_DIRECT_STRING_DATA) {
  971. dstbuf=jso->o.c_string.str.data;
  972. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  973. } else {
  974. dstbuf=(char *)malloc(len+1);
  975. if (dstbuf==NULL) return 0;
  976. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  977. jso->o.c_string.str.ptr=dstbuf;
  978. }
  979. jso->o.c_string.len=len;
  980. memcpy(dstbuf, (const void *)s, len);
  981. dstbuf[len] = '\0';
  982. return 1;
  983. }
  984. /* json_object_array */
  985. static int json_object_array_to_json_string(struct json_object* jso,
  986. struct printbuf *pb,
  987. int level,
  988. int flags)
  989. {
  990. int had_children = 0;
  991. size_t ii;
  992. printbuf_strappend(pb, "[");
  993. if (flags & JSON_C_TO_STRING_PRETTY)
  994. printbuf_strappend(pb, "\n");
  995. for(ii=0; ii < json_object_array_length(jso); ii++)
  996. {
  997. struct json_object *val;
  998. if (had_children)
  999. {
  1000. printbuf_strappend(pb, ",");
  1001. if (flags & JSON_C_TO_STRING_PRETTY)
  1002. printbuf_strappend(pb, "\n");
  1003. }
  1004. had_children = 1;
  1005. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1006. printbuf_strappend(pb, " ");
  1007. indent(pb, level + 1, flags);
  1008. val = json_object_array_get_idx(jso, ii);
  1009. if(val == NULL)
  1010. printbuf_strappend(pb, "null");
  1011. else
  1012. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  1013. return -1;
  1014. }
  1015. if (flags & JSON_C_TO_STRING_PRETTY)
  1016. {
  1017. if (had_children)
  1018. printbuf_strappend(pb, "\n");
  1019. indent(pb,level,flags);
  1020. }
  1021. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1022. return printbuf_strappend(pb, " ]");
  1023. return printbuf_strappend(pb, "]");
  1024. }
  1025. static void json_object_array_entry_free(void *data)
  1026. {
  1027. json_object_put((struct json_object*)data);
  1028. }
  1029. static void json_object_array_delete(struct json_object* jso)
  1030. {
  1031. array_list_free(jso->o.c_array);
  1032. json_object_generic_delete(jso);
  1033. }
  1034. struct json_object* json_object_new_array(void)
  1035. {
  1036. struct json_object *jso = json_object_new(json_type_array);
  1037. if (!jso)
  1038. return NULL;
  1039. jso->_delete = &json_object_array_delete;
  1040. jso->_to_json_string = &json_object_array_to_json_string;
  1041. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1042. if(jso->o.c_array == NULL)
  1043. {
  1044. free(jso);
  1045. return NULL;
  1046. }
  1047. return jso;
  1048. }
  1049. struct array_list* json_object_get_array(const struct json_object *jso)
  1050. {
  1051. if (!jso)
  1052. return NULL;
  1053. switch(jso->o_type)
  1054. {
  1055. case json_type_array:
  1056. return jso->o.c_array;
  1057. default:
  1058. return NULL;
  1059. }
  1060. }
  1061. void json_object_array_sort(struct json_object *jso,
  1062. int(*sort_fn)(const void *, const void *))
  1063. {
  1064. assert(json_object_get_type(jso) == json_type_array);
  1065. array_list_sort(jso->o.c_array, sort_fn);
  1066. }
  1067. struct json_object* json_object_array_bsearch(
  1068. const struct json_object *key,
  1069. const struct json_object *jso,
  1070. int (*sort_fn)(const void *, const void *))
  1071. {
  1072. struct json_object **result;
  1073. assert(json_object_get_type(jso) == json_type_array);
  1074. result = (struct json_object **)array_list_bsearch(
  1075. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1076. if (!result)
  1077. return NULL;
  1078. return *result;
  1079. }
  1080. size_t json_object_array_length(const struct json_object *jso)
  1081. {
  1082. assert(json_object_get_type(jso) == json_type_array);
  1083. return array_list_length(jso->o.c_array);
  1084. }
  1085. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1086. {
  1087. assert(json_object_get_type(jso) == json_type_array);
  1088. return array_list_add(jso->o.c_array, val);
  1089. }
  1090. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1091. struct json_object *val)
  1092. {
  1093. assert(json_object_get_type(jso) == json_type_array);
  1094. return array_list_put_idx(jso->o.c_array, idx, val);
  1095. }
  1096. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1097. {
  1098. assert(json_object_get_type(jso) == json_type_array);
  1099. return array_list_del_idx(jso->o.c_array, idx, count);
  1100. }
  1101. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1102. size_t idx)
  1103. {
  1104. assert(json_object_get_type(jso) == json_type_array);
  1105. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1106. }
  1107. static int json_array_equal(struct json_object* jso1,
  1108. struct json_object* jso2)
  1109. {
  1110. size_t len, i;
  1111. len = json_object_array_length(jso1);
  1112. if (len != json_object_array_length(jso2))
  1113. return 0;
  1114. for (i = 0; i < len; i++) {
  1115. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1116. json_object_array_get_idx(jso2, i)))
  1117. return 0;
  1118. }
  1119. return 1;
  1120. }
  1121. static int json_object_all_values_equal(struct json_object* jso1,
  1122. struct json_object* jso2)
  1123. {
  1124. struct json_object_iter iter;
  1125. struct json_object *sub;
  1126. assert(json_object_get_type(jso1) == json_type_object);
  1127. assert(json_object_get_type(jso2) == json_type_object);
  1128. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1129. json_object_object_foreachC(jso1, iter) {
  1130. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1131. (void**)(void *)&sub))
  1132. return 0;
  1133. if (!json_object_equal(iter.val, sub))
  1134. return 0;
  1135. }
  1136. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1137. json_object_object_foreachC(jso2, iter) {
  1138. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1139. (void**)(void *)&sub))
  1140. return 0;
  1141. }
  1142. return 1;
  1143. }
  1144. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1145. {
  1146. if (jso1 == jso2)
  1147. return 1;
  1148. if (!jso1 || !jso2)
  1149. return 0;
  1150. if (jso1->o_type != jso2->o_type)
  1151. return 0;
  1152. switch(jso1->o_type) {
  1153. case json_type_boolean:
  1154. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1155. case json_type_double:
  1156. return (jso1->o.c_double == jso2->o.c_double);
  1157. case json_type_int:
  1158. return (jso1->o.c_int64 == jso2->o.c_int64);
  1159. case json_type_string:
  1160. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1161. memcmp(get_string_component(jso1),
  1162. get_string_component(jso2),
  1163. jso1->o.c_string.len) == 0);
  1164. case json_type_object:
  1165. return json_object_all_values_equal(jso1, jso2);
  1166. case json_type_array:
  1167. return json_array_equal(jso1, jso2);
  1168. case json_type_null:
  1169. return 1;
  1170. };
  1171. return 0;
  1172. }
  1173. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1174. {
  1175. if (!src->_userdata && !src->_user_delete)
  1176. return 0;
  1177. if (dst->_to_json_string == json_object_userdata_to_json_string)
  1178. {
  1179. dst->_userdata = strdup(src->_userdata);
  1180. }
  1181. // else if ... other supported serializers ...
  1182. else
  1183. {
  1184. _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
  1185. return -1;
  1186. }
  1187. dst->_user_delete = src->_user_delete;
  1188. return 0;
  1189. }
  1190. /**
  1191. * The default shallow copy implementation. Simply creates a new object of the same
  1192. * type but does *not* copy over _userdata nor retain any custom serializer.
  1193. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1194. * implementation that is aware of how to copy them.
  1195. *
  1196. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1197. */
  1198. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  1199. {
  1200. switch (src->o_type) {
  1201. case json_type_boolean:
  1202. *dst = json_object_new_boolean(src->o.c_boolean);
  1203. break;
  1204. case json_type_double:
  1205. *dst = json_object_new_double(src->o.c_double);
  1206. break;
  1207. case json_type_int:
  1208. *dst = json_object_new_int64(src->o.c_int64);
  1209. break;
  1210. case json_type_string:
  1211. *dst = json_object_new_string(get_string_component(src));
  1212. break;
  1213. case json_type_object:
  1214. *dst = json_object_new_object();
  1215. break;
  1216. case json_type_array:
  1217. *dst = json_object_new_array();
  1218. break;
  1219. default:
  1220. errno = EINVAL;
  1221. return -1;
  1222. }
  1223. if (!*dst) {
  1224. errno = ENOMEM;
  1225. return -1;
  1226. }
  1227. (*dst)->_to_json_string = src->_to_json_string;
  1228. // _userdata and _user_delete are copied later
  1229. return 1;
  1230. }
  1231. /*
  1232. * The actual guts of json_object_deep_copy(), with a few additional args
  1233. * needed so we can keep track of where we are within the object tree.
  1234. *
  1235. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1236. */
  1237. 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)
  1238. {
  1239. struct json_object_iter iter;
  1240. size_t src_array_len, ii;
  1241. int shallow_copy_rc = 0;
  1242. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1243. /* -1=error, 1=object created ok, 2=userdata set */
  1244. if (shallow_copy_rc < 1)
  1245. {
  1246. errno = EINVAL;
  1247. return -1;
  1248. }
  1249. assert(*dst != NULL);
  1250. switch (src->o_type) {
  1251. case json_type_object:
  1252. json_object_object_foreachC(src, iter) {
  1253. struct json_object *jso = NULL;
  1254. /* This handles the `json_type_null` case */
  1255. if (!iter.val)
  1256. jso = NULL;
  1257. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
  1258. {
  1259. json_object_put(jso);
  1260. return -1;
  1261. }
  1262. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1263. {
  1264. json_object_put(jso);
  1265. return -1;
  1266. }
  1267. }
  1268. break;
  1269. case json_type_array:
  1270. src_array_len = json_object_array_length(src);
  1271. for (ii = 0; ii < src_array_len; ii++) {
  1272. struct json_object *jso = NULL;
  1273. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1274. /* This handles the `json_type_null` case */
  1275. if (!jso1)
  1276. jso = NULL;
  1277. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
  1278. {
  1279. json_object_put(jso);
  1280. return -1;
  1281. }
  1282. if (json_object_array_add(*dst, jso) < 0)
  1283. {
  1284. json_object_put(jso);
  1285. return -1;
  1286. }
  1287. }
  1288. break;
  1289. default:
  1290. break;
  1291. /* else, nothing to do, shallow_copy already did. */
  1292. }
  1293. if (shallow_copy_rc != 2)
  1294. return json_object_copy_serializer_data(src, *dst);
  1295. return 0;
  1296. }
  1297. int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1298. {
  1299. int rc;
  1300. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1301. if (!src || !dst || *dst) {
  1302. errno = EINVAL;
  1303. return -1;
  1304. }
  1305. if (shallow_copy == NULL)
  1306. shallow_copy = json_c_shallow_copy_default;
  1307. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1308. if (rc < 0) {
  1309. json_object_put(*dst);
  1310. *dst = NULL;
  1311. }
  1312. return rc;
  1313. }