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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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. struct json_object* json_object_new_int64(int64_t i)
  575. {
  576. struct json_object *jso = json_object_new(json_type_int);
  577. if (!jso)
  578. return NULL;
  579. jso->_to_json_string = &json_object_int_to_json_string;
  580. jso->o.c_int64 = i;
  581. return jso;
  582. }
  583. int64_t json_object_get_int64(const struct json_object *jso)
  584. {
  585. int64_t cint;
  586. if (!jso)
  587. return 0;
  588. switch(jso->o_type)
  589. {
  590. case json_type_int:
  591. return jso->o.c_int64;
  592. case json_type_double:
  593. return (int64_t)jso->o.c_double;
  594. case json_type_boolean:
  595. return jso->o.c_boolean;
  596. case json_type_string:
  597. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  598. return cint;
  599. /* FALLTHRU */
  600. default:
  601. return 0;
  602. }
  603. }
  604. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  605. if (!jso || jso->o_type!=json_type_int)
  606. return 0;
  607. jso->o.c_int64=new_value;
  608. return 1;
  609. }
  610. /* json_object_double */
  611. #if defined(HAVE___THREAD)
  612. // i.e. __thread or __declspec(thread)
  613. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  614. #endif
  615. static char *global_serialization_float_format = NULL;
  616. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  617. {
  618. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  619. {
  620. #if defined(HAVE___THREAD)
  621. if (tls_serialization_float_format)
  622. {
  623. free(tls_serialization_float_format);
  624. tls_serialization_float_format = NULL;
  625. }
  626. #endif
  627. if (global_serialization_float_format)
  628. free(global_serialization_float_format);
  629. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  630. }
  631. else if (global_or_thread == JSON_C_OPTION_THREAD)
  632. {
  633. #if defined(HAVE___THREAD)
  634. if (tls_serialization_float_format)
  635. {
  636. free(tls_serialization_float_format);
  637. tls_serialization_float_format = NULL;
  638. }
  639. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  640. #else
  641. _set_last_err("json_c_set_option: not compiled with __thread support\n");
  642. return -1;
  643. #endif
  644. }
  645. else
  646. {
  647. _set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  648. return -1;
  649. }
  650. return 0;
  651. }
  652. static int json_object_double_to_json_string_format(struct json_object* jso,
  653. struct printbuf *pb,
  654. int level,
  655. int flags,
  656. const char *format)
  657. {
  658. char buf[128], *p, *q;
  659. int size;
  660. double dummy; /* needed for modf() */
  661. /* Although JSON RFC does not support
  662. NaN or Infinity as numeric values
  663. ECMA 262 section 9.8.1 defines
  664. how to handle these cases as strings */
  665. if (isnan(jso->o.c_double))
  666. {
  667. size = snprintf(buf, sizeof(buf), "NaN");
  668. }
  669. else if (isinf(jso->o.c_double))
  670. {
  671. if(jso->o.c_double > 0)
  672. size = snprintf(buf, sizeof(buf), "Infinity");
  673. else
  674. size = snprintf(buf, sizeof(buf), "-Infinity");
  675. }
  676. else
  677. {
  678. const char *std_format = "%.17g";
  679. #if defined(HAVE___THREAD)
  680. if (tls_serialization_float_format)
  681. std_format = tls_serialization_float_format;
  682. else
  683. #endif
  684. if (global_serialization_float_format)
  685. std_format = global_serialization_float_format;
  686. if (!format)
  687. format = std_format;
  688. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  689. if (modf(jso->o.c_double, &dummy) == 0 && size >= 0 && size < (int)sizeof(buf) - 2)
  690. {
  691. // Ensure it looks like a float, even if snprintf didn't.
  692. strcat(buf, ".0");
  693. size += 2;
  694. }
  695. }
  696. // although unlikely, snprintf can fail
  697. if (size < 0)
  698. return -1;
  699. p = strchr(buf, ',');
  700. if (p)
  701. *p = '.';
  702. else
  703. p = strchr(buf, '.');
  704. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  705. {
  706. /* last useful digit, always keep 1 zero */
  707. p++;
  708. for (q=p ; *q ; q++) {
  709. if (*q!='0') p=q;
  710. }
  711. /* drop trailing zeroes */
  712. *(++p) = 0;
  713. size = p-buf;
  714. }
  715. if (size >= (int)sizeof(buf))
  716. // The standard formats are guaranteed not to overrun the buffer,
  717. // but if a custom one happens to do so, just silently truncate.
  718. size = sizeof(buf) - 1;
  719. printbuf_memappend(pb, buf, size);
  720. return size;
  721. }
  722. static int json_object_double_to_json_string_default(struct json_object* jso,
  723. struct printbuf *pb,
  724. int level,
  725. int flags)
  726. {
  727. return json_object_double_to_json_string_format(jso, pb, level, flags,
  728. NULL);
  729. }
  730. int json_object_double_to_json_string(struct json_object* jso,
  731. struct printbuf *pb,
  732. int level,
  733. int flags)
  734. {
  735. return json_object_double_to_json_string_format(jso, pb, level, flags,
  736. (const char *)jso->_userdata);
  737. }
  738. struct json_object* json_object_new_double(double d)
  739. {
  740. struct json_object *jso = json_object_new(json_type_double);
  741. if (!jso)
  742. return NULL;
  743. jso->_to_json_string = &json_object_double_to_json_string_default;
  744. jso->o.c_double = d;
  745. return jso;
  746. }
  747. struct json_object* json_object_new_double_s(double d, const char *ds)
  748. {
  749. char *new_ds;
  750. struct json_object *jso = json_object_new_double(d);
  751. if (!jso)
  752. return NULL;
  753. new_ds = strdup(ds);
  754. if (!new_ds)
  755. {
  756. json_object_generic_delete(jso);
  757. errno = ENOMEM;
  758. return NULL;
  759. }
  760. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  761. new_ds, json_object_free_userdata);
  762. return jso;
  763. }
  764. int json_object_userdata_to_json_string(struct json_object *jso,
  765. struct printbuf *pb, int level, int flags)
  766. {
  767. int userdata_len = strlen((const char *)jso->_userdata);
  768. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  769. return userdata_len;
  770. }
  771. void json_object_free_userdata(struct json_object *jso, void *userdata)
  772. {
  773. free(userdata);
  774. }
  775. double json_object_get_double(const struct json_object *jso)
  776. {
  777. double cdouble;
  778. char *errPtr = NULL;
  779. if(!jso) return 0.0;
  780. switch(jso->o_type) {
  781. case json_type_double:
  782. return jso->o.c_double;
  783. case json_type_int:
  784. return jso->o.c_int64;
  785. case json_type_boolean:
  786. return jso->o.c_boolean;
  787. case json_type_string:
  788. errno = 0;
  789. cdouble = strtod(get_string_component(jso), &errPtr);
  790. /* if conversion stopped at the first character, return 0.0 */
  791. if (errPtr == get_string_component(jso))
  792. return 0.0;
  793. /*
  794. * Check that the conversion terminated on something sensible
  795. *
  796. * For example, { "pay" : 123AB } would parse as 123.
  797. */
  798. if (*errPtr != '\0')
  799. return 0.0;
  800. /*
  801. * If strtod encounters a string which would exceed the
  802. * capacity of a double, it returns +/- HUGE_VAL and sets
  803. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  804. * from a conversion, so we need to check errno.
  805. *
  806. * Underflow also sets errno to ERANGE, but it returns 0 in
  807. * that case, which is what we will return anyway.
  808. *
  809. * See CERT guideline ERR30-C
  810. */
  811. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  812. (ERANGE == errno))
  813. cdouble = 0.0;
  814. return cdouble;
  815. default:
  816. return 0.0;
  817. }
  818. }
  819. int json_object_set_double(struct json_object *jso,double new_value){
  820. if (!jso || jso->o_type!=json_type_double)
  821. return 0;
  822. jso->o.c_double=new_value;
  823. return 1;
  824. }
  825. /* json_object_string */
  826. static int json_object_string_to_json_string(struct json_object* jso,
  827. struct printbuf *pb,
  828. int level,
  829. int flags)
  830. {
  831. printbuf_strappend(pb, "\"");
  832. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  833. printbuf_strappend(pb, "\"");
  834. return 0;
  835. }
  836. static void json_object_string_delete(struct json_object* jso)
  837. {
  838. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  839. free(jso->o.c_string.str.ptr);
  840. json_object_generic_delete(jso);
  841. }
  842. struct json_object* json_object_new_string(const char *s)
  843. {
  844. struct json_object *jso = json_object_new(json_type_string);
  845. if (!jso)
  846. return NULL;
  847. jso->_delete = &json_object_string_delete;
  848. jso->_to_json_string = &json_object_string_to_json_string;
  849. jso->o.c_string.len = strlen(s);
  850. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  851. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  852. } else {
  853. jso->o.c_string.str.ptr = strdup(s);
  854. if (!jso->o.c_string.str.ptr)
  855. {
  856. json_object_generic_delete(jso);
  857. errno = ENOMEM;
  858. return NULL;
  859. }
  860. }
  861. return jso;
  862. }
  863. struct json_object* json_object_new_string_len(const char *s, int len)
  864. {
  865. char *dstbuf;
  866. struct json_object *jso = json_object_new(json_type_string);
  867. if (!jso)
  868. return NULL;
  869. jso->_delete = &json_object_string_delete;
  870. jso->_to_json_string = &json_object_string_to_json_string;
  871. if(len < LEN_DIRECT_STRING_DATA) {
  872. dstbuf = jso->o.c_string.str.data;
  873. } else {
  874. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  875. if (!jso->o.c_string.str.ptr)
  876. {
  877. json_object_generic_delete(jso);
  878. errno = ENOMEM;
  879. return NULL;
  880. }
  881. dstbuf = jso->o.c_string.str.ptr;
  882. }
  883. memcpy(dstbuf, (const void *)s, len);
  884. dstbuf[len] = '\0';
  885. jso->o.c_string.len = len;
  886. return jso;
  887. }
  888. const char* json_object_get_string(struct json_object *jso)
  889. {
  890. if (!jso)
  891. return NULL;
  892. switch(jso->o_type)
  893. {
  894. case json_type_string:
  895. return get_string_component(jso);
  896. default:
  897. return json_object_to_json_string(jso);
  898. }
  899. }
  900. int json_object_get_string_len(const struct json_object *jso)
  901. {
  902. if (!jso)
  903. return 0;
  904. switch(jso->o_type)
  905. {
  906. case json_type_string:
  907. return jso->o.c_string.len;
  908. default:
  909. return 0;
  910. }
  911. }
  912. int json_object_set_string(json_object* jso, const char* s) {
  913. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  914. }
  915. int json_object_set_string_len(json_object* jso, const char* s, int len){
  916. char *dstbuf;
  917. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  918. if (len<LEN_DIRECT_STRING_DATA) {
  919. dstbuf=jso->o.c_string.str.data;
  920. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  921. } else {
  922. dstbuf=(char *)malloc(len+1);
  923. if (dstbuf==NULL) return 0;
  924. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  925. jso->o.c_string.str.ptr=dstbuf;
  926. }
  927. jso->o.c_string.len=len;
  928. memcpy(dstbuf, (const void *)s, len);
  929. dstbuf[len] = '\0';
  930. return 1;
  931. }
  932. /* json_object_array */
  933. static int json_object_array_to_json_string(struct json_object* jso,
  934. struct printbuf *pb,
  935. int level,
  936. int flags)
  937. {
  938. int had_children = 0;
  939. size_t ii;
  940. printbuf_strappend(pb, "[");
  941. if (flags & JSON_C_TO_STRING_PRETTY)
  942. printbuf_strappend(pb, "\n");
  943. for(ii=0; ii < json_object_array_length(jso); ii++)
  944. {
  945. struct json_object *val;
  946. if (had_children)
  947. {
  948. printbuf_strappend(pb, ",");
  949. if (flags & JSON_C_TO_STRING_PRETTY)
  950. printbuf_strappend(pb, "\n");
  951. }
  952. had_children = 1;
  953. if (flags & JSON_C_TO_STRING_SPACED)
  954. printbuf_strappend(pb, " ");
  955. indent(pb, level + 1, flags);
  956. val = json_object_array_get_idx(jso, ii);
  957. if(val == NULL)
  958. printbuf_strappend(pb, "null");
  959. else
  960. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  961. return -1;
  962. }
  963. if (flags & JSON_C_TO_STRING_PRETTY)
  964. {
  965. if (had_children)
  966. printbuf_strappend(pb, "\n");
  967. indent(pb,level,flags);
  968. }
  969. if (flags & JSON_C_TO_STRING_SPACED)
  970. return printbuf_strappend(pb, " ]");
  971. return printbuf_strappend(pb, "]");
  972. }
  973. static void json_object_array_entry_free(void *data)
  974. {
  975. json_object_put((struct json_object*)data);
  976. }
  977. static void json_object_array_delete(struct json_object* jso)
  978. {
  979. array_list_free(jso->o.c_array);
  980. json_object_generic_delete(jso);
  981. }
  982. struct json_object* json_object_new_array(void)
  983. {
  984. struct json_object *jso = json_object_new(json_type_array);
  985. if (!jso)
  986. return NULL;
  987. jso->_delete = &json_object_array_delete;
  988. jso->_to_json_string = &json_object_array_to_json_string;
  989. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  990. if(jso->o.c_array == NULL)
  991. {
  992. free(jso);
  993. return NULL;
  994. }
  995. return jso;
  996. }
  997. struct array_list* json_object_get_array(const struct json_object *jso)
  998. {
  999. if (!jso)
  1000. return NULL;
  1001. switch(jso->o_type)
  1002. {
  1003. case json_type_array:
  1004. return jso->o.c_array;
  1005. default:
  1006. return NULL;
  1007. }
  1008. }
  1009. void json_object_array_sort(struct json_object *jso,
  1010. int(*sort_fn)(const void *, const void *))
  1011. {
  1012. assert(json_object_get_type(jso) == json_type_array);
  1013. array_list_sort(jso->o.c_array, sort_fn);
  1014. }
  1015. struct json_object* json_object_array_bsearch(
  1016. const struct json_object *key,
  1017. const struct json_object *jso,
  1018. int (*sort_fn)(const void *, const void *))
  1019. {
  1020. struct json_object **result;
  1021. assert(json_object_get_type(jso) == json_type_array);
  1022. result = (struct json_object **)array_list_bsearch(
  1023. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1024. if (!result)
  1025. return NULL;
  1026. return *result;
  1027. }
  1028. size_t json_object_array_length(const struct json_object *jso)
  1029. {
  1030. assert(json_object_get_type(jso) == json_type_array);
  1031. return array_list_length(jso->o.c_array);
  1032. }
  1033. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1034. {
  1035. assert(json_object_get_type(jso) == json_type_array);
  1036. return array_list_add(jso->o.c_array, val);
  1037. }
  1038. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1039. struct json_object *val)
  1040. {
  1041. assert(json_object_get_type(jso) == json_type_array);
  1042. return array_list_put_idx(jso->o.c_array, idx, val);
  1043. }
  1044. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1045. {
  1046. assert(json_object_get_type(jso) == json_type_array);
  1047. return array_list_del_idx(jso->o.c_array, idx, count);
  1048. }
  1049. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1050. size_t idx)
  1051. {
  1052. assert(json_object_get_type(jso) == json_type_array);
  1053. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1054. }
  1055. static int json_array_equal(struct json_object* jso1,
  1056. struct json_object* jso2)
  1057. {
  1058. size_t len, i;
  1059. len = json_object_array_length(jso1);
  1060. if (len != json_object_array_length(jso2))
  1061. return 0;
  1062. for (i = 0; i < len; i++) {
  1063. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1064. json_object_array_get_idx(jso2, i)))
  1065. return 0;
  1066. }
  1067. return 1;
  1068. }
  1069. static int json_object_all_values_equal(struct json_object* jso1,
  1070. struct json_object* jso2)
  1071. {
  1072. struct json_object_iter iter;
  1073. struct json_object *sub;
  1074. assert(json_object_get_type(jso1) == json_type_object);
  1075. assert(json_object_get_type(jso2) == json_type_object);
  1076. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1077. json_object_object_foreachC(jso1, iter) {
  1078. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1079. (void**)(void *)&sub))
  1080. return 0;
  1081. if (!json_object_equal(iter.val, sub))
  1082. return 0;
  1083. }
  1084. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1085. json_object_object_foreachC(jso2, iter) {
  1086. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1087. (void**)(void *)&sub))
  1088. return 0;
  1089. }
  1090. return 1;
  1091. }
  1092. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1093. {
  1094. if (jso1 == jso2)
  1095. return 1;
  1096. if (!jso1 || !jso2)
  1097. return 0;
  1098. if (jso1->o_type != jso2->o_type)
  1099. return 0;
  1100. switch(jso1->o_type) {
  1101. case json_type_boolean:
  1102. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1103. case json_type_double:
  1104. return (jso1->o.c_double == jso2->o.c_double);
  1105. case json_type_int:
  1106. return (jso1->o.c_int64 == jso2->o.c_int64);
  1107. case json_type_string:
  1108. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1109. memcmp(get_string_component(jso1),
  1110. get_string_component(jso2),
  1111. jso1->o.c_string.len) == 0);
  1112. case json_type_object:
  1113. return json_object_all_values_equal(jso1, jso2);
  1114. case json_type_array:
  1115. return json_array_equal(jso1, jso2);
  1116. case json_type_null:
  1117. return 1;
  1118. };
  1119. return 0;
  1120. }