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