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

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