You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 44 kB

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