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

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