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

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