You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 37 kB

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