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