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

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