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

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