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