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

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