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

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