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