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

json_object.c 38 kB

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