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

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