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

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