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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  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. #ifdef HAVE_LIMITS_H
  17. #include <limits.h>
  18. #endif
  19. #include <math.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "arraylist.h"
  25. #include "debug.h"
  26. #include "json_inttypes.h"
  27. #include "json_object.h"
  28. #include "json_object_private.h"
  29. #include "json_util.h"
  30. #include "linkhash.h"
  31. #include "math_compat.h"
  32. #include "printbuf.h"
  33. #include "snprintf_compat.h"
  34. #include "strdup_compat.h"
  35. #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
  36. #error "The long long type isn't 64-bits"
  37. #endif
  38. #ifndef SSIZE_T_MAX
  39. #if SIZEOF_SSIZE_T == SIZEOF_INT
  40. #define SSIZE_T_MAX UINT_MAX
  41. #elif SIZEOF_SSIZE_T == SIZEOF_LONG
  42. #define SSIZE_T_MAX ULONG_MAX
  43. #elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG
  44. #define SSIZE_T_MAX ULLONG_MAX
  45. #else
  46. #error Unable to determine size of ssize_t
  47. #endif
  48. #endif
  49. // Don't define this. It's not thread-safe.
  50. /* #define REFCOUNT_DEBUG 1 */
  51. const char *json_number_chars = "0123456789.+-eE";
  52. const char *json_hex_chars = "0123456789abcdefABCDEF";
  53. static void json_object_generic_delete(struct json_object *jso);
  54. static void Xjson_object_generic_delete(struct json_object_base *jso);
  55. static struct json_object *json_object_new(enum json_type o_type);
  56. #if defined(_MSC_VER) && (_MSC_VER <= 1800)
  57. /* VS2013 doesn't know about "inline" */
  58. #define inline __inline
  59. #elif defined(AIX_CC)
  60. #define inline
  61. #endif
  62. // XAX kill this once json_object_base is renamed back to json_object
  63. static inline struct json_object *PUBLIC(struct json_object_base *jso)
  64. {
  65. return (struct json_object *)jso;
  66. }
  67. static inline const struct json_object *PUBLIC_C(const struct json_object_base *jso)
  68. {
  69. return (const struct json_object *)jso;
  70. }
  71. /*
  72. * Helper functions to more safely cast to a particular type of json_object
  73. */
  74. static inline struct json_object_object *JC_OBJECT(struct json_object_base *jso)
  75. {
  76. return (void *)jso;
  77. }
  78. static inline const struct json_object_object *JC_OBJECT_C(const struct json_object_base *jso)
  79. {
  80. return (const void *)jso;
  81. }
  82. static inline struct json_object_array *JC_ARRAY(struct json_object_base *jso)
  83. {
  84. return (void *)jso;
  85. }
  86. static inline const struct json_object_array *JC_ARRAY_C(const struct json_object_base *jso)
  87. {
  88. return (const void *)jso;
  89. }
  90. static inline struct json_object_boolean *JC_BOOL(struct json_object_base *jso)
  91. {
  92. return (void *)jso;
  93. }
  94. static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object_base *jso)
  95. {
  96. return (const void *)jso;
  97. }
  98. static inline struct json_object_double *JC_DOUBLE(struct json_object_base *jso)
  99. {
  100. return (void *)jso;
  101. }
  102. static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object_base *jso)
  103. {
  104. return (const void *)jso;
  105. }
  106. static inline struct json_object_int *JC_INT(struct json_object_base *jso)
  107. {
  108. return (void *)jso;
  109. }
  110. static inline const struct json_object_int *JC_INT_C(const struct json_object_base *jso)
  111. {
  112. return (const void *)jso;
  113. }
  114. static inline struct json_object_string *JC_STRING(struct json_object_base *jso)
  115. {
  116. return (void *)jso;
  117. }
  118. static inline const struct json_object_string *JC_STRING_C(const struct json_object_base *jso)
  119. {
  120. return (const void *)jso;
  121. }
  122. #define JC_CONCAT(a,b) a##b
  123. #define JC_CONCAT3(a,b,c) a##b##c
  124. #define JSON_OBJECT_NEW(jtype, delete_fn) \
  125. (struct JC_CONCAT(json_object_,jtype) *)Xjson_object_new(JC_CONCAT(json_type_,jtype), \
  126. sizeof(struct JC_CONCAT(json_object_,jtype)), \
  127. &JC_CONCAT3(json_object_,jtype,_to_json_string), \
  128. (void *)delete_fn) // XAX drop cast
  129. static inline struct json_object_base *Xjson_object_new(enum json_type o_type,
  130. size_t alloc_size,
  131. json_object_to_json_string_fn *to_json_string,
  132. json_object_private_delete_fn *delete_fn);
  133. static json_object_to_json_string_fn json_object_object_to_json_string;
  134. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  135. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  136. static json_object_to_json_string_fn json_object_int_to_json_string;
  137. static json_object_to_json_string_fn json_object_string_to_json_string;
  138. static json_object_to_json_string_fn json_object_array_to_json_string;
  139. static json_object_to_json_string_fn _json_object_userdata_to_json_string;
  140. #ifndef JSON_NORETURN
  141. #if defined(_MSC_VER)
  142. #define JSON_NORETURN __declspec(noreturn)
  143. #elif defined(__OS400__)
  144. #define JSON_NORETURN
  145. #else
  146. /* 'cold' attribute is for optimization, telling the computer this code
  147. * path is unlikely.
  148. */
  149. #define JSON_NORETURN __attribute__((noreturn, cold))
  150. #endif
  151. #endif
  152. /**
  153. * Abort and optionally print a message on standard error.
  154. * This should be used rather than assert() for unconditional abortion
  155. * (in particular for code paths which are never supposed to be run).
  156. * */
  157. JSON_NORETURN static void json_abort(const char *message);
  158. /* ref count debugging */
  159. #ifdef REFCOUNT_DEBUG
  160. static struct lh_table *json_object_table;
  161. static void json_object_init(void) __attribute__((constructor));
  162. static void json_object_init(void)
  163. {
  164. MC_DEBUG("json_object_init: creating object table\n");
  165. json_object_table = lh_kptr_table_new(128, NULL);
  166. }
  167. static void json_object_fini(void) __attribute__((destructor));
  168. static void json_object_fini(void)
  169. {
  170. struct lh_entry *ent;
  171. if (MC_GET_DEBUG())
  172. {
  173. if (json_object_table->count)
  174. {
  175. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  176. json_object_table->count);
  177. lh_foreach(json_object_table, ent)
  178. {
  179. struct json_object *obj = (struct json_object *)lh_entry_v(ent);
  180. MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
  181. }
  182. }
  183. }
  184. MC_DEBUG("json_object_fini: freeing object table\n");
  185. lh_table_free(json_object_table);
  186. }
  187. #endif /* REFCOUNT_DEBUG */
  188. /* helper for accessing the optimized string data component in json_object
  189. */
  190. static inline char *get_string_component_mutable(struct json_object_base *jso)
  191. {
  192. if (JC_STRING_C(jso)->len < 0)
  193. {
  194. /* Due to json_object_str_string(), we might have a pointer */
  195. return JC_STRING(jso)->c_string.pdata;
  196. }
  197. return JC_STRING(jso)->c_string.idata;
  198. }
  199. static inline const char *get_string_component(const struct json_object_base *jso)
  200. {
  201. return get_string_component_mutable((void *)(uintptr_t)(const void *)jso);
  202. }
  203. /* string escaping */
  204. static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags)
  205. {
  206. int pos = 0, start_offset = 0;
  207. unsigned char c;
  208. while (len--)
  209. {
  210. c = str[pos];
  211. switch (c)
  212. {
  213. case '\b':
  214. case '\n':
  215. case '\r':
  216. case '\t':
  217. case '\f':
  218. case '"':
  219. case '\\':
  220. case '/':
  221. if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  222. {
  223. pos++;
  224. break;
  225. }
  226. if (pos - start_offset > 0)
  227. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  228. if (c == '\b')
  229. printbuf_memappend(pb, "\\b", 2);
  230. else if (c == '\n')
  231. printbuf_memappend(pb, "\\n", 2);
  232. else if (c == '\r')
  233. printbuf_memappend(pb, "\\r", 2);
  234. else if (c == '\t')
  235. printbuf_memappend(pb, "\\t", 2);
  236. else if (c == '\f')
  237. printbuf_memappend(pb, "\\f", 2);
  238. else if (c == '"')
  239. printbuf_memappend(pb, "\\\"", 2);
  240. else if (c == '\\')
  241. printbuf_memappend(pb, "\\\\", 2);
  242. else if (c == '/')
  243. printbuf_memappend(pb, "\\/", 2);
  244. start_offset = ++pos;
  245. break;
  246. default:
  247. if (c < ' ')
  248. {
  249. char sbuf[7];
  250. if (pos - start_offset > 0)
  251. printbuf_memappend(pb, str + start_offset,
  252. pos - start_offset);
  253. snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4],
  254. json_hex_chars[c & 0xf]);
  255. printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1);
  256. start_offset = ++pos;
  257. }
  258. else
  259. pos++;
  260. }
  261. }
  262. if (pos - start_offset > 0)
  263. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  264. return 0;
  265. }
  266. /* reference counting */
  267. struct json_object *json_object_get(struct json_object *jso)
  268. {
  269. if (!jso)
  270. return jso;
  271. // Don't overflow the refcounter.
  272. assert(jso->_ref_count < UINT32_MAX);
  273. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  274. __sync_add_and_fetch(&jso->_ref_count, 1);
  275. #else
  276. ++jso->_ref_count;
  277. #endif
  278. return jso;
  279. }
  280. // XAX remove this Xjson_object_put function once conversion is done
  281. static int Xjson_object_put(struct json_object_base *jso)
  282. {
  283. if (!jso)
  284. return 0;
  285. /* Avoid invalid free and crash explicitly instead of (silently)
  286. * segfaulting.
  287. */
  288. assert(jso->_ref_count > 0);
  289. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  290. /* Note: this only allow the refcount to remain correct
  291. * when multiple threads are adjusting it. It is still an error
  292. * for a thread to decrement the refcount if it doesn't "own" it,
  293. * as that can result in the thread that loses the race to 0
  294. * operating on an already-freed object.
  295. */
  296. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
  297. return 0;
  298. #else
  299. if (--jso->_ref_count > 0)
  300. return 0;
  301. #endif
  302. if (jso->_user_delete)
  303. jso->_user_delete(PUBLIC(jso), jso->_userdata);
  304. jso->_delete(PUBLIC(jso));
  305. return 1;
  306. }
  307. int json_object_put(struct json_object *jso)
  308. {
  309. if (!jso)
  310. return 0;
  311. if (jso->newold)
  312. return Xjson_object_put((struct json_object_base *)jso);
  313. /* Avoid invalid free and crash explicitly instead of (silently)
  314. * segfaulting.
  315. */
  316. assert(jso->_ref_count > 0);
  317. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  318. /* Note: this only allow the refcount to remain correct
  319. * when multiple threads are adjusting it. It is still an error
  320. * for a thread to decrement the refcount if it doesn't "own" it,
  321. * as that can result in the thread that loses the race to 0
  322. * operating on an already-freed object.
  323. */
  324. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
  325. return 0;
  326. #else
  327. if (--jso->_ref_count > 0)
  328. return 0;
  329. #endif
  330. if (jso->_user_delete)
  331. jso->_user_delete(jso, jso->_userdata);
  332. jso->_delete(jso);
  333. return 1;
  334. }
  335. /* generic object construction and destruction parts */
  336. static void json_object_generic_delete(struct json_object *jso)
  337. {
  338. Xjson_object_generic_delete((void *)jso);
  339. }
  340. static void Xjson_object_generic_delete(struct json_object_base *jso)
  341. {
  342. #ifdef REFCOUNT_DEBUG
  343. MC_DEBUG("json_object_delete_%s: %p\n", json_type_to_name(jso->o_type), jso);
  344. lh_table_delete(json_object_table, jso);
  345. #endif /* REFCOUNT_DEBUG */
  346. printbuf_free(jso->_pb);
  347. free(jso);
  348. }
  349. // XAX remove this once all is using new api
  350. static inline struct json_object *json_object_new(enum json_type o_type)
  351. {
  352. struct json_object *jso;
  353. jso = (struct json_object *)calloc(1, sizeof(struct json_object));
  354. if (!jso)
  355. return NULL;
  356. jso->o_type = o_type;
  357. jso->_ref_count = 1;
  358. jso->_delete = &json_object_generic_delete;
  359. #ifdef REFCOUNT_DEBUG
  360. lh_table_insert(json_object_table, jso, jso);
  361. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  362. #endif /* REFCOUNT_DEBUG */
  363. return jso;
  364. }
  365. static inline struct json_object_base *Xjson_object_new(enum json_type o_type,
  366. size_t alloc_size,
  367. json_object_to_json_string_fn *to_json_string,
  368. json_object_private_delete_fn *delete_fn)
  369. {
  370. struct json_object_base *jso;
  371. jso = (struct json_object_base *)malloc(alloc_size);
  372. if (!jso)
  373. return NULL;
  374. jso->newold = 1; // XAX cut this after conversion
  375. jso->o_type = o_type;
  376. jso->_ref_count = 1;
  377. jso->_delete = delete_fn;
  378. jso->_to_json_string = to_json_string;
  379. jso->_pb = NULL;
  380. jso->_user_delete = NULL;
  381. jso->_userdata= NULL;
  382. //jso->... // Type-specific fields must be set by caller
  383. #ifdef REFCOUNT_DEBUG
  384. lh_table_insert(json_object_table, jso, jso);
  385. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  386. #endif /* REFCOUNT_DEBUG */
  387. return jso;
  388. }
  389. /* type checking functions */
  390. int json_object_is_type(const struct json_object *jso, enum json_type type)
  391. {
  392. #define jso ((const struct json_object_base *)jso)
  393. if (!jso)
  394. return (type == json_type_null);
  395. return (jso->o_type == type);
  396. #undef jso
  397. }
  398. enum json_type json_object_get_type(const struct json_object *jso)
  399. {
  400. #define jso ((const struct json_object_base *)jso)
  401. if (!jso)
  402. return json_type_null;
  403. return jso->o_type;
  404. #undef jso
  405. }
  406. void *json_object_get_userdata(json_object *jso)
  407. {
  408. if (!jso->newold)
  409. return jso ? jso->_userdata : NULL;
  410. #define jso ((const struct json_object_base *)jso)
  411. return jso ? jso->_userdata : NULL;
  412. #undef jso
  413. }
  414. static void Xjson_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
  415. {
  416. // Can't return failure, so abort if we can't perform the operation.
  417. assert(jso != NULL);
  418. // First, clean up any previously existing user info
  419. if (jso->_user_delete)
  420. jso->_user_delete(jso, jso->_userdata);
  421. jso->_userdata = userdata;
  422. jso->_user_delete = user_delete;
  423. }
  424. void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
  425. {
  426. // XAX call old code:
  427. if (!jso->newold)
  428. {
  429. Xjson_object_set_userdata(jso, userdata, user_delete);
  430. return;
  431. }
  432. #define jso ((struct json_object_base *)jso)
  433. // Can't return failure, so abort if we can't perform the operation.
  434. assert(jso != NULL);
  435. // First, clean up any previously existing user info
  436. if (jso->_user_delete)
  437. jso->_user_delete(PUBLIC(jso), jso->_userdata);
  438. jso->_userdata = userdata;
  439. jso->_user_delete = user_delete;
  440. #undef jso
  441. }
  442. /* set a custom conversion to string */
  443. static void Xjson_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
  444. void *userdata, json_object_delete_fn *user_delete)
  445. {
  446. json_object_set_userdata(jso, userdata, user_delete);
  447. if (to_string_func == NULL)
  448. {
  449. // Reset to the standard serialization function
  450. switch (jso->o_type)
  451. {
  452. case json_type_null: jso->_to_json_string = NULL; break;
  453. case json_type_boolean:
  454. jso->_to_json_string = &json_object_boolean_to_json_string;
  455. break;
  456. case json_type_double:
  457. jso->_to_json_string = &json_object_double_to_json_string_default;
  458. break;
  459. case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
  460. case json_type_object:
  461. jso->_to_json_string = &json_object_object_to_json_string;
  462. break;
  463. case json_type_array:
  464. jso->_to_json_string = &json_object_array_to_json_string;
  465. break;
  466. case json_type_string:
  467. jso->_to_json_string = &json_object_string_to_json_string;
  468. break;
  469. }
  470. return;
  471. }
  472. jso->_to_json_string = to_string_func;
  473. }
  474. void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
  475. void *userdata, json_object_delete_fn *user_delete)
  476. {
  477. // XAX call old code, remove after conversion:
  478. if (jso && !jso->newold)
  479. {
  480. Xjson_object_set_serializer(jso, to_string_func, userdata, user_delete);
  481. return;
  482. }
  483. #define jso ((struct json_object_base *)jso)
  484. json_object_set_userdata(PUBLIC(jso), userdata, user_delete);
  485. if (to_string_func == NULL)
  486. {
  487. // Reset to the standard serialization function
  488. switch (jso->o_type)
  489. {
  490. case json_type_null: jso->_to_json_string = NULL; break;
  491. case json_type_boolean:
  492. jso->_to_json_string = &json_object_boolean_to_json_string;
  493. break;
  494. case json_type_double:
  495. jso->_to_json_string = &json_object_double_to_json_string_default;
  496. break;
  497. case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
  498. case json_type_object:
  499. jso->_to_json_string = &json_object_object_to_json_string;
  500. break;
  501. case json_type_array:
  502. jso->_to_json_string = &json_object_array_to_json_string;
  503. break;
  504. case json_type_string:
  505. jso->_to_json_string = &json_object_string_to_json_string;
  506. break;
  507. }
  508. return;
  509. }
  510. jso->_to_json_string = to_string_func;
  511. #undef jso
  512. }
  513. /* extended conversion to string */
  514. const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  515. {
  516. #define jso ((struct json_object_base *)jso)
  517. const char *r = NULL;
  518. size_t s = 0;
  519. if (!jso)
  520. {
  521. s = 4;
  522. r = "null";
  523. }
  524. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  525. {
  526. printbuf_reset(jso->_pb);
  527. if (jso->_to_json_string(PUBLIC(jso), jso->_pb, 0, flags) >= 0)
  528. {
  529. s = (size_t)jso->_pb->bpos;
  530. r = jso->_pb->buf;
  531. }
  532. }
  533. if (length)
  534. *length = s;
  535. return r;
  536. #undef jso
  537. }
  538. const char *json_object_to_json_string_ext(struct json_object *jso, int flags)
  539. {
  540. return json_object_to_json_string_length(jso, flags, NULL);
  541. }
  542. /* backwards-compatible conversion to string */
  543. const char *json_object_to_json_string(struct json_object *jso)
  544. {
  545. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  546. }
  547. static void indent(struct printbuf *pb, int level, int flags)
  548. {
  549. if (flags & JSON_C_TO_STRING_PRETTY)
  550. {
  551. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  552. {
  553. printbuf_memset(pb, -1, '\t', level);
  554. }
  555. else
  556. {
  557. printbuf_memset(pb, -1, ' ', level * 2);
  558. }
  559. }
  560. }
  561. /* json_object_object */
  562. static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb,
  563. int level, int flags)
  564. {
  565. #define jso ((struct json_object_base *)jso)
  566. int had_children = 0;
  567. struct json_object_iter iter;
  568. printbuf_strappend(pb, "{" /*}*/);
  569. if (flags & JSON_C_TO_STRING_PRETTY)
  570. printbuf_strappend(pb, "\n");
  571. json_object_object_foreachC(jso, iter)
  572. {
  573. if (had_children)
  574. {
  575. printbuf_strappend(pb, ",");
  576. if (flags & JSON_C_TO_STRING_PRETTY)
  577. printbuf_strappend(pb, "\n");
  578. }
  579. had_children = 1;
  580. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  581. printbuf_strappend(pb, " ");
  582. indent(pb, level + 1, flags);
  583. printbuf_strappend(pb, "\"");
  584. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  585. if (flags & JSON_C_TO_STRING_SPACED)
  586. printbuf_strappend(pb, "\": ");
  587. else
  588. printbuf_strappend(pb, "\":");
  589. if (iter.val == NULL)
  590. printbuf_strappend(pb, "null");
  591. else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
  592. return -1;
  593. }
  594. if (flags & JSON_C_TO_STRING_PRETTY)
  595. {
  596. if (had_children)
  597. printbuf_strappend(pb, "\n");
  598. indent(pb, level, flags);
  599. }
  600. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  601. return printbuf_strappend(pb, /*{*/ " }");
  602. else
  603. return printbuf_strappend(pb, /*{*/ "}");
  604. #undef jso
  605. }
  606. static void json_object_lh_entry_free(struct lh_entry *ent)
  607. {
  608. if (!ent->k_is_constant)
  609. free(lh_entry_k(ent));
  610. json_object_put((struct json_object *)lh_entry_v(ent));
  611. }
  612. static void json_object_object_delete(struct json_object_base *jso_base)
  613. {
  614. lh_table_free(JC_OBJECT(jso_base)->c_object);
  615. Xjson_object_generic_delete(jso_base);
  616. }
  617. struct json_object *json_object_new_object(void)
  618. {
  619. struct json_object_object *jso = JSON_OBJECT_NEW(object, &json_object_object_delete);
  620. if (!jso)
  621. return NULL;
  622. jso->c_object =
  623. lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
  624. if (!jso->c_object)
  625. {
  626. Xjson_object_generic_delete(&jso->base);
  627. errno = ENOMEM;
  628. return NULL;
  629. }
  630. return PUBLIC(&jso->base);
  631. }
  632. struct lh_table *json_object_get_object(const struct json_object *jso)
  633. {
  634. #define jso ((const struct json_object_base *)jso)
  635. if (!jso)
  636. return NULL;
  637. switch (jso->o_type)
  638. {
  639. case json_type_object: return JC_OBJECT_C(jso)->c_object;
  640. default: return NULL;
  641. }
  642. #undef jso
  643. }
  644. int json_object_object_add_ex(struct json_object *jso, const char *const key,
  645. struct json_object *const val, const unsigned opts)
  646. {
  647. #define jso ((struct json_object_base *)jso)
  648. #define val ((struct json_object_base *)val)
  649. struct json_object *existing_value = NULL;
  650. struct lh_entry *existing_entry;
  651. unsigned long hash;
  652. assert(json_object_get_type(PUBLIC(jso)) == json_type_object);
  653. // We lookup the entry and replace the value, rather than just deleting
  654. // and re-adding it, so the existing key remains valid.
  655. hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
  656. existing_entry =
  657. (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
  658. ? NULL
  659. : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
  660. // The caller must avoid creating loops in the object tree, but do a
  661. // quick check anyway to make sure we're not creating a trivial loop.
  662. if (jso == val)
  663. return -1;
  664. if (!existing_entry)
  665. {
  666. const void *const k =
  667. (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? (const void *)key : strdup(key);
  668. if (k == NULL)
  669. return -1;
  670. return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
  671. }
  672. existing_value = (json_object *)lh_entry_v(existing_entry);
  673. if (existing_value)
  674. json_object_put(existing_value);
  675. existing_entry->v = val;
  676. return 0;
  677. #undef jso
  678. #undef val
  679. }
  680. int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
  681. {
  682. return json_object_object_add_ex(jso, key, val, 0);
  683. }
  684. int json_object_object_length(const struct json_object *jso)
  685. {
  686. #define jso ((const struct json_object_base *)jso)
  687. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_object);
  688. return lh_table_length(JC_OBJECT_C(jso)->c_object);
  689. #undef jso
  690. }
  691. size_t json_c_object_sizeof(void)
  692. {
  693. return sizeof(struct json_object);
  694. }
  695. struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
  696. {
  697. struct json_object *result = NULL;
  698. json_object_object_get_ex(jso, key, &result);
  699. return result;
  700. }
  701. json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
  702. struct json_object **value)
  703. {
  704. #define jso ((const struct json_object_base *)jso)
  705. if (value != NULL)
  706. *value = NULL;
  707. if (NULL == jso)
  708. return 0;
  709. switch (jso->o_type)
  710. {
  711. case json_type_object:
  712. return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key, (void **)value);
  713. default:
  714. if (value != NULL)
  715. *value = NULL;
  716. return 0;
  717. }
  718. #undef jso
  719. }
  720. void json_object_object_del(struct json_object *jso, const char *key)
  721. {
  722. #define jso ((struct json_object_base *)jso)
  723. assert(json_object_get_type(PUBLIC(jso)) == json_type_object);
  724. lh_table_delete(JC_OBJECT(jso)->c_object, key);
  725. #undef jso
  726. }
  727. /* json_object_boolean */
  728. static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
  729. int level, int flags)
  730. {
  731. #define jso ((struct json_object_base *)jso)
  732. if (JC_BOOL(jso)->c_boolean)
  733. return printbuf_strappend(pb, "true");
  734. return printbuf_strappend(pb, "false");
  735. #undef jso
  736. }
  737. struct json_object *json_object_new_boolean(json_bool b)
  738. {
  739. struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean, &json_object_generic_delete);
  740. if (!jso)
  741. return NULL;
  742. jso->c_boolean = b;
  743. return PUBLIC(&jso->base);
  744. }
  745. json_bool json_object_get_boolean(const struct json_object *jso)
  746. {
  747. if (!jso)
  748. return 0;
  749. switch (jso->o_type)
  750. {
  751. #define jso ((const struct json_object_base *)jso)
  752. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  753. case json_type_int:
  754. switch (JC_INT_C(jso)->cint_type)
  755. {
  756. case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0);
  757. case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0);
  758. default: json_abort("invalid cint_type");
  759. }
  760. case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0);
  761. case json_type_string: return (JC_STRING_C(jso)->len != 0);
  762. default: return 0;
  763. }
  764. #undef jso
  765. }
  766. int json_object_set_boolean(struct json_object *jso, json_bool new_value)
  767. {
  768. #define jso ((struct json_object_base *)jso)
  769. if (!jso || jso->o_type != json_type_boolean)
  770. return 0;
  771. JC_BOOL(jso)->c_boolean = new_value;
  772. return 1;
  773. #undef jso
  774. }
  775. /* json_object_int */
  776. static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  777. int flags)
  778. {
  779. #define jso ((struct json_object_base *)jso)
  780. /* room for 19 digits, the sign char, and a null term */
  781. char sbuf[21];
  782. if (JC_INT(jso)->cint_type == json_object_int_type_int64)
  783. snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64);
  784. else
  785. snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64);
  786. return printbuf_memappend(pb, sbuf, strlen(sbuf));
  787. #undef jso
  788. }
  789. struct json_object *json_object_new_int(int32_t i)
  790. {
  791. return json_object_new_int64(i);
  792. }
  793. int32_t json_object_get_int(const struct json_object *jso)
  794. {
  795. #define jso ((const struct json_object_base *)jso)
  796. int64_t cint64;
  797. double cdouble;
  798. enum json_type o_type;
  799. if (!jso)
  800. return 0;
  801. o_type = jso->o_type;
  802. if (o_type == json_type_int)
  803. {
  804. const struct json_object_int *jsoint = JC_INT_C(jso);
  805. if (jsoint->cint_type == json_object_int_type_int64)
  806. {
  807. cint64 = jsoint->cint.c_int64;
  808. }
  809. else
  810. {
  811. if (jsoint->cint.c_uint64 >= INT64_MAX)
  812. cint64 = INT64_MAX;
  813. else
  814. cint64 = (int64_t)jsoint->cint.c_uint64;
  815. }
  816. }
  817. else if (o_type == json_type_string)
  818. {
  819. /*
  820. * Parse strings into 64-bit numbers, then use the
  821. * 64-to-32-bit number handling below.
  822. */
  823. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  824. return 0; /* whoops, it didn't work. */
  825. o_type = json_type_int;
  826. }
  827. switch (o_type)
  828. {
  829. case json_type_int:
  830. /* Make sure we return the correct values for out of range numbers. */
  831. if (cint64 <= INT32_MIN)
  832. return INT32_MIN;
  833. if (cint64 >= INT32_MAX)
  834. return INT32_MAX;
  835. return (int32_t)cint64;
  836. case json_type_double:
  837. cdouble = JC_DOUBLE_C(jso)->c_double;
  838. if (cdouble <= INT32_MIN)
  839. return INT32_MIN;
  840. if (cdouble >= INT32_MAX)
  841. return INT32_MAX;
  842. return (int32_t)cdouble;
  843. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  844. default: return 0;
  845. }
  846. #undef jso
  847. }
  848. int json_object_set_int(struct json_object *jso, int new_value)
  849. {
  850. return json_object_set_int64(jso, (int64_t)new_value);
  851. }
  852. struct json_object *json_object_new_int64(int64_t i)
  853. {
  854. struct json_object_int *jso = JSON_OBJECT_NEW(int, &json_object_generic_delete);
  855. if (!jso)
  856. return NULL;
  857. jso->cint.c_int64 = i;
  858. jso->cint_type = json_object_int_type_int64;
  859. return PUBLIC(&jso->base);
  860. }
  861. struct json_object *json_object_new_uint64(uint64_t i)
  862. {
  863. struct json_object_int *jso = JSON_OBJECT_NEW(int, &json_object_generic_delete);
  864. if (!jso)
  865. return NULL;
  866. jso->cint.c_uint64 = i;
  867. jso->cint_type = json_object_int_type_uint64;
  868. return PUBLIC(&jso->base);
  869. }
  870. int64_t json_object_get_int64(const struct json_object *jso)
  871. {
  872. #define jso ((const struct json_object_base *)jso)
  873. int64_t cint;
  874. if (!jso)
  875. return 0;
  876. switch (jso->o_type)
  877. {
  878. case json_type_int:
  879. {
  880. const struct json_object_int *jsoint = JC_INT_C(jso);
  881. switch (jsoint->cint_type)
  882. {
  883. case json_object_int_type_int64: return jsoint->cint.c_int64;
  884. case json_object_int_type_uint64:
  885. if (jsoint->cint.c_uint64 >= INT64_MAX)
  886. return INT64_MAX;
  887. return (int64_t)jsoint->cint.c_uint64;
  888. default: json_abort("invalid cint_type");
  889. }
  890. }
  891. case json_type_double:
  892. // INT64_MAX can't be exactly represented as a double
  893. // so cast to tell the compiler it's ok to round up.
  894. if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX)
  895. return INT64_MAX;
  896. if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN)
  897. return INT64_MIN;
  898. return (int64_t)JC_DOUBLE_C(jso)->c_double;
  899. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  900. case json_type_string:
  901. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  902. return cint;
  903. /* FALLTHRU */
  904. default: return 0;
  905. }
  906. #undef jso
  907. }
  908. uint64_t json_object_get_uint64(const struct json_object *jso)
  909. {
  910. #define jso ((const struct json_object_base *)jso)
  911. uint64_t cuint;
  912. if (!jso)
  913. return 0;
  914. switch (jso->o_type)
  915. {
  916. case json_type_int:
  917. {
  918. const struct json_object_int *jsoint = JC_INT_C(jso);
  919. switch (jsoint->cint_type)
  920. {
  921. case json_object_int_type_int64:
  922. if (jsoint->cint.c_int64 < 0)
  923. return 0;
  924. return (uint64_t)jsoint->cint.c_int64;
  925. case json_object_int_type_uint64: return jsoint->cint.c_uint64;
  926. default: json_abort("invalid cint_type");
  927. }
  928. }
  929. case json_type_double:
  930. // UINT64_MAX can't be exactly represented as a double
  931. // so cast to tell the compiler it's ok to round up.
  932. if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX)
  933. return UINT64_MAX;
  934. if (JC_DOUBLE_C(jso)->c_double < 0)
  935. return 0;
  936. return (uint64_t)JC_DOUBLE_C(jso)->c_double;
  937. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  938. case json_type_string:
  939. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  940. return cuint;
  941. /* FALLTHRU */
  942. default: return 0;
  943. }
  944. #undef jso
  945. }
  946. int json_object_set_int64(struct json_object *jso, int64_t new_value)
  947. {
  948. #define jso ((struct json_object_base *)jso)
  949. if (!jso || jso->o_type != json_type_int)
  950. return 0;
  951. JC_INT(jso)->cint.c_int64 = new_value;
  952. JC_INT(jso)->cint_type = json_object_int_type_int64;
  953. return 1;
  954. #undef jso
  955. }
  956. int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
  957. {
  958. #define jso ((struct json_object_base *)jso)
  959. if (!jso || jso->o_type != json_type_int)
  960. return 0;
  961. JC_INT(jso)->cint.c_uint64 = new_value;
  962. JC_INT(jso)->cint_type = json_object_int_type_uint64;
  963. return 1;
  964. #undef jso
  965. }
  966. int json_object_int_inc(struct json_object *jso, int64_t val)
  967. {
  968. #define jso ((struct json_object_base *)jso)
  969. struct json_object_int *jsoint;
  970. if (!jso || jso->o_type != json_type_int)
  971. return 0;
  972. jsoint = JC_INT(jso);
  973. switch (jsoint->cint_type)
  974. {
  975. case json_object_int_type_int64:
  976. if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val)
  977. {
  978. jsoint->cint.c_uint64 =
  979. (uint64_t)jsoint->cint.c_int64 + (uint64_t)val;
  980. jsoint->cint_type = json_object_int_type_uint64;
  981. }
  982. else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val)
  983. {
  984. jsoint->cint.c_int64 = INT64_MIN;
  985. }
  986. else
  987. {
  988. jsoint->cint.c_int64 += val;
  989. }
  990. return 1;
  991. case json_object_int_type_uint64:
  992. if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val)
  993. {
  994. jsoint->cint.c_uint64 = UINT64_MAX;
  995. }
  996. else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val))
  997. {
  998. jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val;
  999. jsoint->cint_type = json_object_int_type_int64;
  1000. }
  1001. else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val))
  1002. {
  1003. jsoint->cint.c_uint64 -= (uint64_t)(-val);
  1004. }
  1005. else
  1006. {
  1007. jsoint->cint.c_uint64 += val;
  1008. }
  1009. return 1;
  1010. default: json_abort("invalid cint_type");
  1011. }
  1012. #undef jso
  1013. }
  1014. /* json_object_double */
  1015. #if defined(HAVE___THREAD)
  1016. // i.e. __thread or __declspec(thread)
  1017. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  1018. #endif
  1019. static char *global_serialization_float_format = NULL;
  1020. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  1021. {
  1022. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  1023. {
  1024. #if defined(HAVE___THREAD)
  1025. if (tls_serialization_float_format)
  1026. {
  1027. free(tls_serialization_float_format);
  1028. tls_serialization_float_format = NULL;
  1029. }
  1030. #endif
  1031. if (global_serialization_float_format)
  1032. free(global_serialization_float_format);
  1033. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  1034. }
  1035. else if (global_or_thread == JSON_C_OPTION_THREAD)
  1036. {
  1037. #if defined(HAVE___THREAD)
  1038. if (tls_serialization_float_format)
  1039. {
  1040. free(tls_serialization_float_format);
  1041. tls_serialization_float_format = NULL;
  1042. }
  1043. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  1044. #else
  1045. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  1046. return -1;
  1047. #endif
  1048. }
  1049. else
  1050. {
  1051. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n",
  1052. global_or_thread);
  1053. return -1;
  1054. }
  1055. return 0;
  1056. }
  1057. static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
  1058. int level, int flags, const char *format)
  1059. {
  1060. #define jso ((struct json_object_base *)jso)
  1061. struct json_object_double *jsodbl = JC_DOUBLE(jso);
  1062. char buf[128], *p, *q;
  1063. int size;
  1064. /* Although JSON RFC does not support
  1065. * NaN or Infinity as numeric values
  1066. * ECMA 262 section 9.8.1 defines
  1067. * how to handle these cases as strings
  1068. */
  1069. if (isnan(jsodbl->c_double))
  1070. {
  1071. size = snprintf(buf, sizeof(buf), "NaN");
  1072. }
  1073. else if (isinf(jsodbl->c_double))
  1074. {
  1075. if (jsodbl->c_double > 0)
  1076. size = snprintf(buf, sizeof(buf), "Infinity");
  1077. else
  1078. size = snprintf(buf, sizeof(buf), "-Infinity");
  1079. }
  1080. else
  1081. {
  1082. const char *std_format = "%.17g";
  1083. int format_drops_decimals = 0;
  1084. int looks_numeric = 0;
  1085. if (!format)
  1086. {
  1087. #if defined(HAVE___THREAD)
  1088. if (tls_serialization_float_format)
  1089. format = tls_serialization_float_format;
  1090. else
  1091. #endif
  1092. if (global_serialization_float_format)
  1093. format = global_serialization_float_format;
  1094. else
  1095. format = std_format;
  1096. }
  1097. size = snprintf(buf, sizeof(buf), format, jsodbl->c_double);
  1098. if (size < 0)
  1099. return -1;
  1100. p = strchr(buf, ',');
  1101. if (p)
  1102. *p = '.';
  1103. else
  1104. p = strchr(buf, '.');
  1105. if (format == std_format || strstr(format, ".0f") == NULL)
  1106. format_drops_decimals = 1;
  1107. looks_numeric = /* Looks like *some* kind of number */
  1108. isdigit((unsigned char)buf[0]) ||
  1109. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  1110. if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
  1111. strchr(buf, 'e') == NULL && /* Not scientific notation */
  1112. format_drops_decimals)
  1113. {
  1114. // Ensure it looks like a float, even if snprintf didn't,
  1115. // unless a custom format is set to omit the decimal.
  1116. strcat(buf, ".0");
  1117. size += 2;
  1118. }
  1119. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  1120. {
  1121. /* last useful digit, always keep 1 zero */
  1122. p++;
  1123. for (q = p; *q; q++)
  1124. {
  1125. if (*q != '0')
  1126. p = q;
  1127. }
  1128. /* drop trailing zeroes */
  1129. if (*p != 0)
  1130. *(++p) = 0;
  1131. size = p - buf;
  1132. }
  1133. }
  1134. // although unlikely, snprintf can fail
  1135. if (size < 0)
  1136. return -1;
  1137. if (size >= (int)sizeof(buf))
  1138. // The standard formats are guaranteed not to overrun the buffer,
  1139. // but if a custom one happens to do so, just silently truncate.
  1140. size = sizeof(buf) - 1;
  1141. printbuf_memappend(pb, buf, size);
  1142. return size;
  1143. #undef jso
  1144. }
  1145. static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
  1146. int level, int flags)
  1147. {
  1148. #define jso ((struct json_object_base *)jso)
  1149. return json_object_double_to_json_string_format(PUBLIC(jso), pb, level, flags, NULL);
  1150. #undef jso
  1151. }
  1152. int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1153. int flags)
  1154. {
  1155. #define jso ((struct json_object_base *)jso)
  1156. return json_object_double_to_json_string_format(PUBLIC(jso), pb, level, flags,
  1157. (const char *)jso->_userdata);
  1158. #undef jso
  1159. }
  1160. struct json_object *json_object_new_double(double d)
  1161. {
  1162. struct json_object_double *jso = JSON_OBJECT_NEW(double, &json_object_generic_delete);
  1163. if (!jso)
  1164. return NULL;
  1165. jso->base._to_json_string = &json_object_double_to_json_string_default;
  1166. jso->c_double = d;
  1167. return PUBLIC(&jso->base);
  1168. }
  1169. struct json_object *json_object_new_double_s(double d, const char *ds)
  1170. {
  1171. char *new_ds;
  1172. struct json_object *jso = json_object_new_double(d);
  1173. if (!jso)
  1174. return NULL;
  1175. new_ds = strdup(ds);
  1176. if (!new_ds)
  1177. {
  1178. json_object_generic_delete(jso);
  1179. errno = ENOMEM;
  1180. return NULL;
  1181. }
  1182. json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
  1183. json_object_free_userdata);
  1184. return jso;
  1185. }
  1186. /*
  1187. * A wrapper around json_object_userdata_to_json_string() used only
  1188. * by json_object_new_double_s() just so json_object_set_double() can
  1189. * detect when it needs to reset the serializer to the default.
  1190. */
  1191. static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
  1192. int level, int flags)
  1193. {
  1194. return json_object_userdata_to_json_string(jso, pb, level, flags);
  1195. }
  1196. int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1197. int flags)
  1198. {
  1199. // XAX old code compat, remove this
  1200. if (!jso->newold)
  1201. {
  1202. int userdata_len = strlen((const char *)jso->_userdata);
  1203. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  1204. return userdata_len;
  1205. }
  1206. else
  1207. {
  1208. #define jso ((const struct json_object_base *)jso)
  1209. int userdata_len = strlen((const char *)jso->_userdata);
  1210. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  1211. return userdata_len;
  1212. #undef jso
  1213. }
  1214. }
  1215. void json_object_free_userdata(struct json_object *jso, void *userdata)
  1216. {
  1217. free(userdata);
  1218. }
  1219. double json_object_get_double(const struct json_object *jso)
  1220. {
  1221. #define jso ((const struct json_object_base *)jso)
  1222. double cdouble;
  1223. char *errPtr = NULL;
  1224. if (!jso)
  1225. return 0.0;
  1226. switch (jso->o_type)
  1227. {
  1228. case json_type_double: return JC_DOUBLE_C(jso)->c_double;
  1229. case json_type_int:
  1230. switch (JC_INT_C(jso)->cint_type)
  1231. {
  1232. case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64;
  1233. case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64;
  1234. default: json_abort("invalid cint_type");
  1235. }
  1236. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  1237. case json_type_string:
  1238. errno = 0;
  1239. cdouble = strtod(get_string_component(jso), &errPtr);
  1240. /* if conversion stopped at the first character, return 0.0 */
  1241. if (errPtr == get_string_component(jso))
  1242. {
  1243. errno = EINVAL;
  1244. return 0.0;
  1245. }
  1246. /*
  1247. * Check that the conversion terminated on something sensible
  1248. *
  1249. * For example, { "pay" : 123AB } would parse as 123.
  1250. */
  1251. if (*errPtr != '\0')
  1252. {
  1253. errno = EINVAL;
  1254. return 0.0;
  1255. }
  1256. /*
  1257. * If strtod encounters a string which would exceed the
  1258. * capacity of a double, it returns +/- HUGE_VAL and sets
  1259. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  1260. * from a conversion, so we need to check errno.
  1261. *
  1262. * Underflow also sets errno to ERANGE, but it returns 0 in
  1263. * that case, which is what we will return anyway.
  1264. *
  1265. * See CERT guideline ERR30-C
  1266. */
  1267. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
  1268. cdouble = 0.0;
  1269. return cdouble;
  1270. default: errno = EINVAL; return 0.0;
  1271. }
  1272. #undef jso
  1273. }
  1274. int json_object_set_double(struct json_object *jso, double new_value)
  1275. {
  1276. #define jso ((struct json_object_base *)jso)
  1277. if (!jso || jso->o_type != json_type_double)
  1278. return 0;
  1279. JC_DOUBLE(jso)->c_double = new_value;
  1280. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  1281. json_object_set_serializer(PUBLIC(jso), NULL, NULL, NULL);
  1282. return 1;
  1283. #undef jso
  1284. }
  1285. /* json_object_string */
  1286. static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
  1287. int level, int flags)
  1288. {
  1289. #define jso ((struct json_object_base *)jso)
  1290. printbuf_strappend(pb, "\"");
  1291. ssize_t len = JC_STRING(jso)->len;
  1292. json_escape_str(pb, get_string_component(jso), len < 0 ? -len : len, flags);
  1293. printbuf_strappend(pb, "\"");
  1294. return 0;
  1295. #undef jso
  1296. }
  1297. static void json_object_string_delete(struct json_object *jso)
  1298. {
  1299. #define jso ((struct json_object_base *)jso)
  1300. if (JC_STRING(jso)->len < 0)
  1301. free(JC_STRING(jso)->c_string.pdata);
  1302. Xjson_object_generic_delete(jso);
  1303. #undef jso
  1304. }
  1305. static struct json_object *_json_object_new_string(const char *s, const size_t len)
  1306. {
  1307. size_t objsize;
  1308. struct json_object_string *jso;
  1309. /*
  1310. * Structures Actual memory layout
  1311. * ------------------- --------------------
  1312. * [json_object_string [json_object_string
  1313. * [json_object_base] [json_object_base]
  1314. * ...other fields... ...other fields...
  1315. * c_string] len
  1316. * bytes
  1317. * of
  1318. * string
  1319. * data
  1320. * \0]
  1321. */
  1322. if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1))
  1323. return NULL;
  1324. objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1;
  1325. if (len < sizeof(void *))
  1326. // We need a minimum size to support json_object_set_string() mutability
  1327. // so we can stuff a pointer into pdata :(
  1328. objsize += sizeof(void *) - len;
  1329. jso = (struct json_object_string *)Xjson_object_new(json_type_string, objsize,
  1330. &json_object_string_to_json_string, &json_object_string_delete);
  1331. if (!jso)
  1332. return NULL;
  1333. jso->len = len;
  1334. memcpy(jso->c_string.idata, s, len);
  1335. jso->c_string.idata[len] = '\0';
  1336. return PUBLIC(&jso->base);
  1337. }
  1338. struct json_object *json_object_new_string(const char *s)
  1339. {
  1340. return _json_object_new_string(s, strlen(s));
  1341. }
  1342. struct json_object *json_object_new_string_len(const char *s, const int len)
  1343. {
  1344. return _json_object_new_string(s, len);
  1345. }
  1346. const char *json_object_get_string(struct json_object *jso)
  1347. {
  1348. #define jso ((struct json_object_base *)jso)
  1349. if (!jso)
  1350. return NULL;
  1351. switch (jso->o_type)
  1352. {
  1353. case json_type_string: return get_string_component(jso);
  1354. default: return json_object_to_json_string(PUBLIC(jso));
  1355. }
  1356. #undef jso
  1357. }
  1358. int json_object_get_string_len(const struct json_object *jso)
  1359. {
  1360. #define jso ((const struct json_object_base *)jso)
  1361. ssize_t len;
  1362. if (!jso)
  1363. return 0;
  1364. switch (jso->o_type)
  1365. {
  1366. case json_type_string:
  1367. len = JC_STRING_C(jso)->len;
  1368. return (len < 0) ? -len : len;
  1369. default: return 0;
  1370. }
  1371. #undef jso
  1372. }
  1373. static int _json_object_set_string_len(json_object *jso, const char *s, size_t len)
  1374. {
  1375. #define jso ((struct json_object_base *)jso)
  1376. char *dstbuf;
  1377. ssize_t curlen;
  1378. ssize_t newlen;
  1379. if (jso == NULL || jso->o_type != json_type_string)
  1380. return 0;
  1381. if (len >= SSIZE_T_MAX - 1)
  1382. // jso->len is a signed ssize_t, so it can't hold the
  1383. // full size_t range.
  1384. return 0;
  1385. dstbuf = get_string_component_mutable(jso);
  1386. curlen = JC_STRING(jso)->len;
  1387. if (curlen < 0)
  1388. curlen = -curlen;
  1389. newlen = len;
  1390. if ((ssize_t)len > curlen)
  1391. {
  1392. // We have no way to return the new ptr from realloc(jso, newlen)
  1393. // and we have no way of knowing whether there's extra room available
  1394. // so we need to stuff a pointer in to pdata :(
  1395. dstbuf = (char *)malloc(len + 1);
  1396. if (dstbuf == NULL)
  1397. return 0;
  1398. if (JC_STRING(jso)->len < 0)
  1399. free(JC_STRING(jso)->c_string.pdata);
  1400. JC_STRING(jso)->c_string.pdata = dstbuf;
  1401. newlen = -len;
  1402. }
  1403. else if (JC_STRING(jso)->len < 0)
  1404. {
  1405. // We've got enough room in the separate allocated buffer,
  1406. // so use it as-is and continue to indicate that pdata is used.
  1407. newlen = -len;
  1408. }
  1409. memcpy(dstbuf, (const void *)s, len);
  1410. dstbuf[len] = '\0';
  1411. JC_STRING(jso)->len = newlen;
  1412. return 1;
  1413. #undef jso
  1414. }
  1415. int json_object_set_string(json_object *jso, const char *s)
  1416. {
  1417. return _json_object_set_string_len(jso, s, strlen(s));
  1418. }
  1419. int json_object_set_string_len(json_object *jso, const char *s, int len)
  1420. {
  1421. return _json_object_set_string_len(jso, s, len);
  1422. }
  1423. /* json_object_array */
  1424. static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1425. int flags)
  1426. {
  1427. #define jso ((struct json_object_base *)jso)
  1428. int had_children = 0;
  1429. size_t ii;
  1430. printbuf_strappend(pb, "[");
  1431. if (flags & JSON_C_TO_STRING_PRETTY)
  1432. printbuf_strappend(pb, "\n");
  1433. for (ii = 0; ii < json_object_array_length(PUBLIC(jso)); ii++)
  1434. {
  1435. struct json_object *val;
  1436. if (had_children)
  1437. {
  1438. printbuf_strappend(pb, ",");
  1439. if (flags & JSON_C_TO_STRING_PRETTY)
  1440. printbuf_strappend(pb, "\n");
  1441. }
  1442. had_children = 1;
  1443. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1444. printbuf_strappend(pb, " ");
  1445. indent(pb, level + 1, flags);
  1446. val = json_object_array_get_idx(PUBLIC(jso), ii);
  1447. if (val == NULL)
  1448. printbuf_strappend(pb, "null");
  1449. else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
  1450. return -1;
  1451. }
  1452. if (flags & JSON_C_TO_STRING_PRETTY)
  1453. {
  1454. if (had_children)
  1455. printbuf_strappend(pb, "\n");
  1456. indent(pb, level, flags);
  1457. }
  1458. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1459. return printbuf_strappend(pb, " ]");
  1460. return printbuf_strappend(pb, "]");
  1461. #undef jso
  1462. }
  1463. static void json_object_array_entry_free(void *data)
  1464. {
  1465. json_object_put((struct json_object *)data);
  1466. }
  1467. static void json_object_array_delete(struct json_object *jso)
  1468. {
  1469. #define jso ((struct json_object_base *)jso)
  1470. array_list_free(JC_ARRAY(jso)->c_array);
  1471. Xjson_object_generic_delete(jso);
  1472. #undef jso
  1473. }
  1474. struct json_object *json_object_new_array(void)
  1475. {
  1476. struct json_object_array *jso = JSON_OBJECT_NEW(array, &json_object_array_delete);
  1477. if (!jso)
  1478. return NULL;
  1479. jso->c_array = array_list_new(&json_object_array_entry_free);
  1480. if (jso->c_array == NULL)
  1481. {
  1482. free(jso);
  1483. return NULL;
  1484. }
  1485. return PUBLIC(&jso->base);
  1486. }
  1487. struct array_list *json_object_get_array(const struct json_object *jso)
  1488. {
  1489. #define jso ((const struct json_object_base *)jso)
  1490. if (!jso)
  1491. return NULL;
  1492. switch (jso->o_type)
  1493. {
  1494. case json_type_array: return JC_ARRAY_C(jso)->c_array;
  1495. default: return NULL;
  1496. }
  1497. #undef jso
  1498. }
  1499. void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
  1500. {
  1501. #define jso ((struct json_object_base *)jso)
  1502. assert(json_object_get_type(PUBLIC(jso)) == json_type_array);
  1503. array_list_sort(JC_ARRAY(jso)->c_array, sort_fn);
  1504. #undef jso
  1505. }
  1506. struct json_object *json_object_array_bsearch(const struct json_object *key,
  1507. const struct json_object *jso,
  1508. int (*sort_fn)(const void *, const void *))
  1509. {
  1510. #define jso ((const struct json_object_base *)jso)
  1511. struct json_object **result;
  1512. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1513. result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
  1514. JC_ARRAY_C(jso)->c_array, sort_fn);
  1515. if (!result)
  1516. return NULL;
  1517. return *result;
  1518. #undef jso
  1519. }
  1520. size_t json_object_array_length(const struct json_object *jso)
  1521. {
  1522. #define jso ((const struct json_object_base *)jso)
  1523. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1524. return array_list_length(JC_ARRAY_C(jso)->c_array);
  1525. #undef jso
  1526. }
  1527. int json_object_array_add(struct json_object *jso, struct json_object *val)
  1528. {
  1529. #define jso ((struct json_object_base *)jso)
  1530. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1531. return array_list_add(JC_ARRAY(jso)->c_array, val);
  1532. #undef jso
  1533. }
  1534. int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
  1535. {
  1536. #define jso ((struct json_object_base *)jso)
  1537. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1538. return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val);
  1539. #undef jso
  1540. }
  1541. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1542. {
  1543. #define jso ((struct json_object_base *)jso)
  1544. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1545. return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count);
  1546. #undef jso
  1547. }
  1548. struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
  1549. {
  1550. #define jso ((const struct json_object_base *)jso)
  1551. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1552. return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx);
  1553. #undef jso
  1554. }
  1555. static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
  1556. {
  1557. size_t len, i;
  1558. len = json_object_array_length(jso1);
  1559. if (len != json_object_array_length(jso2))
  1560. return 0;
  1561. for (i = 0; i < len; i++)
  1562. {
  1563. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1564. json_object_array_get_idx(jso2, i)))
  1565. return 0;
  1566. }
  1567. return 1;
  1568. }
  1569. struct json_object *json_object_new_null(void)
  1570. {
  1571. return NULL;
  1572. }
  1573. static int json_object_all_values_equal(struct json_object_base *jso1, struct json_object_base *jso2)
  1574. {
  1575. struct json_object_iter iter;
  1576. struct json_object *sub;
  1577. assert(json_object_get_type(PUBLIC(jso1)) == json_type_object);
  1578. assert(json_object_get_type(PUBLIC(jso2)) == json_type_object);
  1579. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1580. json_object_object_foreachC(jso1, iter)
  1581. {
  1582. if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key, (void **)(void *)&sub))
  1583. return 0;
  1584. if (!json_object_equal(iter.val, sub))
  1585. return 0;
  1586. }
  1587. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1588. json_object_object_foreachC(jso2, iter)
  1589. {
  1590. if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key, (void **)(void *)&sub))
  1591. return 0;
  1592. }
  1593. return 1;
  1594. }
  1595. static int Xjson_object_equal(struct json_object *jso1, struct json_object *jso2)
  1596. {
  1597. if (jso1 == jso2)
  1598. return 1;
  1599. if (!jso1 || !jso2)
  1600. return 0;
  1601. if (jso1->o_type != jso2->o_type)
  1602. return 0;
  1603. switch (jso1->o_type)
  1604. {
  1605. case json_type_boolean: assert(0); //return (jso1->o.c_boolean == jso2->o.c_boolean);
  1606. case json_type_double: assert(0); // return (jso1->o.c_double == jso2->o.c_double);
  1607. case json_type_int: assert(0);
  1608. /*
  1609. if (jso1->o.c_int.cint_type == json_object_int_type_int64)
  1610. {
  1611. if (jso2->o.c_int.cint_type == json_object_int_type_int64)
  1612. return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
  1613. if (jso1->o.c_int.cint.c_int64 < 0)
  1614. return 0;
  1615. return ((uint64_t)jso1->o.c_int.cint.c_int64 ==
  1616. jso2->o.c_int.cint.c_uint64);
  1617. }
  1618. // else jso1 is a uint64
  1619. if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
  1620. return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
  1621. if (jso2->o.c_int.cint.c_int64 < 0)
  1622. return 0;
  1623. return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
  1624. */
  1625. case json_type_string: assert(0);
  1626. /*
  1627. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1628. memcmp(get_string_component(jso1), get_string_component(jso2),
  1629. jso1->o.c_string.len) == 0);
  1630. */
  1631. case json_type_object: assert(0); //return json_object_all_values_equal(jso1, jso2);
  1632. case json_type_array: assert(0); //return json_array_equal(jso1, jso2);
  1633. case json_type_null: return 1;
  1634. };
  1635. return 0;
  1636. }
  1637. int json_object_equal(struct json_object *jso1, struct json_object *jso2)
  1638. {
  1639. if (jso1 == jso2)
  1640. return 1;
  1641. if (!jso1 || !jso2)
  1642. return 0;
  1643. // XAX cut this after conversion
  1644. if (!jso1->newold && !jso2->newold)
  1645. return Xjson_object_equal(jso1, jso2); // call old code
  1646. #define jso1 ((struct json_object_base *)jso1)
  1647. #define jso2 ((struct json_object_base *)jso2)
  1648. if (jso1->o_type != jso2->o_type)
  1649. return 0;
  1650. switch (jso1->o_type)
  1651. {
  1652. case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean);
  1653. case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double);
  1654. case json_type_int:
  1655. {
  1656. struct json_object_int *int1 = JC_INT(jso1);
  1657. struct json_object_int *int2 = JC_INT(jso2);
  1658. if (int1->cint_type == json_object_int_type_int64)
  1659. {
  1660. if (int2->cint_type == json_object_int_type_int64)
  1661. return (int1->cint.c_int64 == int2->cint.c_int64);
  1662. if (int1->cint.c_int64 < 0)
  1663. return 0;
  1664. return ((uint64_t)int1->cint.c_int64 ==
  1665. int2->cint.c_uint64);
  1666. }
  1667. // else jso1 is a uint64
  1668. if (int2->cint_type == json_object_int_type_uint64)
  1669. return (int1->cint.c_uint64 == int2->cint.c_uint64);
  1670. if (int2->cint.c_int64 < 0)
  1671. return 0;
  1672. return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64);
  1673. }
  1674. case json_type_string:
  1675. {
  1676. return (json_object_get_string_len(PUBLIC(jso1)) == json_object_get_string_len(PUBLIC(jso2)) &&
  1677. memcmp(get_string_component(jso1), get_string_component(jso2), json_object_get_string_len(PUBLIC(jso1))) == 0);
  1678. }
  1679. case json_type_object: return json_object_all_values_equal(jso1, jso2);
  1680. case json_type_array: return json_array_equal(PUBLIC(jso1), PUBLIC(jso2));
  1681. case json_type_null: return 1;
  1682. };
  1683. return 0;
  1684. #undef jso1
  1685. #undef jso2
  1686. }
  1687. // XAX remove this function after code conversion
  1688. static int Xjson_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1689. {
  1690. if (!src->_userdata && !src->_user_delete)
  1691. return 0;
  1692. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1693. dst->_to_json_string == _json_object_userdata_to_json_string)
  1694. {
  1695. dst->_userdata = strdup(src->_userdata);
  1696. }
  1697. // else if ... other supported serializers ...
  1698. else
  1699. {
  1700. _json_c_set_last_err(
  1701. "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
  1702. dst->_to_json_string);
  1703. return -1;
  1704. }
  1705. dst->_user_delete = src->_user_delete;
  1706. return 0;
  1707. }
  1708. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1709. {
  1710. if (!src->newold)
  1711. return Xjson_object_copy_serializer_data(src, dst);
  1712. #define src ((struct json_object_base *)src)
  1713. #define dst ((struct json_object_base *)dst)
  1714. if (!src->_userdata && !src->_user_delete)
  1715. return 0;
  1716. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1717. dst->_to_json_string == _json_object_userdata_to_json_string)
  1718. {
  1719. dst->_userdata = strdup(src->_userdata);
  1720. }
  1721. // else if ... other supported serializers ...
  1722. else
  1723. {
  1724. _json_c_set_last_err(
  1725. "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
  1726. dst->_to_json_string);
  1727. return -1;
  1728. }
  1729. dst->_user_delete = src->_user_delete;
  1730. return 0;
  1731. #undef src
  1732. #undef dst
  1733. }
  1734. /**
  1735. * The default shallow copy implementation. Simply creates a new object of the same
  1736. * type but does *not* copy over _userdata nor retain any custom serializer.
  1737. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1738. * implementation that is aware of how to copy them.
  1739. *
  1740. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1741. */
  1742. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
  1743. size_t index, json_object **dst)
  1744. {
  1745. switch (src->o_type)
  1746. {
  1747. #define src ((struct json_object_base *)src)
  1748. case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break;
  1749. case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break;
  1750. case json_type_int:
  1751. switch (JC_INT(src)->cint_type)
  1752. {
  1753. case json_object_int_type_int64:
  1754. *dst = json_object_new_int64(JC_INT(src)->cint.c_int64);
  1755. break;
  1756. case json_object_int_type_uint64:
  1757. *dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64);
  1758. break;
  1759. default: json_abort("invalid cint_type");
  1760. }
  1761. break;
  1762. case json_type_string: *dst = json_object_new_string(get_string_component(src)); break;
  1763. case json_type_object: *dst = json_object_new_object(); break;
  1764. case json_type_array: *dst = json_object_new_array(); break;
  1765. default: errno = EINVAL; return -1;
  1766. }
  1767. if (!*dst)
  1768. {
  1769. errno = ENOMEM;
  1770. return -1;
  1771. }
  1772. (*dst)->_to_json_string = src->_to_json_string;
  1773. // _userdata and _user_delete are copied later
  1774. return 1;
  1775. #undef src
  1776. }
  1777. /*
  1778. * The actual guts of json_object_deep_copy(), with a few additional args
  1779. * needed so we can keep track of where we are within the object tree.
  1780. *
  1781. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1782. */
  1783. static int json_object_deep_copy_recursive(struct json_object_base *src, struct json_object_base *parent,
  1784. const char *key_in_parent, size_t index_in_parent,
  1785. struct json_object_base **dst,
  1786. json_c_shallow_copy_fn *shallow_copy)
  1787. {
  1788. struct json_object_iter iter;
  1789. size_t src_array_len, ii;
  1790. int shallow_copy_rc = 0;
  1791. shallow_copy_rc = shallow_copy(PUBLIC(src), PUBLIC(parent), key_in_parent, index_in_parent, (struct json_object **)dst); // XAX remove cast too
  1792. /* -1=error, 1=object created ok, 2=userdata set */
  1793. if (shallow_copy_rc < 1)
  1794. {
  1795. errno = EINVAL;
  1796. return -1;
  1797. }
  1798. assert(*dst != NULL);
  1799. switch (src->o_type)
  1800. {
  1801. case json_type_object:
  1802. json_object_object_foreachC(src, iter)
  1803. {
  1804. struct json_object_base *jso = NULL;
  1805. /* This handles the `json_type_null` case */
  1806. if (!iter.val)
  1807. jso = NULL;
  1808. else if (json_object_deep_copy_recursive((struct json_object_base *)/*XAX removecast*/iter.val, src, iter.key, -1, &jso,
  1809. shallow_copy) < 0)
  1810. {
  1811. json_object_put(PUBLIC(jso));
  1812. return -1;
  1813. }
  1814. if (json_object_object_add(PUBLIC(*dst), iter.key, PUBLIC(jso)) < 0)
  1815. {
  1816. json_object_put(PUBLIC(jso));
  1817. return -1;
  1818. }
  1819. }
  1820. break;
  1821. case json_type_array:
  1822. src_array_len = json_object_array_length(PUBLIC(src));
  1823. for (ii = 0; ii < src_array_len; ii++)
  1824. {
  1825. struct json_object_base *jso = NULL;
  1826. struct json_object_base *jso1 = (struct json_object_base *)/*XAXremovecast*/json_object_array_get_idx(PUBLIC(src), ii);
  1827. /* This handles the `json_type_null` case */
  1828. if (!jso1)
  1829. jso = NULL;
  1830. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
  1831. shallow_copy) < 0)
  1832. {
  1833. json_object_put(PUBLIC(jso));
  1834. return -1;
  1835. }
  1836. if (json_object_array_add(PUBLIC(*dst), PUBLIC(jso)) < 0)
  1837. {
  1838. json_object_put(PUBLIC(jso));
  1839. return -1;
  1840. }
  1841. }
  1842. break;
  1843. default:
  1844. break;
  1845. /* else, nothing to do, shallow_copy already did. */
  1846. }
  1847. if (shallow_copy_rc != 2)
  1848. return json_object_copy_serializer_data(PUBLIC(src), PUBLIC(*dst));
  1849. return 0;
  1850. }
  1851. int json_object_deep_copy(struct json_object *src, struct json_object **dst,
  1852. json_c_shallow_copy_fn *shallow_copy)
  1853. {
  1854. #define src ((struct json_object_base *)src)
  1855. #define parent ((struct json_object_base *)parent)
  1856. #define dst ((struct json_object_base **)dst)
  1857. int rc;
  1858. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1859. if (!src || !dst || *dst)
  1860. {
  1861. errno = EINVAL;
  1862. return -1;
  1863. }
  1864. if (shallow_copy == NULL)
  1865. shallow_copy = json_c_shallow_copy_default;
  1866. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1867. if (rc < 0)
  1868. {
  1869. json_object_put(PUBLIC(*dst));
  1870. *dst = NULL;
  1871. }
  1872. return rc;
  1873. #undef src
  1874. #undef parent
  1875. #undef dst
  1876. }
  1877. static void json_abort(const char *message)
  1878. {
  1879. if (message != NULL)
  1880. fprintf(stderr, "json-c aborts with error: %s\n", message);
  1881. abort();
  1882. }