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