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