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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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. struct json_object_object *jso;
  589. jso_base = JSON_OBJECT_NEW(object, &json_object_object_delete);
  590. if (!jso_base)
  591. return NULL;
  592. jso = (struct json_object_object *)jso_base;
  593. jso->c_object =
  594. lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
  595. if (!jso->c_object)
  596. {
  597. Xjson_object_generic_delete(jso_base);
  598. errno = ENOMEM;
  599. return NULL;
  600. }
  601. return PUBLIC(jso_base);
  602. }
  603. struct lh_table *json_object_get_object(const struct json_object *jso)
  604. {
  605. #define jso ((const struct json_object_base *)jso)
  606. if (!jso)
  607. return NULL;
  608. switch (jso->o_type)
  609. {
  610. case json_type_object: return JC_OBJECT_C(jso)->c_object;
  611. default: return NULL;
  612. }
  613. #undef jso
  614. }
  615. int json_object_object_add_ex(struct json_object *jso, const char *const key,
  616. struct json_object *const val, const unsigned opts)
  617. {
  618. #define jso ((struct json_object_base *)jso)
  619. #define val ((struct json_object_base *)val)
  620. struct json_object *existing_value = NULL;
  621. struct lh_entry *existing_entry;
  622. unsigned long hash;
  623. assert(json_object_get_type(PUBLIC(jso)) == json_type_object);
  624. // We lookup the entry and replace the value, rather than just deleting
  625. // and re-adding it, so the existing key remains valid.
  626. hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
  627. existing_entry =
  628. (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
  629. ? NULL
  630. : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
  631. // The caller must avoid creating loops in the object tree, but do a
  632. // quick check anyway to make sure we're not creating a trivial loop.
  633. if (jso == val)
  634. return -1;
  635. if (!existing_entry)
  636. {
  637. const void *const k =
  638. (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? (const void *)key : strdup(key);
  639. if (k == NULL)
  640. return -1;
  641. return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
  642. }
  643. existing_value = (json_object *)lh_entry_v(existing_entry);
  644. if (existing_value)
  645. json_object_put(existing_value);
  646. existing_entry->v = val;
  647. return 0;
  648. #undef jso
  649. #undef val
  650. }
  651. int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
  652. {
  653. return json_object_object_add_ex(jso, key, val, 0);
  654. }
  655. int json_object_object_length(const struct json_object *jso)
  656. {
  657. #define jso ((const struct json_object_base *)jso)
  658. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_object);
  659. return lh_table_length(JC_OBJECT_C(jso)->c_object);
  660. #undef jso
  661. }
  662. size_t json_c_object_sizeof(void)
  663. {
  664. return sizeof(struct json_object);
  665. }
  666. struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
  667. {
  668. struct json_object *result = NULL;
  669. json_object_object_get_ex(jso, key, &result);
  670. return result;
  671. }
  672. json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
  673. struct json_object **value)
  674. {
  675. #define jso ((const struct json_object_base *)jso)
  676. if (value != NULL)
  677. *value = NULL;
  678. if (NULL == jso)
  679. return 0;
  680. switch (jso->o_type)
  681. {
  682. case json_type_object:
  683. return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key, (void **)value);
  684. default:
  685. if (value != NULL)
  686. *value = NULL;
  687. return 0;
  688. }
  689. #undef jso
  690. }
  691. void json_object_object_del(struct json_object *jso, const char *key)
  692. {
  693. #define jso ((struct json_object_base *)jso)
  694. assert(json_object_get_type(PUBLIC(jso)) == json_type_object);
  695. lh_table_delete(JC_OBJECT(jso)->c_object, key);
  696. #undef jso
  697. }
  698. /* json_object_boolean */
  699. static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
  700. int level, int flags)
  701. {
  702. #define jso ((struct json_object_base *)jso)
  703. if (JC_BOOL(jso)->c_boolean)
  704. return printbuf_strappend(pb, "true");
  705. return printbuf_strappend(pb, "false");
  706. #undef jso
  707. }
  708. struct json_object *json_object_new_boolean(json_bool b)
  709. {
  710. struct json_object_base *jso_base;
  711. struct json_object_boolean *jso;
  712. jso_base = JSON_OBJECT_NEW(boolean, &json_object_generic_delete);
  713. if (!jso_base)
  714. return NULL;
  715. jso = (struct json_object_boolean *)jso_base;
  716. jso->c_boolean = b;
  717. return PUBLIC(jso_base);
  718. }
  719. json_bool json_object_get_boolean(const struct json_object *jso)
  720. {
  721. if (!jso)
  722. return 0;
  723. switch (jso->o_type)
  724. {
  725. #define jso ((const struct json_object_base *)jso)
  726. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  727. #undef jso
  728. case json_type_int:
  729. switch (jso->o.c_int.cint_type)
  730. {
  731. case json_object_int_type_int64: return (jso->o.c_int.cint.c_int64 != 0);
  732. case json_object_int_type_uint64: return (jso->o.c_int.cint.c_uint64 != 0);
  733. default: json_abort("invalid cint_type");
  734. }
  735. case json_type_double: return (jso->o.c_double != 0);
  736. case json_type_string: return (jso->o.c_string.len != 0);
  737. default: return 0;
  738. }
  739. }
  740. int json_object_set_boolean(struct json_object *jso, json_bool new_value)
  741. {
  742. #define jso ((struct json_object_base *)jso)
  743. if (!jso || jso->o_type != json_type_boolean)
  744. return 0;
  745. JC_BOOL(jso)->c_boolean = new_value;
  746. return 1;
  747. #undef jso
  748. }
  749. // XAX ------------------------------ start unconverted code:
  750. /* json_object_int */
  751. static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  752. int flags)
  753. {
  754. /* room for 19 digits, the sign char, and a null term */
  755. char sbuf[21];
  756. if (jso->o.c_int.cint_type == json_object_int_type_int64)
  757. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
  758. else
  759. snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
  760. return printbuf_memappend(pb, sbuf, strlen(sbuf));
  761. }
  762. struct json_object *json_object_new_int(int32_t i)
  763. {
  764. struct json_object *jso = json_object_new(json_type_int);
  765. if (!jso)
  766. return NULL;
  767. jso->_to_json_string = &json_object_int_to_json_string;
  768. jso->o.c_int.cint.c_int64 = i;
  769. jso->o.c_int.cint_type = json_object_int_type_int64;
  770. return jso;
  771. }
  772. int32_t json_object_get_int(const struct json_object *jso)
  773. {
  774. int64_t cint64;
  775. enum json_type o_type;
  776. if (!jso)
  777. return 0;
  778. o_type = jso->o_type;
  779. if (jso->o.c_int.cint_type == json_object_int_type_int64)
  780. {
  781. cint64 = jso->o.c_int.cint.c_int64;
  782. }
  783. else
  784. {
  785. if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
  786. cint64 = INT64_MAX;
  787. else
  788. cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
  789. }
  790. if (o_type == json_type_string)
  791. {
  792. /*
  793. * Parse strings into 64-bit numbers, then use the
  794. * 64-to-32-bit number handling below.
  795. */
  796. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  797. return 0; /* whoops, it didn't work. */
  798. o_type = json_type_int;
  799. }
  800. switch (o_type)
  801. {
  802. case json_type_int:
  803. /* Make sure we return the correct values for out of range numbers. */
  804. if (cint64 <= INT32_MIN)
  805. return INT32_MIN;
  806. if (cint64 >= INT32_MAX)
  807. return INT32_MAX;
  808. return (int32_t)cint64;
  809. case json_type_double:
  810. if (jso->o.c_double <= INT32_MIN)
  811. return INT32_MIN;
  812. if (jso->o.c_double >= INT32_MAX)
  813. return INT32_MAX;
  814. return (int32_t)jso->o.c_double;
  815. #define jso ((const struct json_object_base *)jso)
  816. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  817. #undef jso
  818. default: return 0;
  819. }
  820. }
  821. int json_object_set_int(struct json_object *jso, int new_value)
  822. {
  823. return json_object_set_int64(jso, (int64_t)new_value);
  824. }
  825. struct json_object *json_object_new_int64(int64_t i)
  826. {
  827. struct json_object *jso = json_object_new(json_type_int);
  828. if (!jso)
  829. return NULL;
  830. jso->_to_json_string = &json_object_int_to_json_string;
  831. jso->o.c_int.cint.c_int64 = i;
  832. jso->o.c_int.cint_type = json_object_int_type_int64;
  833. return jso;
  834. }
  835. struct json_object *json_object_new_uint64(uint64_t i)
  836. {
  837. struct json_object *jso = json_object_new(json_type_int);
  838. if (!jso)
  839. return NULL;
  840. jso->_to_json_string = &json_object_int_to_json_string;
  841. jso->o.c_int.cint.c_uint64 = i;
  842. jso->o.c_int.cint_type = json_object_int_type_uint64;
  843. return jso;
  844. }
  845. int64_t json_object_get_int64(const struct json_object *jso)
  846. {
  847. int64_t cint;
  848. if (!jso)
  849. return 0;
  850. switch (jso->o_type)
  851. {
  852. case json_type_int:
  853. switch (jso->o.c_int.cint_type)
  854. {
  855. case json_object_int_type_int64: return jso->o.c_int.cint.c_int64;
  856. case json_object_int_type_uint64:
  857. if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
  858. return INT64_MAX;
  859. return (int64_t)jso->o.c_int.cint.c_uint64;
  860. default: json_abort("invalid cint_type");
  861. }
  862. case json_type_double:
  863. // INT64_MAX can't be exactly represented as a double
  864. // so cast to tell the compiler it's ok to round up.
  865. if (jso->o.c_double >= (double)INT64_MAX)
  866. return INT64_MAX;
  867. if (jso->o.c_double <= INT64_MIN)
  868. return INT64_MIN;
  869. return (int64_t)jso->o.c_double;
  870. #define jso ((const struct json_object_base *)jso)
  871. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  872. #undef jso
  873. case json_type_string:
  874. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  875. return cint;
  876. /* FALLTHRU */
  877. default: return 0;
  878. }
  879. }
  880. uint64_t json_object_get_uint64(const struct json_object *jso)
  881. {
  882. uint64_t cuint;
  883. if (!jso)
  884. return 0;
  885. switch (jso->o_type)
  886. {
  887. case json_type_int:
  888. switch (jso->o.c_int.cint_type)
  889. {
  890. case json_object_int_type_int64:
  891. if (jso->o.c_int.cint.c_int64 < 0)
  892. return 0;
  893. return (uint64_t)jso->o.c_int.cint.c_int64;
  894. case json_object_int_type_uint64: return jso->o.c_int.cint.c_uint64;
  895. default: json_abort("invalid cint_type");
  896. }
  897. case json_type_double:
  898. // UINT64_MAX can't be exactly represented as a double
  899. // so cast to tell the compiler it's ok to round up.
  900. if (jso->o.c_double >= (double)UINT64_MAX)
  901. return UINT64_MAX;
  902. if (jso->o.c_double < 0)
  903. return 0;
  904. return (uint64_t)jso->o.c_double;
  905. #define jso ((const struct json_object_base *)jso)
  906. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  907. #undef jso
  908. case json_type_string:
  909. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  910. return cuint;
  911. /* FALLTHRU */
  912. default: return 0;
  913. }
  914. }
  915. int json_object_set_int64(struct json_object *jso, int64_t new_value)
  916. {
  917. if (!jso || jso->o_type != json_type_int)
  918. return 0;
  919. jso->o.c_int.cint.c_int64 = new_value;
  920. jso->o.c_int.cint_type = json_object_int_type_int64;
  921. return 1;
  922. }
  923. int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
  924. {
  925. if (!jso || jso->o_type != json_type_int)
  926. return 0;
  927. jso->o.c_int.cint.c_uint64 = new_value;
  928. jso->o.c_int.cint_type = json_object_int_type_uint64;
  929. return 1;
  930. }
  931. int json_object_int_inc(struct json_object *jso, int64_t val)
  932. {
  933. if (!jso || jso->o_type != json_type_int)
  934. return 0;
  935. switch (jso->o.c_int.cint_type)
  936. {
  937. case json_object_int_type_int64:
  938. if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val)
  939. {
  940. jso->o.c_int.cint.c_uint64 =
  941. (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
  942. jso->o.c_int.cint_type = json_object_int_type_uint64;
  943. }
  944. else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val)
  945. {
  946. jso->o.c_int.cint.c_int64 = INT64_MIN;
  947. }
  948. else
  949. {
  950. jso->o.c_int.cint.c_int64 += val;
  951. }
  952. return 1;
  953. case json_object_int_type_uint64:
  954. if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val)
  955. {
  956. jso->o.c_int.cint.c_uint64 = UINT64_MAX;
  957. }
  958. else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val))
  959. {
  960. jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
  961. jso->o.c_int.cint_type = json_object_int_type_int64;
  962. }
  963. else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val))
  964. {
  965. jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
  966. }
  967. else
  968. {
  969. jso->o.c_int.cint.c_uint64 += val;
  970. }
  971. return 1;
  972. default: json_abort("invalid cint_type");
  973. }
  974. }
  975. /* json_object_double */
  976. #if defined(HAVE___THREAD)
  977. // i.e. __thread or __declspec(thread)
  978. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  979. #endif
  980. static char *global_serialization_float_format = NULL;
  981. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  982. {
  983. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  984. {
  985. #if defined(HAVE___THREAD)
  986. if (tls_serialization_float_format)
  987. {
  988. free(tls_serialization_float_format);
  989. tls_serialization_float_format = NULL;
  990. }
  991. #endif
  992. if (global_serialization_float_format)
  993. free(global_serialization_float_format);
  994. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  995. }
  996. else if (global_or_thread == JSON_C_OPTION_THREAD)
  997. {
  998. #if defined(HAVE___THREAD)
  999. if (tls_serialization_float_format)
  1000. {
  1001. free(tls_serialization_float_format);
  1002. tls_serialization_float_format = NULL;
  1003. }
  1004. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  1005. #else
  1006. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  1007. return -1;
  1008. #endif
  1009. }
  1010. else
  1011. {
  1012. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n",
  1013. global_or_thread);
  1014. return -1;
  1015. }
  1016. return 0;
  1017. }
  1018. static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
  1019. int level, int flags, const char *format)
  1020. {
  1021. char buf[128], *p, *q;
  1022. int size;
  1023. /* Although JSON RFC does not support
  1024. * NaN or Infinity as numeric values
  1025. * ECMA 262 section 9.8.1 defines
  1026. * how to handle these cases as strings
  1027. */
  1028. if (isnan(jso->o.c_double))
  1029. {
  1030. size = snprintf(buf, sizeof(buf), "NaN");
  1031. }
  1032. else if (isinf(jso->o.c_double))
  1033. {
  1034. if (jso->o.c_double > 0)
  1035. size = snprintf(buf, sizeof(buf), "Infinity");
  1036. else
  1037. size = snprintf(buf, sizeof(buf), "-Infinity");
  1038. }
  1039. else
  1040. {
  1041. const char *std_format = "%.17g";
  1042. int format_drops_decimals = 0;
  1043. int looks_numeric = 0;
  1044. if (!format)
  1045. {
  1046. #if defined(HAVE___THREAD)
  1047. if (tls_serialization_float_format)
  1048. format = tls_serialization_float_format;
  1049. else
  1050. #endif
  1051. if (global_serialization_float_format)
  1052. format = global_serialization_float_format;
  1053. else
  1054. format = std_format;
  1055. }
  1056. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  1057. if (size < 0)
  1058. return -1;
  1059. p = strchr(buf, ',');
  1060. if (p)
  1061. *p = '.';
  1062. else
  1063. p = strchr(buf, '.');
  1064. if (format == std_format || strstr(format, ".0f") == NULL)
  1065. format_drops_decimals = 1;
  1066. looks_numeric = /* Looks like *some* kind of number */
  1067. isdigit((unsigned char)buf[0]) ||
  1068. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  1069. if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
  1070. strchr(buf, 'e') == NULL && /* Not scientific notation */
  1071. format_drops_decimals)
  1072. {
  1073. // Ensure it looks like a float, even if snprintf didn't,
  1074. // unless a custom format is set to omit the decimal.
  1075. strcat(buf, ".0");
  1076. size += 2;
  1077. }
  1078. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  1079. {
  1080. /* last useful digit, always keep 1 zero */
  1081. p++;
  1082. for (q = p; *q; q++)
  1083. {
  1084. if (*q != '0')
  1085. p = q;
  1086. }
  1087. /* drop trailing zeroes */
  1088. if (*p != 0)
  1089. *(++p) = 0;
  1090. size = p - buf;
  1091. }
  1092. }
  1093. // although unlikely, snprintf can fail
  1094. if (size < 0)
  1095. return -1;
  1096. if (size >= (int)sizeof(buf))
  1097. // The standard formats are guaranteed not to overrun the buffer,
  1098. // but if a custom one happens to do so, just silently truncate.
  1099. size = sizeof(buf) - 1;
  1100. printbuf_memappend(pb, buf, size);
  1101. return size;
  1102. }
  1103. static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
  1104. int level, int flags)
  1105. {
  1106. return json_object_double_to_json_string_format(jso, pb, level, flags, NULL);
  1107. }
  1108. int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1109. int flags)
  1110. {
  1111. return json_object_double_to_json_string_format(jso, pb, level, flags,
  1112. (const char *)jso->_userdata);
  1113. }
  1114. struct json_object *json_object_new_double(double d)
  1115. {
  1116. struct json_object *jso = json_object_new(json_type_double);
  1117. if (!jso)
  1118. return NULL;
  1119. jso->_to_json_string = &json_object_double_to_json_string_default;
  1120. jso->o.c_double = d;
  1121. return jso;
  1122. }
  1123. struct json_object *json_object_new_double_s(double d, const char *ds)
  1124. {
  1125. char *new_ds;
  1126. struct json_object *jso = json_object_new_double(d);
  1127. if (!jso)
  1128. return NULL;
  1129. new_ds = strdup(ds);
  1130. if (!new_ds)
  1131. {
  1132. json_object_generic_delete(jso);
  1133. errno = ENOMEM;
  1134. return NULL;
  1135. }
  1136. json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
  1137. json_object_free_userdata);
  1138. return jso;
  1139. }
  1140. /*
  1141. * A wrapper around json_object_userdata_to_json_string() used only
  1142. * by json_object_new_double_s() just so json_object_set_double() can
  1143. * detect when it needs to reset the serializer to the default.
  1144. */
  1145. static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
  1146. int level, int flags)
  1147. {
  1148. return json_object_userdata_to_json_string(jso, pb, level, flags);
  1149. }
  1150. int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1151. int flags)
  1152. {
  1153. int userdata_len = strlen((const char *)jso->_userdata);
  1154. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  1155. return userdata_len;
  1156. }
  1157. void json_object_free_userdata(struct json_object *jso, void *userdata)
  1158. {
  1159. free(userdata);
  1160. }
  1161. double json_object_get_double(const struct json_object *jso)
  1162. {
  1163. double cdouble;
  1164. char *errPtr = NULL;
  1165. if (!jso)
  1166. return 0.0;
  1167. switch (jso->o_type)
  1168. {
  1169. case json_type_double: return jso->o.c_double;
  1170. case json_type_int:
  1171. switch (jso->o.c_int.cint_type)
  1172. {
  1173. case json_object_int_type_int64: return jso->o.c_int.cint.c_int64;
  1174. case json_object_int_type_uint64: return jso->o.c_int.cint.c_uint64;
  1175. default: json_abort("invalid cint_type");
  1176. }
  1177. #define jso ((const struct json_object_base *)jso)
  1178. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  1179. #undef jso
  1180. case json_type_string:
  1181. errno = 0;
  1182. cdouble = strtod(get_string_component(jso), &errPtr);
  1183. /* if conversion stopped at the first character, return 0.0 */
  1184. if (errPtr == get_string_component(jso))
  1185. {
  1186. errno = EINVAL;
  1187. return 0.0;
  1188. }
  1189. /*
  1190. * Check that the conversion terminated on something sensible
  1191. *
  1192. * For example, { "pay" : 123AB } would parse as 123.
  1193. */
  1194. if (*errPtr != '\0')
  1195. {
  1196. errno = EINVAL;
  1197. return 0.0;
  1198. }
  1199. /*
  1200. * If strtod encounters a string which would exceed the
  1201. * capacity of a double, it returns +/- HUGE_VAL and sets
  1202. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  1203. * from a conversion, so we need to check errno.
  1204. *
  1205. * Underflow also sets errno to ERANGE, but it returns 0 in
  1206. * that case, which is what we will return anyway.
  1207. *
  1208. * See CERT guideline ERR30-C
  1209. */
  1210. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
  1211. cdouble = 0.0;
  1212. return cdouble;
  1213. default: errno = EINVAL; return 0.0;
  1214. }
  1215. }
  1216. int json_object_set_double(struct json_object *jso, double new_value)
  1217. {
  1218. if (!jso || jso->o_type != json_type_double)
  1219. return 0;
  1220. jso->o.c_double = new_value;
  1221. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  1222. json_object_set_serializer(jso, NULL, NULL, NULL);
  1223. return 1;
  1224. }
  1225. /* json_object_string */
  1226. static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
  1227. int level, int flags)
  1228. {
  1229. printbuf_strappend(pb, "\"");
  1230. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  1231. printbuf_strappend(pb, "\"");
  1232. return 0;
  1233. }
  1234. static void json_object_string_delete(struct json_object *jso)
  1235. {
  1236. if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1237. free(jso->o.c_string.str.ptr);
  1238. json_object_generic_delete(jso);
  1239. }
  1240. struct json_object *json_object_new_string(const char *s)
  1241. {
  1242. struct json_object *jso = json_object_new(json_type_string);
  1243. if (!jso)
  1244. return NULL;
  1245. jso->_delete = &json_object_string_delete;
  1246. jso->_to_json_string = &json_object_string_to_json_string;
  1247. jso->o.c_string.len = strlen(s);
  1248. if (jso->o.c_string.len < LEN_DIRECT_STRING_DATA)
  1249. {
  1250. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  1251. }
  1252. else
  1253. {
  1254. jso->o.c_string.str.ptr = strdup(s);
  1255. if (!jso->o.c_string.str.ptr)
  1256. {
  1257. json_object_generic_delete(jso);
  1258. errno = ENOMEM;
  1259. return NULL;
  1260. }
  1261. }
  1262. return jso;
  1263. }
  1264. struct json_object *json_object_new_string_len(const char *s, const int len)
  1265. {
  1266. char *dstbuf;
  1267. struct json_object *jso = json_object_new(json_type_string);
  1268. if (!jso)
  1269. return NULL;
  1270. jso->_delete = &json_object_string_delete;
  1271. jso->_to_json_string = &json_object_string_to_json_string;
  1272. if (len < LEN_DIRECT_STRING_DATA)
  1273. {
  1274. dstbuf = jso->o.c_string.str.data;
  1275. }
  1276. else
  1277. {
  1278. jso->o.c_string.str.ptr = (char *)malloc(len + 1);
  1279. if (!jso->o.c_string.str.ptr)
  1280. {
  1281. json_object_generic_delete(jso);
  1282. errno = ENOMEM;
  1283. return NULL;
  1284. }
  1285. dstbuf = jso->o.c_string.str.ptr;
  1286. }
  1287. memcpy(dstbuf, (const void *)s, len);
  1288. dstbuf[len] = '\0';
  1289. jso->o.c_string.len = len;
  1290. return jso;
  1291. }
  1292. const char *json_object_get_string(struct json_object *jso)
  1293. {
  1294. if (!jso)
  1295. return NULL;
  1296. switch (jso->o_type)
  1297. {
  1298. case json_type_string: return get_string_component(jso);
  1299. default: return json_object_to_json_string(jso);
  1300. }
  1301. }
  1302. int json_object_get_string_len(const struct json_object *jso)
  1303. {
  1304. if (!jso)
  1305. return 0;
  1306. switch (jso->o_type)
  1307. {
  1308. case json_type_string: return jso->o.c_string.len;
  1309. default: return 0;
  1310. }
  1311. }
  1312. int json_object_set_string(json_object *jso, const char *s)
  1313. {
  1314. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  1315. }
  1316. int json_object_set_string_len(json_object *jso, const char *s, int len)
  1317. {
  1318. char *dstbuf;
  1319. if (jso == NULL || jso->o_type != json_type_string)
  1320. return 0;
  1321. if (len < LEN_DIRECT_STRING_DATA)
  1322. {
  1323. dstbuf = jso->o.c_string.str.data;
  1324. if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1325. free(jso->o.c_string.str.ptr);
  1326. }
  1327. else
  1328. {
  1329. dstbuf = (char *)malloc(len + 1);
  1330. if (dstbuf == NULL)
  1331. return 0;
  1332. if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1333. free(jso->o.c_string.str.ptr);
  1334. jso->o.c_string.str.ptr = dstbuf;
  1335. }
  1336. jso->o.c_string.len = len;
  1337. memcpy(dstbuf, (const void *)s, len);
  1338. dstbuf[len] = '\0';
  1339. return 1;
  1340. }
  1341. /* json_object_array */
  1342. static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1343. int flags)
  1344. {
  1345. #define jso ((struct json_object_base *)jso)
  1346. int had_children = 0;
  1347. size_t ii;
  1348. printbuf_strappend(pb, "[");
  1349. if (flags & JSON_C_TO_STRING_PRETTY)
  1350. printbuf_strappend(pb, "\n");
  1351. for (ii = 0; ii < json_object_array_length(PUBLIC(jso)); ii++)
  1352. {
  1353. struct json_object *val;
  1354. if (had_children)
  1355. {
  1356. printbuf_strappend(pb, ",");
  1357. if (flags & JSON_C_TO_STRING_PRETTY)
  1358. printbuf_strappend(pb, "\n");
  1359. }
  1360. had_children = 1;
  1361. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1362. printbuf_strappend(pb, " ");
  1363. indent(pb, level + 1, flags);
  1364. val = json_object_array_get_idx(PUBLIC(jso), ii);
  1365. if (val == NULL)
  1366. printbuf_strappend(pb, "null");
  1367. else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
  1368. return -1;
  1369. }
  1370. if (flags & JSON_C_TO_STRING_PRETTY)
  1371. {
  1372. if (had_children)
  1373. printbuf_strappend(pb, "\n");
  1374. indent(pb, level, flags);
  1375. }
  1376. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1377. return printbuf_strappend(pb, " ]");
  1378. return printbuf_strappend(pb, "]");
  1379. #undef jso
  1380. }
  1381. static void json_object_array_entry_free(void *data)
  1382. {
  1383. json_object_put((struct json_object *)data);
  1384. }
  1385. static void json_object_array_delete(struct json_object *jso)
  1386. {
  1387. #define jso ((struct json_object_base *)jso)
  1388. array_list_free(JC_ARRAY(jso)->c_array);
  1389. json_object_generic_delete(PUBLIC(jso));
  1390. #undef jso
  1391. }
  1392. struct json_object *json_object_new_array(void)
  1393. {
  1394. struct json_object_base *jso_base;
  1395. struct json_object_array *jso;
  1396. jso_base = JSON_OBJECT_NEW(array, &json_object_array_delete);
  1397. if (!jso_base)
  1398. return NULL;
  1399. jso = (struct json_object_array *)jso_base;
  1400. jso->c_array = array_list_new(&json_object_array_entry_free);
  1401. if (jso->c_array == NULL)
  1402. {
  1403. free(jso);
  1404. return NULL;
  1405. }
  1406. return PUBLIC(jso_base);
  1407. }
  1408. struct array_list *json_object_get_array(const struct json_object *jso)
  1409. {
  1410. #define jso ((const struct json_object_base *)jso)
  1411. if (!jso)
  1412. return NULL;
  1413. switch (jso->o_type)
  1414. {
  1415. case json_type_array: return JC_ARRAY_C(jso)->c_array;
  1416. default: return NULL;
  1417. }
  1418. #undef jso
  1419. }
  1420. void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
  1421. {
  1422. #define jso ((struct json_object_base *)jso)
  1423. assert(json_object_get_type(PUBLIC(jso)) == json_type_array);
  1424. array_list_sort(JC_ARRAY(jso)->c_array, sort_fn);
  1425. #undef jso
  1426. }
  1427. struct json_object *json_object_array_bsearch(const struct json_object *key,
  1428. const struct json_object *jso,
  1429. int (*sort_fn)(const void *, const void *))
  1430. {
  1431. #define jso ((const struct json_object_base *)jso)
  1432. struct json_object **result;
  1433. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1434. result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
  1435. JC_ARRAY_C(jso)->c_array, sort_fn);
  1436. if (!result)
  1437. return NULL;
  1438. return *result;
  1439. #undef jso
  1440. }
  1441. size_t json_object_array_length(const struct json_object *jso)
  1442. {
  1443. #define jso ((const struct json_object_base *)jso)
  1444. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1445. return array_list_length(JC_ARRAY_C(jso)->c_array);
  1446. #undef jso
  1447. }
  1448. int json_object_array_add(struct json_object *jso, struct json_object *val)
  1449. {
  1450. #define jso ((struct json_object_base *)jso)
  1451. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1452. return array_list_add(JC_ARRAY(jso)->c_array, val);
  1453. #undef jso
  1454. }
  1455. int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
  1456. {
  1457. #define jso ((struct json_object_base *)jso)
  1458. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1459. return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val);
  1460. #undef jso
  1461. }
  1462. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1463. {
  1464. #define jso ((struct json_object_base *)jso)
  1465. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1466. return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count);
  1467. #undef jso
  1468. }
  1469. struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
  1470. {
  1471. #define jso ((const struct json_object_base *)jso)
  1472. assert(json_object_get_type(PUBLIC_C(jso)) == json_type_array);
  1473. return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx);
  1474. #undef jso
  1475. }
  1476. static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
  1477. {
  1478. size_t len, i;
  1479. len = json_object_array_length(jso1);
  1480. if (len != json_object_array_length(jso2))
  1481. return 0;
  1482. for (i = 0; i < len; i++)
  1483. {
  1484. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1485. json_object_array_get_idx(jso2, i)))
  1486. return 0;
  1487. }
  1488. return 1;
  1489. }
  1490. struct json_object *json_object_new_null(void)
  1491. {
  1492. return NULL;
  1493. }
  1494. static int json_object_all_values_equal(struct json_object_base *jso1, struct json_object_base *jso2)
  1495. {
  1496. struct json_object_iter iter;
  1497. struct json_object *sub;
  1498. assert(json_object_get_type(PUBLIC(jso1)) == json_type_object);
  1499. assert(json_object_get_type(PUBLIC(jso2)) == json_type_object);
  1500. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1501. json_object_object_foreachC(jso1, iter)
  1502. {
  1503. struct json_object_object *jso2_object = (struct json_object_object *)jso2;
  1504. if (!lh_table_lookup_ex(jso2_object->c_object, (void *)iter.key, (void **)(void *)&sub))
  1505. return 0;
  1506. if (!json_object_equal(iter.val, sub))
  1507. return 0;
  1508. }
  1509. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1510. json_object_object_foreachC(jso2, iter)
  1511. {
  1512. struct json_object_object *jso1_object = (struct json_object_object *)jso1;
  1513. if (!lh_table_lookup_ex(jso1_object->c_object, (void *)iter.key, (void **)(void *)&sub))
  1514. return 0;
  1515. }
  1516. return 1;
  1517. }
  1518. static int Xjson_object_equal(struct json_object *jso1, struct json_object *jso2)
  1519. {
  1520. if (jso1 == jso2)
  1521. return 1;
  1522. if (!jso1 || !jso2)
  1523. return 0;
  1524. if (jso1->o_type != jso2->o_type)
  1525. return 0;
  1526. switch (jso1->o_type)
  1527. {
  1528. case json_type_boolean: assert(0); //return (jso1->o.c_boolean == jso2->o.c_boolean);
  1529. case json_type_double: return (jso1->o.c_double == jso2->o.c_double);
  1530. case json_type_int:
  1531. if (jso1->o.c_int.cint_type == json_object_int_type_int64)
  1532. {
  1533. if (jso2->o.c_int.cint_type == json_object_int_type_int64)
  1534. return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
  1535. if (jso1->o.c_int.cint.c_int64 < 0)
  1536. return 0;
  1537. return ((uint64_t)jso1->o.c_int.cint.c_int64 ==
  1538. jso2->o.c_int.cint.c_uint64);
  1539. }
  1540. // else jso1 is a uint64
  1541. if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
  1542. return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
  1543. if (jso2->o.c_int.cint.c_int64 < 0)
  1544. return 0;
  1545. return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
  1546. case json_type_string:
  1547. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1548. memcmp(get_string_component(jso1), get_string_component(jso2),
  1549. jso1->o.c_string.len) == 0);
  1550. case json_type_object: assert(0); //return json_object_all_values_equal(jso1, jso2);
  1551. case json_type_array: assert(0); //return json_array_equal(jso1, jso2);
  1552. case json_type_null: return 1;
  1553. };
  1554. return 0;
  1555. }
  1556. int json_object_equal(struct json_object *jso1, struct json_object *jso2)
  1557. {
  1558. if (jso1 == jso2)
  1559. return 1;
  1560. if (!jso1 || !jso2)
  1561. return 0;
  1562. // XAX cut this after conversion
  1563. if (!jso1->newold && !jso2->newold)
  1564. return Xjson_object_equal(jso1, jso2); // call old code
  1565. #define jso1 ((struct json_object_base *)jso1)
  1566. #define jso2 ((struct json_object_base *)jso2)
  1567. if (jso1->o_type != jso2->o_type)
  1568. return 0;
  1569. switch (jso1->o_type)
  1570. {
  1571. case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean);
  1572. case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double);
  1573. case json_type_int:
  1574. {
  1575. struct json_object_int *int1 = JC_INT(jso1);
  1576. struct json_object_int *int2 = JC_INT(jso2);
  1577. if (int1->cint_type == json_object_int_type_int64)
  1578. {
  1579. if (int2->cint_type == json_object_int_type_int64)
  1580. return (int1->cint.c_int64 == int2->cint.c_int64);
  1581. if (int1->cint.c_int64 < 0)
  1582. return 0;
  1583. return ((uint64_t)int1->cint.c_int64 ==
  1584. int2->cint.c_uint64);
  1585. }
  1586. // else jso1 is a uint64
  1587. if (int2->cint_type == json_object_int_type_uint64)
  1588. return (int1->cint.c_uint64 == int2->cint.c_uint64);
  1589. if (int2->cint.c_int64 < 0)
  1590. return 0;
  1591. return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64);
  1592. }
  1593. case json_type_string:
  1594. {
  1595. struct json_object_string *str1 = JC_STRING(jso1);
  1596. struct json_object_string *str2 = JC_STRING(jso2);
  1597. return (str1->len == str2->len &&
  1598. memcmp(str1->data, str2->data, str1->len) == 0);
  1599. }
  1600. case json_type_object: return json_object_all_values_equal(jso1, jso2);
  1601. case json_type_array: return json_array_equal(PUBLIC(jso1), PUBLIC(jso2));
  1602. case json_type_null: return 1;
  1603. };
  1604. return 0;
  1605. #undef jso1
  1606. #undef jso2
  1607. }
  1608. // XAX remove this function after code conversion
  1609. static int Xjson_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1610. {
  1611. if (!src->_userdata && !src->_user_delete)
  1612. return 0;
  1613. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1614. dst->_to_json_string == _json_object_userdata_to_json_string)
  1615. {
  1616. dst->_userdata = strdup(src->_userdata);
  1617. }
  1618. // else if ... other supported serializers ...
  1619. else
  1620. {
  1621. _json_c_set_last_err(
  1622. "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
  1623. dst->_to_json_string);
  1624. return -1;
  1625. }
  1626. dst->_user_delete = src->_user_delete;
  1627. return 0;
  1628. }
  1629. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1630. {
  1631. if (!src->newold)
  1632. return Xjson_object_copy_serializer_data(src, dst);
  1633. #define src ((struct json_object_base *)src)
  1634. #define dst ((struct json_object_base *)dst)
  1635. if (!src->_userdata && !src->_user_delete)
  1636. return 0;
  1637. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1638. dst->_to_json_string == _json_object_userdata_to_json_string)
  1639. {
  1640. dst->_userdata = strdup(src->_userdata);
  1641. }
  1642. // else if ... other supported serializers ...
  1643. else
  1644. {
  1645. _json_c_set_last_err(
  1646. "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
  1647. dst->_to_json_string);
  1648. return -1;
  1649. }
  1650. dst->_user_delete = src->_user_delete;
  1651. return 0;
  1652. #undef src
  1653. #undef dst
  1654. }
  1655. /**
  1656. * The default shallow copy implementation. Simply creates a new object of the same
  1657. * type but does *not* copy over _userdata nor retain any custom serializer.
  1658. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1659. * implementation that is aware of how to copy them.
  1660. *
  1661. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1662. */
  1663. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
  1664. size_t index, json_object **dst)
  1665. {
  1666. switch (src->o_type)
  1667. {
  1668. #define src ((struct json_object_base *)src)
  1669. case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break;
  1670. #undef src
  1671. case json_type_double: *dst = json_object_new_double(src->o.c_double); break;
  1672. case json_type_int:
  1673. switch (src->o.c_int.cint_type)
  1674. {
  1675. case json_object_int_type_int64:
  1676. *dst = json_object_new_int64(src->o.c_int.cint.c_int64);
  1677. break;
  1678. case json_object_int_type_uint64:
  1679. *dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
  1680. break;
  1681. default: json_abort("invalid cint_type");
  1682. }
  1683. break;
  1684. case json_type_string: *dst = json_object_new_string(get_string_component(src)); break;
  1685. case json_type_object: *dst = json_object_new_object(); break;
  1686. case json_type_array: *dst = json_object_new_array(); break;
  1687. default: errno = EINVAL; return -1;
  1688. }
  1689. if (!*dst)
  1690. {
  1691. errno = ENOMEM;
  1692. return -1;
  1693. }
  1694. (*dst)->_to_json_string = src->_to_json_string;
  1695. // _userdata and _user_delete are copied later
  1696. return 1;
  1697. }
  1698. /*
  1699. * The actual guts of json_object_deep_copy(), with a few additional args
  1700. * needed so we can keep track of where we are within the object tree.
  1701. *
  1702. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1703. */
  1704. static int json_object_deep_copy_recursive(struct json_object_base *src, struct json_object_base *parent,
  1705. const char *key_in_parent, size_t index_in_parent,
  1706. struct json_object_base **dst,
  1707. json_c_shallow_copy_fn *shallow_copy)
  1708. {
  1709. struct json_object_iter iter;
  1710. size_t src_array_len, ii;
  1711. int shallow_copy_rc = 0;
  1712. shallow_copy_rc = shallow_copy(PUBLIC(src), PUBLIC(parent), key_in_parent, index_in_parent, (struct json_object **)dst); // XAX remove cast too
  1713. /* -1=error, 1=object created ok, 2=userdata set */
  1714. if (shallow_copy_rc < 1)
  1715. {
  1716. errno = EINVAL;
  1717. return -1;
  1718. }
  1719. assert(*dst != NULL);
  1720. switch (src->o_type)
  1721. {
  1722. case json_type_object:
  1723. json_object_object_foreachC(src, iter)
  1724. {
  1725. struct json_object_base *jso = NULL;
  1726. /* This handles the `json_type_null` case */
  1727. if (!iter.val)
  1728. jso = NULL;
  1729. else if (json_object_deep_copy_recursive((struct json_object_base *)/*XAX removecast*/iter.val, src, iter.key, -1, &jso,
  1730. shallow_copy) < 0)
  1731. {
  1732. json_object_put(PUBLIC(jso));
  1733. return -1;
  1734. }
  1735. if (json_object_object_add(PUBLIC(*dst), iter.key, PUBLIC(jso)) < 0)
  1736. {
  1737. json_object_put(PUBLIC(jso));
  1738. return -1;
  1739. }
  1740. }
  1741. break;
  1742. case json_type_array:
  1743. src_array_len = json_object_array_length(PUBLIC(src));
  1744. for (ii = 0; ii < src_array_len; ii++)
  1745. {
  1746. struct json_object_base *jso = NULL;
  1747. struct json_object_base *jso1 = (struct json_object_base *)/*XAXremovecast*/json_object_array_get_idx(PUBLIC(src), ii);
  1748. /* This handles the `json_type_null` case */
  1749. if (!jso1)
  1750. jso = NULL;
  1751. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
  1752. shallow_copy) < 0)
  1753. {
  1754. json_object_put(PUBLIC(jso));
  1755. return -1;
  1756. }
  1757. if (json_object_array_add(PUBLIC(*dst), PUBLIC(jso)) < 0)
  1758. {
  1759. json_object_put(PUBLIC(jso));
  1760. return -1;
  1761. }
  1762. }
  1763. break;
  1764. default:
  1765. break;
  1766. /* else, nothing to do, shallow_copy already did. */
  1767. }
  1768. if (shallow_copy_rc != 2)
  1769. return json_object_copy_serializer_data(PUBLIC(src), PUBLIC(*dst));
  1770. return 0;
  1771. }
  1772. int json_object_deep_copy(struct json_object *src, struct json_object **dst,
  1773. json_c_shallow_copy_fn *shallow_copy)
  1774. {
  1775. #define src ((struct json_object_base *)src)
  1776. #define parent ((struct json_object_base *)parent)
  1777. #define dst ((struct json_object_base **)dst)
  1778. int rc;
  1779. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1780. if (!src || !dst || *dst)
  1781. {
  1782. errno = EINVAL;
  1783. return -1;
  1784. }
  1785. if (shallow_copy == NULL)
  1786. shallow_copy = json_c_shallow_copy_default;
  1787. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1788. if (rc < 0)
  1789. {
  1790. json_object_put(PUBLIC(*dst));
  1791. *dst = NULL;
  1792. }
  1793. return rc;
  1794. #undef src
  1795. #undef parent
  1796. #undef dst
  1797. }
  1798. static void json_abort(const char *message)
  1799. {
  1800. if (message != NULL)
  1801. fprintf(stderr, "json-c aborts with error: %s\n", message);
  1802. abort();
  1803. }