json_object.c 36 KB

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