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