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

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