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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <errno.h>
  19. #include "debug.h"
  20. #include "printbuf.h"
  21. #include "linkhash.h"
  22. #include "arraylist.h"
  23. #include "json_inttypes.h"
  24. #include "json_object.h"
  25. #include "json_object_private.h"
  26. #include "json_util.h"
  27. #include "math_compat.h"
  28. #if !defined(HAVE_STRDUP) && defined(_MSC_VER)
  29. /* MSC has the version as _strdup */
  30. # define strdup _strdup
  31. #elif !defined(HAVE_STRDUP)
  32. # error You do not have strdup on your system.
  33. #endif /* HAVE_STRDUP */
  34. #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
  35. /* MSC has the version as _snprintf */
  36. # define snprintf _snprintf
  37. #elif !defined(HAVE_SNPRINTF)
  38. # error You do not have snprintf on your system.
  39. #endif /* HAVE_SNPRINTF */
  40. // Don't define this. It's not thread-safe.
  41. /* #define REFCOUNT_DEBUG 1 */
  42. const char *json_number_chars = "0123456789.+-eE";
  43. const char *json_hex_chars = "0123456789abcdefABCDEF";
  44. static void json_object_generic_delete(struct json_object* jso);
  45. static struct json_object* json_object_new(enum json_type o_type);
  46. static json_object_to_json_string_fn json_object_object_to_json_string;
  47. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  48. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  49. static json_object_to_json_string_fn json_object_int_to_json_string;
  50. static json_object_to_json_string_fn json_object_string_to_json_string;
  51. static json_object_to_json_string_fn json_object_array_to_json_string;
  52. /* ref count debugging */
  53. #ifdef REFCOUNT_DEBUG
  54. static struct lh_table *json_object_table;
  55. static void json_object_init(void) __attribute__ ((constructor));
  56. static void json_object_init(void) {
  57. MC_DEBUG("json_object_init: creating object table\n");
  58. json_object_table = lh_kptr_table_new(128, NULL);
  59. }
  60. static void json_object_fini(void) __attribute__ ((destructor));
  61. static void json_object_fini(void)
  62. {
  63. struct lh_entry *ent;
  64. if (MC_GET_DEBUG())
  65. {
  66. if (json_object_table->count)
  67. {
  68. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  69. json_object_table->count);
  70. lh_foreach(json_object_table, ent)
  71. {
  72. struct json_object* obj =
  73. (struct json_object*) lh_entry_v(ent);
  74. MC_DEBUG("\t%s:%p\n",
  75. json_type_to_name(obj->o_type), obj);
  76. }
  77. }
  78. }
  79. MC_DEBUG("json_object_fini: freeing object table\n");
  80. lh_table_free(json_object_table);
  81. }
  82. #endif /* REFCOUNT_DEBUG */
  83. /* helper for accessing the optimized string data component in json_object
  84. */
  85. static const char *
  86. get_string_component(const struct json_object *jso)
  87. {
  88. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  89. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  90. }
  91. /* string escaping */
  92. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  93. {
  94. int pos = 0, start_offset = 0;
  95. unsigned char c;
  96. while (len--)
  97. {
  98. c = str[pos];
  99. switch(c)
  100. {
  101. case '\b':
  102. case '\n':
  103. case '\r':
  104. case '\t':
  105. case '\f':
  106. case '"':
  107. case '\\':
  108. case '/':
  109. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  110. {
  111. pos++;
  112. break;
  113. }
  114. if(pos - start_offset > 0)
  115. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  116. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  117. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  118. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  119. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  120. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  121. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  122. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  123. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  124. start_offset = ++pos;
  125. break;
  126. default:
  127. if(c < ' ')
  128. {
  129. if(pos - start_offset > 0)
  130. printbuf_memappend(pb,
  131. str + start_offset,
  132. pos - start_offset);
  133. sprintbuf(pb, "\\u00%c%c",
  134. json_hex_chars[c >> 4],
  135. json_hex_chars[c & 0xf]);
  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->_userdata;
  208. }
  209. void json_object_set_userdata(json_object *jso, void *userdata,
  210. json_object_delete_fn *user_delete)
  211. {
  212. // First, clean up any previously existing user info
  213. if (jso->_user_delete)
  214. jso->_user_delete(jso, jso->_userdata);
  215. jso->_userdata = userdata;
  216. jso->_user_delete = user_delete;
  217. }
  218. /* set a custom conversion to string */
  219. void json_object_set_serializer(json_object *jso,
  220. json_object_to_json_string_fn to_string_func,
  221. void *userdata,
  222. json_object_delete_fn *user_delete)
  223. {
  224. json_object_set_userdata(jso, userdata, user_delete);
  225. if (to_string_func == NULL)
  226. {
  227. // Reset to the standard serialization function
  228. switch(jso->o_type)
  229. {
  230. case json_type_null:
  231. jso->_to_json_string = NULL;
  232. break;
  233. case json_type_boolean:
  234. jso->_to_json_string = &json_object_boolean_to_json_string;
  235. break;
  236. case json_type_double:
  237. jso->_to_json_string = &json_object_double_to_json_string_default;
  238. break;
  239. case json_type_int:
  240. jso->_to_json_string = &json_object_int_to_json_string;
  241. break;
  242. case json_type_object:
  243. jso->_to_json_string = &json_object_object_to_json_string;
  244. break;
  245. case json_type_array:
  246. jso->_to_json_string = &json_object_array_to_json_string;
  247. break;
  248. case json_type_string:
  249. jso->_to_json_string = &json_object_string_to_json_string;
  250. break;
  251. }
  252. return;
  253. }
  254. jso->_to_json_string = to_string_func;
  255. }
  256. /* extended conversion to string */
  257. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  258. {
  259. const char *r = NULL;
  260. size_t s = 0;
  261. if (!jso)
  262. {
  263. s = 4;
  264. r = "null";
  265. }
  266. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  267. {
  268. printbuf_reset(jso->_pb);
  269. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  270. {
  271. s = (size_t)jso->_pb->bpos;
  272. r = jso->_pb->buf;
  273. }
  274. }
  275. if (length)
  276. *length = s;
  277. return r;
  278. }
  279. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  280. {
  281. return json_object_to_json_string_length(jso, flags, NULL);
  282. }
  283. /* backwards-compatible conversion to string */
  284. const char* json_object_to_json_string(struct json_object *jso)
  285. {
  286. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  287. }
  288. static void indent(struct printbuf *pb, int level, int flags)
  289. {
  290. if (flags & JSON_C_TO_STRING_PRETTY)
  291. {
  292. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  293. {
  294. printbuf_memset(pb, -1, '\t', level);
  295. }
  296. else
  297. {
  298. printbuf_memset(pb, -1, ' ', level * 2);
  299. }
  300. }
  301. }
  302. /* json_object_object */
  303. static int json_object_object_to_json_string(struct json_object* jso,
  304. struct printbuf *pb,
  305. int level,
  306. int flags)
  307. {
  308. int had_children = 0;
  309. struct json_object_iter iter;
  310. sprintbuf(pb, "{" /*}*/);
  311. if (flags & JSON_C_TO_STRING_PRETTY)
  312. sprintbuf(pb, "\n");
  313. json_object_object_foreachC(jso, iter)
  314. {
  315. if (had_children)
  316. {
  317. sprintbuf(pb, ",");
  318. if (flags & JSON_C_TO_STRING_PRETTY)
  319. sprintbuf(pb, "\n");
  320. }
  321. had_children = 1;
  322. if (flags & JSON_C_TO_STRING_SPACED)
  323. sprintbuf(pb, " ");
  324. indent(pb, level+1, flags);
  325. sprintbuf(pb, "\"");
  326. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  327. if (flags & JSON_C_TO_STRING_SPACED)
  328. sprintbuf(pb, "\": ");
  329. else
  330. sprintbuf(pb, "\":");
  331. if(iter.val == NULL)
  332. sprintbuf(pb, "null");
  333. else
  334. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  335. return -1;
  336. }
  337. if (flags & JSON_C_TO_STRING_PRETTY)
  338. {
  339. if (had_children)
  340. sprintbuf(pb, "\n");
  341. indent(pb,level,flags);
  342. }
  343. if (flags & JSON_C_TO_STRING_SPACED)
  344. return sprintbuf(pb, /*{*/ " }");
  345. else
  346. return sprintbuf(pb, /*{*/ "}");
  347. }
  348. static void json_object_lh_entry_free(struct lh_entry *ent)
  349. {
  350. if (!ent->k_is_constant)
  351. free(lh_entry_k(ent));
  352. json_object_put((struct json_object*)lh_entry_v(ent));
  353. }
  354. static void json_object_object_delete(struct json_object* jso)
  355. {
  356. lh_table_free(jso->o.c_object);
  357. json_object_generic_delete(jso);
  358. }
  359. struct json_object* json_object_new_object(void)
  360. {
  361. struct json_object *jso = json_object_new(json_type_object);
  362. if (!jso)
  363. return NULL;
  364. jso->_delete = &json_object_object_delete;
  365. jso->_to_json_string = &json_object_object_to_json_string;
  366. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  367. &json_object_lh_entry_free);
  368. if (!jso->o.c_object)
  369. {
  370. json_object_generic_delete(jso);
  371. errno = ENOMEM;
  372. return NULL;
  373. }
  374. return jso;
  375. }
  376. struct lh_table* json_object_get_object(const struct json_object *jso)
  377. {
  378. if (!jso)
  379. return NULL;
  380. switch(jso->o_type)
  381. {
  382. case json_type_object:
  383. return jso->o.c_object;
  384. default:
  385. return NULL;
  386. }
  387. }
  388. int json_object_object_add_ex(struct json_object* jso,
  389. const char *const key,
  390. struct json_object *const val,
  391. const unsigned opts)
  392. {
  393. // We lookup the entry and replace the value, rather than just deleting
  394. // and re-adding it, so the existing key remains valid.
  395. json_object *existing_value = NULL;
  396. struct lh_entry *existing_entry;
  397. const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
  398. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  399. lh_table_lookup_entry_w_hash(jso->o.c_object,
  400. (const void *)key, hash);
  401. // The caller must avoid creating loops in the object tree, but do a
  402. // quick check anyway to make sure we're not creating a trivial loop.
  403. if (jso == val)
  404. return -1;
  405. if (!existing_entry)
  406. {
  407. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  408. (const void *)key : strdup(key);
  409. if (k == NULL)
  410. return -1;
  411. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  412. }
  413. existing_value = (json_object *) lh_entry_v(existing_entry);
  414. if (existing_value)
  415. json_object_put(existing_value);
  416. existing_entry->v = val;
  417. return 0;
  418. }
  419. int json_object_object_add(struct json_object* jso, const char *key,
  420. struct json_object *val)
  421. {
  422. return json_object_object_add_ex(jso, key, val, 0);
  423. }
  424. int json_object_object_length(const struct json_object *jso)
  425. {
  426. return lh_table_length(jso->o.c_object);
  427. }
  428. struct json_object* json_object_object_get(const struct json_object* jso,
  429. const char *key)
  430. {
  431. struct json_object *result = NULL;
  432. json_object_object_get_ex(jso, key, &result);
  433. return result;
  434. }
  435. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  436. struct json_object **value)
  437. {
  438. if (value != NULL)
  439. *value = NULL;
  440. if (NULL == jso)
  441. return FALSE;
  442. switch(jso->o_type)
  443. {
  444. case json_type_object:
  445. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  446. (void**) value);
  447. default:
  448. if (value != NULL)
  449. *value = NULL;
  450. return FALSE;
  451. }
  452. }
  453. void json_object_object_del(struct json_object* jso, const char *key)
  454. {
  455. lh_table_delete(jso->o.c_object, key);
  456. }
  457. /* json_object_boolean */
  458. static int json_object_boolean_to_json_string(struct json_object* jso,
  459. struct printbuf *pb,
  460. int level,
  461. int flags)
  462. {
  463. if (jso->o.c_boolean)
  464. return sprintbuf(pb, "true");
  465. return sprintbuf(pb, "false");
  466. }
  467. struct json_object* json_object_new_boolean(json_bool b)
  468. {
  469. struct json_object *jso = json_object_new(json_type_boolean);
  470. if (!jso)
  471. return NULL;
  472. jso->_to_json_string = &json_object_boolean_to_json_string;
  473. jso->o.c_boolean = b;
  474. return jso;
  475. }
  476. json_bool json_object_get_boolean(const struct json_object *jso)
  477. {
  478. if (!jso)
  479. return FALSE;
  480. switch(jso->o_type)
  481. {
  482. case json_type_boolean:
  483. return jso->o.c_boolean;
  484. case json_type_int:
  485. return (jso->o.c_int64 != 0);
  486. case json_type_double:
  487. return (jso->o.c_double != 0);
  488. case json_type_string:
  489. return (jso->o.c_string.len != 0);
  490. default:
  491. return FALSE;
  492. }
  493. }
  494. json_bool json_object_set_boolean(struct json_object *jso,json_bool new_value){
  495. if (!jso || jso->o_type!=json_type_boolean)
  496. return FALSE;
  497. jso->o.c_boolean=new_value;
  498. return TRUE;
  499. }
  500. /* json_object_int */
  501. static int json_object_int_to_json_string(struct json_object* jso,
  502. struct printbuf *pb,
  503. int level,
  504. int flags)
  505. {
  506. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  507. }
  508. struct json_object* json_object_new_int(int32_t i)
  509. {
  510. struct json_object *jso = json_object_new(json_type_int);
  511. if (!jso)
  512. return NULL;
  513. jso->_to_json_string = &json_object_int_to_json_string;
  514. jso->o.c_int64 = i;
  515. return jso;
  516. }
  517. int32_t json_object_get_int(const struct json_object *jso)
  518. {
  519. int64_t cint64;
  520. enum json_type o_type;
  521. if(!jso) return 0;
  522. o_type = jso->o_type;
  523. cint64 = jso->o.c_int64;
  524. if (o_type == json_type_string)
  525. {
  526. /*
  527. * Parse strings into 64-bit numbers, then use the
  528. * 64-to-32-bit number handling below.
  529. */
  530. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  531. return 0; /* whoops, it didn't work. */
  532. o_type = json_type_int;
  533. }
  534. switch(o_type) {
  535. case json_type_int:
  536. /* Make sure we return the correct values for out of range numbers. */
  537. if (cint64 <= INT32_MIN)
  538. return INT32_MIN;
  539. if (cint64 >= INT32_MAX)
  540. return INT32_MAX;
  541. return (int32_t) cint64;
  542. case json_type_double:
  543. return (int32_t)jso->o.c_double;
  544. case json_type_boolean:
  545. return jso->o.c_boolean;
  546. default:
  547. return 0;
  548. }
  549. }
  550. struct json_object* json_object_new_int64(int64_t i)
  551. {
  552. struct json_object *jso = json_object_new(json_type_int);
  553. if (!jso)
  554. return NULL;
  555. jso->_to_json_string = &json_object_int_to_json_string;
  556. jso->o.c_int64 = i;
  557. return jso;
  558. }
  559. int64_t json_object_get_int64(const struct json_object *jso)
  560. {
  561. int64_t cint;
  562. if (!jso)
  563. return 0;
  564. switch(jso->o_type)
  565. {
  566. case json_type_int:
  567. return jso->o.c_int64;
  568. case json_type_double:
  569. return (int64_t)jso->o.c_double;
  570. case json_type_boolean:
  571. return jso->o.c_boolean;
  572. case json_type_string:
  573. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  574. return cint;
  575. default:
  576. return 0;
  577. }
  578. }
  579. /* json_object_double */
  580. static int json_object_double_to_json_string_format(struct json_object* jso,
  581. struct printbuf *pb,
  582. int level,
  583. int flags,
  584. const char *format)
  585. {
  586. char buf[128], *p, *q;
  587. int size;
  588. /* Although JSON RFC does not support
  589. NaN or Infinity as numeric values
  590. ECMA 262 section 9.8.1 defines
  591. how to handle these cases as strings */
  592. if(isnan(jso->o.c_double))
  593. size = snprintf(buf, sizeof(buf), "NaN");
  594. else if(isinf(jso->o.c_double))
  595. if(jso->o.c_double > 0)
  596. size = snprintf(buf, sizeof(buf), "Infinity");
  597. else
  598. size = snprintf(buf, sizeof(buf), "-Infinity");
  599. else
  600. size = snprintf(buf, sizeof(buf),
  601. format ? format : "%.17g", jso->o.c_double);
  602. p = strchr(buf, ',');
  603. if (p) {
  604. *p = '.';
  605. } else {
  606. p = strchr(buf, '.');
  607. }
  608. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  609. /* last useful digit, always keep 1 zero */
  610. p++;
  611. for (q=p ; *q ; q++) {
  612. if (*q!='0') p=q;
  613. }
  614. /* drop trailing zeroes */
  615. *(++p) = 0;
  616. size = p-buf;
  617. }
  618. printbuf_memappend(pb, buf, size);
  619. return size;
  620. }
  621. static int json_object_double_to_json_string_default(struct json_object* jso,
  622. struct printbuf *pb,
  623. int level,
  624. int flags)
  625. {
  626. return json_object_double_to_json_string_format(jso, pb, level, flags,
  627. NULL);
  628. }
  629. int json_object_double_to_json_string(struct json_object* jso,
  630. struct printbuf *pb,
  631. int level,
  632. int flags)
  633. {
  634. return json_object_double_to_json_string_format(jso, pb, level, flags,
  635. jso->_userdata);
  636. }
  637. struct json_object* json_object_new_double(double d)
  638. {
  639. struct json_object *jso = json_object_new(json_type_double);
  640. if (!jso)
  641. return NULL;
  642. jso->_to_json_string = &json_object_double_to_json_string_default;
  643. jso->o.c_double = d;
  644. return jso;
  645. }
  646. struct json_object* json_object_new_double_s(double d, const char *ds)
  647. {
  648. struct json_object *jso = json_object_new_double(d);
  649. if (!jso)
  650. return NULL;
  651. char *new_ds = strdup(ds);
  652. if (!new_ds)
  653. {
  654. json_object_generic_delete(jso);
  655. errno = ENOMEM;
  656. return NULL;
  657. }
  658. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  659. new_ds, json_object_free_userdata);
  660. return jso;
  661. }
  662. int json_object_userdata_to_json_string(struct json_object *jso,
  663. struct printbuf *pb, int level, int flags)
  664. {
  665. int userdata_len = strlen((const char *)jso->_userdata);
  666. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  667. return userdata_len;
  668. }
  669. void json_object_free_userdata(struct json_object *jso, void *userdata)
  670. {
  671. free(userdata);
  672. }
  673. double json_object_get_double(const struct json_object *jso)
  674. {
  675. double cdouble;
  676. char *errPtr = NULL;
  677. if(!jso) return 0.0;
  678. switch(jso->o_type) {
  679. case json_type_double:
  680. return jso->o.c_double;
  681. case json_type_int:
  682. return jso->o.c_int64;
  683. case json_type_boolean:
  684. return jso->o.c_boolean;
  685. case json_type_string:
  686. errno = 0;
  687. cdouble = strtod(get_string_component(jso), &errPtr);
  688. /* if conversion stopped at the first character, return 0.0 */
  689. if (errPtr == get_string_component(jso))
  690. return 0.0;
  691. /*
  692. * Check that the conversion terminated on something sensible
  693. *
  694. * For example, { "pay" : 123AB } would parse as 123.
  695. */
  696. if (*errPtr != '\0')
  697. return 0.0;
  698. /*
  699. * If strtod encounters a string which would exceed the
  700. * capacity of a double, it returns +/- HUGE_VAL and sets
  701. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  702. * from a conversion, so we need to check errno.
  703. *
  704. * Underflow also sets errno to ERANGE, but it returns 0 in
  705. * that case, which is what we will return anyway.
  706. *
  707. * See CERT guideline ERR30-C
  708. */
  709. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  710. (ERANGE == errno))
  711. cdouble = 0.0;
  712. return cdouble;
  713. default:
  714. return 0.0;
  715. }
  716. }
  717. /* json_object_string */
  718. static int json_object_string_to_json_string(struct json_object* jso,
  719. struct printbuf *pb,
  720. int level,
  721. int flags)
  722. {
  723. sprintbuf(pb, "\"");
  724. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  725. sprintbuf(pb, "\"");
  726. return 0;
  727. }
  728. static void json_object_string_delete(struct json_object* jso)
  729. {
  730. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  731. free(jso->o.c_string.str.ptr);
  732. json_object_generic_delete(jso);
  733. }
  734. struct json_object* json_object_new_string(const char *s)
  735. {
  736. struct json_object *jso = json_object_new(json_type_string);
  737. if (!jso)
  738. return NULL;
  739. jso->_delete = &json_object_string_delete;
  740. jso->_to_json_string = &json_object_string_to_json_string;
  741. jso->o.c_string.len = strlen(s);
  742. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  743. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  744. } else {
  745. jso->o.c_string.str.ptr = strdup(s);
  746. if (!jso->o.c_string.str.ptr)
  747. {
  748. json_object_generic_delete(jso);
  749. errno = ENOMEM;
  750. return NULL;
  751. }
  752. }
  753. return jso;
  754. }
  755. struct json_object* json_object_new_string_len(const char *s, int len)
  756. {
  757. char *dstbuf;
  758. struct json_object *jso = json_object_new(json_type_string);
  759. if (!jso)
  760. return NULL;
  761. jso->_delete = &json_object_string_delete;
  762. jso->_to_json_string = &json_object_string_to_json_string;
  763. if(len < LEN_DIRECT_STRING_DATA) {
  764. dstbuf = jso->o.c_string.str.data;
  765. } else {
  766. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  767. if (!jso->o.c_string.str.ptr)
  768. {
  769. json_object_generic_delete(jso);
  770. errno = ENOMEM;
  771. return NULL;
  772. }
  773. dstbuf = jso->o.c_string.str.ptr;
  774. }
  775. memcpy(dstbuf, (const void *)s, len);
  776. dstbuf[len] = '\0';
  777. jso->o.c_string.len = len;
  778. return jso;
  779. }
  780. const char* json_object_get_string(struct json_object *jso)
  781. {
  782. if (!jso)
  783. return NULL;
  784. switch(jso->o_type)
  785. {
  786. case json_type_string:
  787. return get_string_component(jso);
  788. default:
  789. return json_object_to_json_string(jso);
  790. }
  791. }
  792. int json_object_get_string_len(const struct json_object *jso)
  793. {
  794. if (!jso)
  795. return 0;
  796. switch(jso->o_type)
  797. {
  798. case json_type_string:
  799. return jso->o.c_string.len;
  800. default:
  801. return 0;
  802. }
  803. }
  804. /* json_object_array */
  805. static int json_object_array_to_json_string(struct json_object* jso,
  806. struct printbuf *pb,
  807. int level,
  808. int flags)
  809. {
  810. int had_children = 0;
  811. size_t ii;
  812. sprintbuf(pb, "[");
  813. if (flags & JSON_C_TO_STRING_PRETTY)
  814. sprintbuf(pb, "\n");
  815. for(ii=0; ii < json_object_array_length(jso); ii++)
  816. {
  817. struct json_object *val;
  818. if (had_children)
  819. {
  820. sprintbuf(pb, ",");
  821. if (flags & JSON_C_TO_STRING_PRETTY)
  822. sprintbuf(pb, "\n");
  823. }
  824. had_children = 1;
  825. if (flags & JSON_C_TO_STRING_SPACED)
  826. sprintbuf(pb, " ");
  827. indent(pb, level + 1, flags);
  828. val = json_object_array_get_idx(jso, ii);
  829. if(val == NULL)
  830. sprintbuf(pb, "null");
  831. else
  832. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  833. return -1;
  834. }
  835. if (flags & JSON_C_TO_STRING_PRETTY)
  836. {
  837. if (had_children)
  838. sprintbuf(pb, "\n");
  839. indent(pb,level,flags);
  840. }
  841. if (flags & JSON_C_TO_STRING_SPACED)
  842. return sprintbuf(pb, " ]");
  843. return sprintbuf(pb, "]");
  844. }
  845. static void json_object_array_entry_free(void *data)
  846. {
  847. json_object_put((struct json_object*)data);
  848. }
  849. static void json_object_array_delete(struct json_object* jso)
  850. {
  851. array_list_free(jso->o.c_array);
  852. json_object_generic_delete(jso);
  853. }
  854. struct json_object* json_object_new_array(void)
  855. {
  856. struct json_object *jso = json_object_new(json_type_array);
  857. if (!jso)
  858. return NULL;
  859. jso->_delete = &json_object_array_delete;
  860. jso->_to_json_string = &json_object_array_to_json_string;
  861. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  862. if(jso->o.c_array == NULL)
  863. {
  864. free(jso);
  865. return NULL;
  866. }
  867. return jso;
  868. }
  869. struct array_list* json_object_get_array(const struct json_object *jso)
  870. {
  871. if (!jso)
  872. return NULL;
  873. switch(jso->o_type)
  874. {
  875. case json_type_array:
  876. return jso->o.c_array;
  877. default:
  878. return NULL;
  879. }
  880. }
  881. void json_object_array_sort(struct json_object *jso,
  882. int(*sort_fn)(const void *, const void *))
  883. {
  884. array_list_sort(jso->o.c_array, sort_fn);
  885. }
  886. struct json_object* json_object_array_bsearch(
  887. const struct json_object *key,
  888. const struct json_object *jso,
  889. int (*sort_fn)(const void *, const void *))
  890. {
  891. struct json_object **result;
  892. result = (struct json_object **)array_list_bsearch(
  893. (const void **)&key, jso->o.c_array, sort_fn);
  894. if (!result)
  895. return NULL;
  896. return *result;
  897. }
  898. size_t json_object_array_length(const struct json_object *jso)
  899. {
  900. return array_list_length(jso->o.c_array);
  901. }
  902. int json_object_array_add(struct json_object *jso,struct json_object *val)
  903. {
  904. return array_list_add(jso->o.c_array, val);
  905. }
  906. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  907. struct json_object *val)
  908. {
  909. return array_list_put_idx(jso->o.c_array, idx, val);
  910. }
  911. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  912. size_t idx)
  913. {
  914. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  915. }
  916. static int json_array_equal(struct json_object* jso1,
  917. struct json_object* jso2)
  918. {
  919. size_t len, i;
  920. len = json_object_array_length(jso1);
  921. if (len != json_object_array_length(jso2))
  922. return 0;
  923. for (i = 0; i < len; i++) {
  924. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  925. json_object_array_get_idx(jso2, i)))
  926. return 0;
  927. }
  928. return 1;
  929. }
  930. static int json_object_all_values_equal(struct json_object* jso1,
  931. struct json_object* jso2)
  932. {
  933. struct json_object_iter iter;
  934. struct json_object *sub;
  935. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  936. json_object_object_foreachC(jso1, iter) {
  937. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  938. (void**)&sub))
  939. return 0;
  940. if (!json_object_equal(iter.val, sub))
  941. return 0;
  942. }
  943. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  944. json_object_object_foreachC(jso2, iter) {
  945. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  946. (void**)&sub))
  947. return 0;
  948. }
  949. return 1;
  950. }
  951. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  952. {
  953. if (jso1 == jso2)
  954. return 1;
  955. if (!jso1 || !jso2)
  956. return 0;
  957. if (jso1->o_type != jso2->o_type)
  958. return 0;
  959. switch(jso1->o_type) {
  960. case json_type_boolean:
  961. return (jso1->o.c_boolean == jso2->o.c_boolean);
  962. case json_type_double:
  963. return (jso1->o.c_double == jso2->o.c_double);
  964. case json_type_int:
  965. return (jso1->o.c_int64 == jso2->o.c_int64);
  966. case json_type_string:
  967. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  968. memcmp(get_string_component(jso1),
  969. get_string_component(jso2),
  970. jso1->o.c_string.len) == 0);
  971. case json_type_object:
  972. return json_object_all_values_equal(jso1, jso2);
  973. case json_type_array:
  974. return json_array_equal(jso1, jso2);
  975. case json_type_null:
  976. return 1;
  977. };
  978. return 0;
  979. }
  980. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  981. {
  982. return array_list_del_idx(jso->o.c_array, idx, count);
  983. }