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

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