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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include "debug.h"
  21. #include "printbuf.h"
  22. #include "linkhash.h"
  23. #include "arraylist.h"
  24. #include "json_inttypes.h"
  25. #include "json_object.h"
  26. #include "json_object_private.h"
  27. #include "json_util.h"
  28. #include "math_compat.h"
  29. #include "strdup_compat.h"
  30. #include "snprintf_compat.h"
  31. #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
  32. #error "The long long type isn't 64-bits"
  33. #endif
  34. // Don't define this. It's not thread-safe.
  35. /* #define REFCOUNT_DEBUG 1 */
  36. const char *json_number_chars = "0123456789.+-eE";
  37. const char *json_hex_chars = "0123456789abcdefABCDEF";
  38. static void json_object_generic_delete(struct json_object* jso);
  39. static struct json_object* json_object_new(enum json_type o_type);
  40. static json_object_to_json_string_fn json_object_object_to_json_string;
  41. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  42. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  43. static json_object_to_json_string_fn json_object_int_to_json_string;
  44. static json_object_to_json_string_fn json_object_string_to_json_string;
  45. static json_object_to_json_string_fn json_object_array_to_json_string;
  46. /* ref count debugging */
  47. #ifdef REFCOUNT_DEBUG
  48. static struct lh_table *json_object_table;
  49. static void json_object_init(void) __attribute__ ((constructor));
  50. static void json_object_init(void) {
  51. MC_DEBUG("json_object_init: creating object table\n");
  52. json_object_table = lh_kptr_table_new(128, NULL);
  53. }
  54. static void json_object_fini(void) __attribute__ ((destructor));
  55. static void json_object_fini(void)
  56. {
  57. struct lh_entry *ent;
  58. if (MC_GET_DEBUG())
  59. {
  60. if (json_object_table->count)
  61. {
  62. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  63. json_object_table->count);
  64. lh_foreach(json_object_table, ent)
  65. {
  66. struct json_object* obj =
  67. (struct json_object*) lh_entry_v(ent);
  68. MC_DEBUG("\t%s:%p\n",
  69. json_type_to_name(obj->o_type), obj);
  70. }
  71. }
  72. }
  73. MC_DEBUG("json_object_fini: freeing object table\n");
  74. lh_table_free(json_object_table);
  75. }
  76. #endif /* REFCOUNT_DEBUG */
  77. /* helper for accessing the optimized string data component in json_object
  78. */
  79. static const char *
  80. get_string_component(const struct json_object *jso)
  81. {
  82. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  83. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  84. }
  85. /* string escaping */
  86. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  87. {
  88. int pos = 0, start_offset = 0;
  89. unsigned char c;
  90. while (len--)
  91. {
  92. c = str[pos];
  93. switch(c)
  94. {
  95. case '\b':
  96. case '\n':
  97. case '\r':
  98. case '\t':
  99. case '\f':
  100. case '"':
  101. case '\\':
  102. case '/':
  103. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  104. {
  105. pos++;
  106. break;
  107. }
  108. if(pos - start_offset > 0)
  109. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  110. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  111. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  112. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  113. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  114. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  115. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  116. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  117. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  118. start_offset = ++pos;
  119. break;
  120. default:
  121. if(c < ' ')
  122. {
  123. char sbuf[7];
  124. if(pos - start_offset > 0)
  125. printbuf_memappend(pb,
  126. str + start_offset,
  127. pos - start_offset);
  128. snprintf(sbuf, sizeof(sbuf),
  129. "\\u00%c%c",
  130. json_hex_chars[c >> 4],
  131. json_hex_chars[c & 0xf]);
  132. printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1);
  133. start_offset = ++pos;
  134. } else
  135. pos++;
  136. }
  137. }
  138. if (pos - start_offset > 0)
  139. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  140. return 0;
  141. }
  142. /* reference counting */
  143. extern struct json_object* json_object_get(struct json_object *jso)
  144. {
  145. if (!jso) return jso;
  146. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  147. __sync_add_and_fetch(&jso->_ref_count, 1);
  148. #else
  149. ++jso->_ref_count;
  150. #endif
  151. return jso;
  152. }
  153. int json_object_put(struct json_object *jso)
  154. {
  155. if(!jso) return 0;
  156. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  157. /* Note: this only allow the refcount to remain correct
  158. * when multiple threads are adjusting it. It is still an error
  159. * for a thread to decrement the refcount if it doesn't "own" it,
  160. * as that can result in the thread that loses the race to 0
  161. * operating on an already-freed object.
  162. */
  163. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
  164. #else
  165. if (--jso->_ref_count > 0) return 0;
  166. #endif
  167. if (jso->_user_delete)
  168. jso->_user_delete(jso, jso->_userdata);
  169. jso->_delete(jso);
  170. return 1;
  171. }
  172. /* generic object construction and destruction parts */
  173. static void json_object_generic_delete(struct json_object* jso)
  174. {
  175. #ifdef REFCOUNT_DEBUG
  176. MC_DEBUG("json_object_delete_%s: %p\n",
  177. json_type_to_name(jso->o_type), jso);
  178. lh_table_delete(json_object_table, jso);
  179. #endif /* REFCOUNT_DEBUG */
  180. printbuf_free(jso->_pb);
  181. free(jso);
  182. }
  183. static struct json_object* json_object_new(enum json_type o_type)
  184. {
  185. struct json_object *jso;
  186. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  187. if (!jso)
  188. return NULL;
  189. jso->o_type = o_type;
  190. jso->_ref_count = 1;
  191. jso->_delete = &json_object_generic_delete;
  192. #ifdef REFCOUNT_DEBUG
  193. lh_table_insert(json_object_table, jso, jso);
  194. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  195. #endif /* REFCOUNT_DEBUG */
  196. return jso;
  197. }
  198. /* type checking functions */
  199. int json_object_is_type(const struct json_object *jso, enum json_type type)
  200. {
  201. if (!jso)
  202. return (type == json_type_null);
  203. return (jso->o_type == type);
  204. }
  205. enum json_type json_object_get_type(const struct json_object *jso)
  206. {
  207. if (!jso)
  208. return json_type_null;
  209. return jso->o_type;
  210. }
  211. void* json_object_get_userdata(json_object *jso) {
  212. return jso ? jso->_userdata : NULL;
  213. }
  214. void json_object_set_userdata(json_object *jso, void *userdata,
  215. json_object_delete_fn *user_delete)
  216. {
  217. // Can't return failure, so abort if we can't perform the operation.
  218. assert(jso != NULL);
  219. // First, clean up any previously existing user info
  220. if (jso->_user_delete)
  221. jso->_user_delete(jso, jso->_userdata);
  222. jso->_userdata = userdata;
  223. jso->_user_delete = user_delete;
  224. }
  225. /* set a custom conversion to string */
  226. void json_object_set_serializer(json_object *jso,
  227. json_object_to_json_string_fn to_string_func,
  228. void *userdata,
  229. json_object_delete_fn *user_delete)
  230. {
  231. json_object_set_userdata(jso, userdata, user_delete);
  232. if (to_string_func == NULL)
  233. {
  234. // Reset to the standard serialization function
  235. switch(jso->o_type)
  236. {
  237. case json_type_null:
  238. jso->_to_json_string = NULL;
  239. break;
  240. case json_type_boolean:
  241. jso->_to_json_string = &json_object_boolean_to_json_string;
  242. break;
  243. case json_type_double:
  244. jso->_to_json_string = &json_object_double_to_json_string_default;
  245. break;
  246. case json_type_int:
  247. jso->_to_json_string = &json_object_int_to_json_string;
  248. break;
  249. case json_type_object:
  250. jso->_to_json_string = &json_object_object_to_json_string;
  251. break;
  252. case json_type_array:
  253. jso->_to_json_string = &json_object_array_to_json_string;
  254. break;
  255. case json_type_string:
  256. jso->_to_json_string = &json_object_string_to_json_string;
  257. break;
  258. }
  259. return;
  260. }
  261. jso->_to_json_string = to_string_func;
  262. }
  263. /* extended conversion to string */
  264. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  265. {
  266. const char *r = NULL;
  267. size_t s = 0;
  268. if (!jso)
  269. {
  270. s = 4;
  271. r = "null";
  272. }
  273. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  274. {
  275. printbuf_reset(jso->_pb);
  276. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  277. {
  278. s = (size_t)jso->_pb->bpos;
  279. r = jso->_pb->buf;
  280. }
  281. }
  282. if (length)
  283. *length = s;
  284. return r;
  285. }
  286. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  287. {
  288. return json_object_to_json_string_length(jso, flags, NULL);
  289. }
  290. /* backwards-compatible conversion to string */
  291. const char* json_object_to_json_string(struct json_object *jso)
  292. {
  293. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  294. }
  295. static void indent(struct printbuf *pb, int level, int flags)
  296. {
  297. if (flags & JSON_C_TO_STRING_PRETTY)
  298. {
  299. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  300. {
  301. printbuf_memset(pb, -1, '\t', level);
  302. }
  303. else
  304. {
  305. printbuf_memset(pb, -1, ' ', level * 2);
  306. }
  307. }
  308. }
  309. /* json_object_object */
  310. static int json_object_object_to_json_string(struct json_object* jso,
  311. struct printbuf *pb,
  312. int level,
  313. int flags)
  314. {
  315. int had_children = 0;
  316. struct json_object_iter iter;
  317. printbuf_strappend(pb, "{" /*}*/);
  318. if (flags & JSON_C_TO_STRING_PRETTY)
  319. printbuf_strappend(pb, "\n");
  320. json_object_object_foreachC(jso, iter)
  321. {
  322. if (had_children)
  323. {
  324. printbuf_strappend(pb, ",");
  325. if (flags & JSON_C_TO_STRING_PRETTY)
  326. printbuf_strappend(pb, "\n");
  327. }
  328. had_children = 1;
  329. if (flags & JSON_C_TO_STRING_SPACED)
  330. printbuf_strappend(pb, " ");
  331. indent(pb, level+1, flags);
  332. printbuf_strappend(pb, "\"");
  333. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  334. if (flags & JSON_C_TO_STRING_SPACED)
  335. printbuf_strappend(pb, "\": ");
  336. else
  337. printbuf_strappend(pb, "\":");
  338. if(iter.val == NULL)
  339. printbuf_strappend(pb, "null");
  340. else
  341. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  342. return -1;
  343. }
  344. if (flags & JSON_C_TO_STRING_PRETTY)
  345. {
  346. if (had_children)
  347. printbuf_strappend(pb, "\n");
  348. indent(pb,level,flags);
  349. }
  350. if (flags & JSON_C_TO_STRING_SPACED)
  351. return printbuf_strappend(pb, /*{*/ " }");
  352. else
  353. return printbuf_strappend(pb, /*{*/ "}");
  354. }
  355. static void json_object_lh_entry_free(struct lh_entry *ent)
  356. {
  357. if (!ent->k_is_constant)
  358. free(lh_entry_k(ent));
  359. json_object_put((struct json_object*)lh_entry_v(ent));
  360. }
  361. static void json_object_object_delete(struct json_object* jso)
  362. {
  363. lh_table_free(jso->o.c_object);
  364. json_object_generic_delete(jso);
  365. }
  366. struct json_object* json_object_new_object(void)
  367. {
  368. struct json_object *jso = json_object_new(json_type_object);
  369. if (!jso)
  370. return NULL;
  371. jso->_delete = &json_object_object_delete;
  372. jso->_to_json_string = &json_object_object_to_json_string;
  373. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  374. &json_object_lh_entry_free);
  375. if (!jso->o.c_object)
  376. {
  377. json_object_generic_delete(jso);
  378. errno = ENOMEM;
  379. return NULL;
  380. }
  381. return jso;
  382. }
  383. struct lh_table* json_object_get_object(const struct json_object *jso)
  384. {
  385. if (!jso)
  386. return NULL;
  387. switch(jso->o_type)
  388. {
  389. case json_type_object:
  390. return jso->o.c_object;
  391. default:
  392. return NULL;
  393. }
  394. }
  395. int json_object_object_add_ex(struct json_object* jso,
  396. const char *const key,
  397. struct json_object *const val,
  398. const unsigned opts)
  399. {
  400. struct json_object *existing_value = NULL;
  401. struct lh_entry *existing_entry;
  402. unsigned long hash;
  403. assert(json_object_get_type(jso) == json_type_object);
  404. // We lookup the entry and replace the value, rather than just deleting
  405. // and re-adding it, so the existing key remains valid.
  406. hash = lh_get_hash(jso->o.c_object, (const void *)key);
  407. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  408. lh_table_lookup_entry_w_hash(jso->o.c_object,
  409. (const void *)key, hash);
  410. // The caller must avoid creating loops in the object tree, but do a
  411. // quick check anyway to make sure we're not creating a trivial loop.
  412. if (jso == val)
  413. return -1;
  414. if (!existing_entry)
  415. {
  416. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  417. (const void *)key : strdup(key);
  418. if (k == NULL)
  419. return -1;
  420. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  421. }
  422. existing_value = (json_object *) lh_entry_v(existing_entry);
  423. if (existing_value)
  424. json_object_put(existing_value);
  425. existing_entry->v = val;
  426. return 0;
  427. }
  428. int json_object_object_add(struct json_object* jso, const char *key,
  429. struct json_object *val)
  430. {
  431. return json_object_object_add_ex(jso, key, val, 0);
  432. }
  433. int json_object_object_length(const struct json_object *jso)
  434. {
  435. assert(json_object_get_type(jso) == json_type_object);
  436. return lh_table_length(jso->o.c_object);
  437. }
  438. struct json_object* json_object_object_get(const struct json_object* jso,
  439. const char *key)
  440. {
  441. struct json_object *result = NULL;
  442. json_object_object_get_ex(jso, key, &result);
  443. return result;
  444. }
  445. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  446. struct json_object **value)
  447. {
  448. if (value != NULL)
  449. *value = NULL;
  450. if (NULL == jso)
  451. return FALSE;
  452. switch(jso->o_type)
  453. {
  454. case json_type_object:
  455. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  456. (void**) value);
  457. default:
  458. if (value != NULL)
  459. *value = NULL;
  460. return FALSE;
  461. }
  462. }
  463. void json_object_object_del(struct json_object* jso, const char *key)
  464. {
  465. assert(json_object_get_type(jso) == json_type_object);
  466. lh_table_delete(jso->o.c_object, key);
  467. }
  468. /* json_object_boolean */
  469. static int json_object_boolean_to_json_string(struct json_object* jso,
  470. struct printbuf *pb,
  471. int level,
  472. int flags)
  473. {
  474. if (jso->o.c_boolean)
  475. return printbuf_strappend(pb, "true");
  476. return printbuf_strappend(pb, "false");
  477. }
  478. struct json_object* json_object_new_boolean(json_bool b)
  479. {
  480. struct json_object *jso = json_object_new(json_type_boolean);
  481. if (!jso)
  482. return NULL;
  483. jso->_to_json_string = &json_object_boolean_to_json_string;
  484. jso->o.c_boolean = b;
  485. return jso;
  486. }
  487. json_bool json_object_get_boolean(const struct json_object *jso)
  488. {
  489. if (!jso)
  490. return FALSE;
  491. switch(jso->o_type)
  492. {
  493. case json_type_boolean:
  494. return jso->o.c_boolean;
  495. case json_type_int:
  496. return (jso->o.c_int64 != 0);
  497. case json_type_double:
  498. return (jso->o.c_double != 0);
  499. case json_type_string:
  500. return (jso->o.c_string.len != 0);
  501. default:
  502. return FALSE;
  503. }
  504. }
  505. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  506. if (!jso || jso->o_type!=json_type_boolean)
  507. return 0;
  508. jso->o.c_boolean=new_value;
  509. return 1;
  510. }
  511. /* json_object_int */
  512. static int json_object_int_to_json_string(struct json_object* jso,
  513. struct printbuf *pb,
  514. int level,
  515. int flags)
  516. {
  517. /* room for 19 digits, the sign char, and a null term */
  518. char sbuf[21];
  519. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
  520. return printbuf_memappend (pb, sbuf, strlen(sbuf));
  521. }
  522. struct json_object* json_object_new_int(int32_t i)
  523. {
  524. struct json_object *jso = json_object_new(json_type_int);
  525. if (!jso)
  526. return NULL;
  527. jso->_to_json_string = &json_object_int_to_json_string;
  528. jso->o.c_int64 = i;
  529. return jso;
  530. }
  531. int32_t json_object_get_int(const struct json_object *jso)
  532. {
  533. int64_t cint64;
  534. enum json_type o_type;
  535. if(!jso) return 0;
  536. o_type = jso->o_type;
  537. cint64 = jso->o.c_int64;
  538. if (o_type == json_type_string)
  539. {
  540. /*
  541. * Parse strings into 64-bit numbers, then use the
  542. * 64-to-32-bit number handling below.
  543. */
  544. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  545. return 0; /* whoops, it didn't work. */
  546. o_type = json_type_int;
  547. }
  548. switch(o_type) {
  549. case json_type_int:
  550. /* Make sure we return the correct values for out of range numbers. */
  551. if (cint64 <= INT32_MIN)
  552. return INT32_MIN;
  553. if (cint64 >= INT32_MAX)
  554. return INT32_MAX;
  555. return (int32_t) cint64;
  556. case json_type_double:
  557. if (jso->o.c_double <= INT32_MIN)
  558. return INT32_MIN;
  559. if (jso->o.c_double >= INT32_MAX)
  560. return INT32_MAX;
  561. return (int32_t)jso->o.c_double;
  562. case json_type_boolean:
  563. return jso->o.c_boolean;
  564. default:
  565. return 0;
  566. }
  567. }
  568. int json_object_set_int(struct json_object *jso,int new_value){
  569. if (!jso || jso->o_type!=json_type_int)
  570. return 0;
  571. jso->o.c_int64=new_value;
  572. return 1;
  573. }
  574. int json_object_add_int(struct json_object *jso, int val) {
  575. if (!jso || jso->o_type != json_type_int)
  576. return 0;
  577. if (val > 0 && jso->o.c_int64 > INT32_MAX - val) {
  578. jso->o.c_int64 = INT32_MAX;
  579. } else if (val < 0 && jso->o.c_int64 < INT32_MIN - val) {
  580. jso->o.c_int64 = INT32_MIN;
  581. } else {
  582. jso->o.c_int64 += val;
  583. }
  584. return 1;
  585. }
  586. struct json_object* json_object_new_int64(int64_t i)
  587. {
  588. struct json_object *jso = json_object_new(json_type_int);
  589. if (!jso)
  590. return NULL;
  591. jso->_to_json_string = &json_object_int_to_json_string;
  592. jso->o.c_int64 = i;
  593. return jso;
  594. }
  595. int64_t json_object_get_int64(const struct json_object *jso)
  596. {
  597. int64_t cint;
  598. if (!jso)
  599. return 0;
  600. switch(jso->o_type)
  601. {
  602. case json_type_int:
  603. return jso->o.c_int64;
  604. case json_type_double:
  605. if (jso->o.c_double >= INT64_MAX)
  606. return INT64_MAX;
  607. if (jso->o.c_double <= INT64_MIN)
  608. return INT64_MIN;
  609. return (int64_t)jso->o.c_double;
  610. case json_type_boolean:
  611. return jso->o.c_boolean;
  612. case json_type_string:
  613. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  614. return cint;
  615. /* FALLTHRU */
  616. default:
  617. return 0;
  618. }
  619. }
  620. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  621. if (!jso || jso->o_type!=json_type_int)
  622. return 0;
  623. jso->o.c_int64=new_value;
  624. return 1;
  625. }
  626. int json_object_add_int64(struct json_object *jso, int64_t val) {
  627. if (!jso || jso->o_type != json_type_int)
  628. return 0;
  629. if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
  630. jso->o.c_int64 = INT64_MAX;
  631. } else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
  632. jso->o.c_int64 = INT64_MIN;
  633. } else {
  634. jso->o.c_int64 += val;
  635. }
  636. return 1;
  637. }
  638. /* json_object_double */
  639. #if defined(HAVE___THREAD)
  640. // i.e. __thread or __declspec(thread)
  641. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  642. #endif
  643. static char *global_serialization_float_format = NULL;
  644. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  645. {
  646. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  647. {
  648. #if defined(HAVE___THREAD)
  649. if (tls_serialization_float_format)
  650. {
  651. free(tls_serialization_float_format);
  652. tls_serialization_float_format = NULL;
  653. }
  654. #endif
  655. if (global_serialization_float_format)
  656. free(global_serialization_float_format);
  657. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  658. }
  659. else if (global_or_thread == JSON_C_OPTION_THREAD)
  660. {
  661. #if defined(HAVE___THREAD)
  662. if (tls_serialization_float_format)
  663. {
  664. free(tls_serialization_float_format);
  665. tls_serialization_float_format = NULL;
  666. }
  667. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  668. #else
  669. _set_last_err("json_c_set_option: not compiled with __thread support\n");
  670. return -1;
  671. #endif
  672. }
  673. else
  674. {
  675. _set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  676. return -1;
  677. }
  678. return 0;
  679. }
  680. static int json_object_double_to_json_string_format(struct json_object* jso,
  681. struct printbuf *pb,
  682. int level,
  683. int flags,
  684. const char *format)
  685. {
  686. char buf[128], *p, *q;
  687. int size;
  688. double dummy; /* needed for modf() */
  689. /* Although JSON RFC does not support
  690. NaN or Infinity as numeric values
  691. ECMA 262 section 9.8.1 defines
  692. how to handle these cases as strings */
  693. if (isnan(jso->o.c_double))
  694. {
  695. size = snprintf(buf, sizeof(buf), "NaN");
  696. }
  697. else if (isinf(jso->o.c_double))
  698. {
  699. if(jso->o.c_double > 0)
  700. size = snprintf(buf, sizeof(buf), "Infinity");
  701. else
  702. size = snprintf(buf, sizeof(buf), "-Infinity");
  703. }
  704. else
  705. {
  706. const char *std_format = "%.17g";
  707. #if defined(HAVE___THREAD)
  708. if (tls_serialization_float_format)
  709. std_format = tls_serialization_float_format;
  710. else
  711. #endif
  712. if (global_serialization_float_format)
  713. std_format = global_serialization_float_format;
  714. if (!format)
  715. format = std_format;
  716. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  717. if (modf(jso->o.c_double, &dummy) == 0 && size >= 0 && size < (int)sizeof(buf) - 2)
  718. {
  719. // Ensure it looks like a float, even if snprintf didn't.
  720. strcat(buf, ".0");
  721. size += 2;
  722. }
  723. }
  724. // although unlikely, snprintf can fail
  725. if (size < 0)
  726. return -1;
  727. p = strchr(buf, ',');
  728. if (p)
  729. *p = '.';
  730. else
  731. p = strchr(buf, '.');
  732. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  733. {
  734. /* last useful digit, always keep 1 zero */
  735. p++;
  736. for (q=p ; *q ; q++) {
  737. if (*q!='0') p=q;
  738. }
  739. /* drop trailing zeroes */
  740. *(++p) = 0;
  741. size = p-buf;
  742. }
  743. if (size >= (int)sizeof(buf))
  744. // The standard formats are guaranteed not to overrun the buffer,
  745. // but if a custom one happens to do so, just silently truncate.
  746. size = sizeof(buf) - 1;
  747. printbuf_memappend(pb, buf, size);
  748. return size;
  749. }
  750. static int json_object_double_to_json_string_default(struct json_object* jso,
  751. struct printbuf *pb,
  752. int level,
  753. int flags)
  754. {
  755. return json_object_double_to_json_string_format(jso, pb, level, flags,
  756. NULL);
  757. }
  758. int json_object_double_to_json_string(struct json_object* jso,
  759. struct printbuf *pb,
  760. int level,
  761. int flags)
  762. {
  763. return json_object_double_to_json_string_format(jso, pb, level, flags,
  764. (const char *)jso->_userdata);
  765. }
  766. struct json_object* json_object_new_double(double d)
  767. {
  768. struct json_object *jso = json_object_new(json_type_double);
  769. if (!jso)
  770. return NULL;
  771. jso->_to_json_string = &json_object_double_to_json_string_default;
  772. jso->o.c_double = d;
  773. return jso;
  774. }
  775. struct json_object* json_object_new_double_s(double d, const char *ds)
  776. {
  777. char *new_ds;
  778. struct json_object *jso = json_object_new_double(d);
  779. if (!jso)
  780. return NULL;
  781. new_ds = strdup(ds);
  782. if (!new_ds)
  783. {
  784. json_object_generic_delete(jso);
  785. errno = ENOMEM;
  786. return NULL;
  787. }
  788. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  789. new_ds, json_object_free_userdata);
  790. return jso;
  791. }
  792. int json_object_userdata_to_json_string(struct json_object *jso,
  793. struct printbuf *pb, int level, int flags)
  794. {
  795. int userdata_len = strlen((const char *)jso->_userdata);
  796. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  797. return userdata_len;
  798. }
  799. void json_object_free_userdata(struct json_object *jso, void *userdata)
  800. {
  801. free(userdata);
  802. }
  803. double json_object_get_double(const struct json_object *jso)
  804. {
  805. double cdouble;
  806. char *errPtr = NULL;
  807. if(!jso) return 0.0;
  808. switch(jso->o_type) {
  809. case json_type_double:
  810. return jso->o.c_double;
  811. case json_type_int:
  812. return jso->o.c_int64;
  813. case json_type_boolean:
  814. return jso->o.c_boolean;
  815. case json_type_string:
  816. errno = 0;
  817. cdouble = strtod(get_string_component(jso), &errPtr);
  818. /* if conversion stopped at the first character, return 0.0 */
  819. if (errPtr == get_string_component(jso))
  820. return 0.0;
  821. /*
  822. * Check that the conversion terminated on something sensible
  823. *
  824. * For example, { "pay" : 123AB } would parse as 123.
  825. */
  826. if (*errPtr != '\0')
  827. return 0.0;
  828. /*
  829. * If strtod encounters a string which would exceed the
  830. * capacity of a double, it returns +/- HUGE_VAL and sets
  831. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  832. * from a conversion, so we need to check errno.
  833. *
  834. * Underflow also sets errno to ERANGE, but it returns 0 in
  835. * that case, which is what we will return anyway.
  836. *
  837. * See CERT guideline ERR30-C
  838. */
  839. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  840. (ERANGE == errno))
  841. cdouble = 0.0;
  842. return cdouble;
  843. default:
  844. return 0.0;
  845. }
  846. }
  847. int json_object_set_double(struct json_object *jso,double new_value){
  848. if (!jso || jso->o_type!=json_type_double)
  849. return 0;
  850. jso->o.c_double=new_value;
  851. return 1;
  852. }
  853. /* json_object_string */
  854. static int json_object_string_to_json_string(struct json_object* jso,
  855. struct printbuf *pb,
  856. int level,
  857. int flags)
  858. {
  859. printbuf_strappend(pb, "\"");
  860. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  861. printbuf_strappend(pb, "\"");
  862. return 0;
  863. }
  864. static void json_object_string_delete(struct json_object* jso)
  865. {
  866. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  867. free(jso->o.c_string.str.ptr);
  868. json_object_generic_delete(jso);
  869. }
  870. struct json_object* json_object_new_string(const char *s)
  871. {
  872. struct json_object *jso = json_object_new(json_type_string);
  873. if (!jso)
  874. return NULL;
  875. jso->_delete = &json_object_string_delete;
  876. jso->_to_json_string = &json_object_string_to_json_string;
  877. jso->o.c_string.len = strlen(s);
  878. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  879. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  880. } else {
  881. jso->o.c_string.str.ptr = strdup(s);
  882. if (!jso->o.c_string.str.ptr)
  883. {
  884. json_object_generic_delete(jso);
  885. errno = ENOMEM;
  886. return NULL;
  887. }
  888. }
  889. return jso;
  890. }
  891. struct json_object* json_object_new_string_len(const char *s, int len)
  892. {
  893. char *dstbuf;
  894. struct json_object *jso = json_object_new(json_type_string);
  895. if (!jso)
  896. return NULL;
  897. jso->_delete = &json_object_string_delete;
  898. jso->_to_json_string = &json_object_string_to_json_string;
  899. if(len < LEN_DIRECT_STRING_DATA) {
  900. dstbuf = jso->o.c_string.str.data;
  901. } else {
  902. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  903. if (!jso->o.c_string.str.ptr)
  904. {
  905. json_object_generic_delete(jso);
  906. errno = ENOMEM;
  907. return NULL;
  908. }
  909. dstbuf = jso->o.c_string.str.ptr;
  910. }
  911. memcpy(dstbuf, (const void *)s, len);
  912. dstbuf[len] = '\0';
  913. jso->o.c_string.len = len;
  914. return jso;
  915. }
  916. const char* json_object_get_string(struct json_object *jso)
  917. {
  918. if (!jso)
  919. return NULL;
  920. switch(jso->o_type)
  921. {
  922. case json_type_string:
  923. return get_string_component(jso);
  924. default:
  925. return json_object_to_json_string(jso);
  926. }
  927. }
  928. int json_object_get_string_len(const struct json_object *jso)
  929. {
  930. if (!jso)
  931. return 0;
  932. switch(jso->o_type)
  933. {
  934. case json_type_string:
  935. return jso->o.c_string.len;
  936. default:
  937. return 0;
  938. }
  939. }
  940. int json_object_set_string(json_object* jso, const char* s) {
  941. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  942. }
  943. int json_object_set_string_len(json_object* jso, const char* s, int len){
  944. char *dstbuf;
  945. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  946. if (len<LEN_DIRECT_STRING_DATA) {
  947. dstbuf=jso->o.c_string.str.data;
  948. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  949. } else {
  950. dstbuf=(char *)malloc(len+1);
  951. if (dstbuf==NULL) return 0;
  952. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  953. jso->o.c_string.str.ptr=dstbuf;
  954. }
  955. jso->o.c_string.len=len;
  956. memcpy(dstbuf, (const void *)s, len);
  957. dstbuf[len] = '\0';
  958. return 1;
  959. }
  960. /* json_object_array */
  961. static int json_object_array_to_json_string(struct json_object* jso,
  962. struct printbuf *pb,
  963. int level,
  964. int flags)
  965. {
  966. int had_children = 0;
  967. size_t ii;
  968. printbuf_strappend(pb, "[");
  969. if (flags & JSON_C_TO_STRING_PRETTY)
  970. printbuf_strappend(pb, "\n");
  971. for(ii=0; ii < json_object_array_length(jso); ii++)
  972. {
  973. struct json_object *val;
  974. if (had_children)
  975. {
  976. printbuf_strappend(pb, ",");
  977. if (flags & JSON_C_TO_STRING_PRETTY)
  978. printbuf_strappend(pb, "\n");
  979. }
  980. had_children = 1;
  981. if (flags & JSON_C_TO_STRING_SPACED)
  982. printbuf_strappend(pb, " ");
  983. indent(pb, level + 1, flags);
  984. val = json_object_array_get_idx(jso, ii);
  985. if(val == NULL)
  986. printbuf_strappend(pb, "null");
  987. else
  988. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  989. return -1;
  990. }
  991. if (flags & JSON_C_TO_STRING_PRETTY)
  992. {
  993. if (had_children)
  994. printbuf_strappend(pb, "\n");
  995. indent(pb,level,flags);
  996. }
  997. if (flags & JSON_C_TO_STRING_SPACED)
  998. return printbuf_strappend(pb, " ]");
  999. return printbuf_strappend(pb, "]");
  1000. }
  1001. static void json_object_array_entry_free(void *data)
  1002. {
  1003. json_object_put((struct json_object*)data);
  1004. }
  1005. static void json_object_array_delete(struct json_object* jso)
  1006. {
  1007. array_list_free(jso->o.c_array);
  1008. json_object_generic_delete(jso);
  1009. }
  1010. struct json_object* json_object_new_array(void)
  1011. {
  1012. struct json_object *jso = json_object_new(json_type_array);
  1013. if (!jso)
  1014. return NULL;
  1015. jso->_delete = &json_object_array_delete;
  1016. jso->_to_json_string = &json_object_array_to_json_string;
  1017. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1018. if(jso->o.c_array == NULL)
  1019. {
  1020. free(jso);
  1021. return NULL;
  1022. }
  1023. return jso;
  1024. }
  1025. struct array_list* json_object_get_array(const struct json_object *jso)
  1026. {
  1027. if (!jso)
  1028. return NULL;
  1029. switch(jso->o_type)
  1030. {
  1031. case json_type_array:
  1032. return jso->o.c_array;
  1033. default:
  1034. return NULL;
  1035. }
  1036. }
  1037. void json_object_array_sort(struct json_object *jso,
  1038. int(*sort_fn)(const void *, const void *))
  1039. {
  1040. assert(json_object_get_type(jso) == json_type_array);
  1041. array_list_sort(jso->o.c_array, sort_fn);
  1042. }
  1043. struct json_object* json_object_array_bsearch(
  1044. const struct json_object *key,
  1045. const struct json_object *jso,
  1046. int (*sort_fn)(const void *, const void *))
  1047. {
  1048. struct json_object **result;
  1049. assert(json_object_get_type(jso) == json_type_array);
  1050. result = (struct json_object **)array_list_bsearch(
  1051. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1052. if (!result)
  1053. return NULL;
  1054. return *result;
  1055. }
  1056. size_t json_object_array_length(const struct json_object *jso)
  1057. {
  1058. assert(json_object_get_type(jso) == json_type_array);
  1059. return array_list_length(jso->o.c_array);
  1060. }
  1061. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1062. {
  1063. assert(json_object_get_type(jso) == json_type_array);
  1064. return array_list_add(jso->o.c_array, val);
  1065. }
  1066. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1067. struct json_object *val)
  1068. {
  1069. assert(json_object_get_type(jso) == json_type_array);
  1070. return array_list_put_idx(jso->o.c_array, idx, val);
  1071. }
  1072. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1073. {
  1074. assert(json_object_get_type(jso) == json_type_array);
  1075. return array_list_del_idx(jso->o.c_array, idx, count);
  1076. }
  1077. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1078. size_t idx)
  1079. {
  1080. assert(json_object_get_type(jso) == json_type_array);
  1081. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1082. }
  1083. static int json_array_equal(struct json_object* jso1,
  1084. struct json_object* jso2)
  1085. {
  1086. size_t len, i;
  1087. len = json_object_array_length(jso1);
  1088. if (len != json_object_array_length(jso2))
  1089. return 0;
  1090. for (i = 0; i < len; i++) {
  1091. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1092. json_object_array_get_idx(jso2, i)))
  1093. return 0;
  1094. }
  1095. return 1;
  1096. }
  1097. static int json_object_all_values_equal(struct json_object* jso1,
  1098. struct json_object* jso2)
  1099. {
  1100. struct json_object_iter iter;
  1101. struct json_object *sub;
  1102. assert(json_object_get_type(jso1) == json_type_object);
  1103. assert(json_object_get_type(jso2) == json_type_object);
  1104. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1105. json_object_object_foreachC(jso1, iter) {
  1106. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1107. (void**)(void *)&sub))
  1108. return 0;
  1109. if (!json_object_equal(iter.val, sub))
  1110. return 0;
  1111. }
  1112. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1113. json_object_object_foreachC(jso2, iter) {
  1114. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1115. (void**)(void *)&sub))
  1116. return 0;
  1117. }
  1118. return 1;
  1119. }
  1120. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1121. {
  1122. if (jso1 == jso2)
  1123. return 1;
  1124. if (!jso1 || !jso2)
  1125. return 0;
  1126. if (jso1->o_type != jso2->o_type)
  1127. return 0;
  1128. switch(jso1->o_type) {
  1129. case json_type_boolean:
  1130. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1131. case json_type_double:
  1132. return (jso1->o.c_double == jso2->o.c_double);
  1133. case json_type_int:
  1134. return (jso1->o.c_int64 == jso2->o.c_int64);
  1135. case json_type_string:
  1136. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1137. memcmp(get_string_component(jso1),
  1138. get_string_component(jso2),
  1139. jso1->o.c_string.len) == 0);
  1140. case json_type_object:
  1141. return json_object_all_values_equal(jso1, jso2);
  1142. case json_type_array:
  1143. return json_array_equal(jso1, jso2);
  1144. case json_type_null:
  1145. return 1;
  1146. };
  1147. return 0;
  1148. }