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