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