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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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_object_int */
  495. static int json_object_int_to_json_string(struct json_object* jso,
  496. struct printbuf *pb,
  497. int level,
  498. int flags)
  499. {
  500. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  501. }
  502. struct json_object* json_object_new_int(int32_t i)
  503. {
  504. struct json_object *jso = json_object_new(json_type_int);
  505. if (!jso)
  506. return NULL;
  507. jso->_to_json_string = &json_object_int_to_json_string;
  508. jso->o.c_int64 = i;
  509. return jso;
  510. }
  511. int32_t json_object_get_int(const struct json_object *jso)
  512. {
  513. int64_t cint64;
  514. enum json_type o_type;
  515. if(!jso) return 0;
  516. o_type = jso->o_type;
  517. cint64 = jso->o.c_int64;
  518. if (o_type == json_type_string)
  519. {
  520. /*
  521. * Parse strings into 64-bit numbers, then use the
  522. * 64-to-32-bit number handling below.
  523. */
  524. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  525. return 0; /* whoops, it didn't work. */
  526. o_type = json_type_int;
  527. }
  528. switch(o_type) {
  529. case json_type_int:
  530. /* Make sure we return the correct values for out of range numbers. */
  531. if (cint64 <= INT32_MIN)
  532. return INT32_MIN;
  533. if (cint64 >= INT32_MAX)
  534. return INT32_MAX;
  535. return (int32_t) cint64;
  536. case json_type_double:
  537. return (int32_t)jso->o.c_double;
  538. case json_type_boolean:
  539. return jso->o.c_boolean;
  540. default:
  541. return 0;
  542. }
  543. }
  544. struct json_object* json_object_new_int64(int64_t i)
  545. {
  546. struct json_object *jso = json_object_new(json_type_int);
  547. if (!jso)
  548. return NULL;
  549. jso->_to_json_string = &json_object_int_to_json_string;
  550. jso->o.c_int64 = i;
  551. return jso;
  552. }
  553. int64_t json_object_get_int64(const struct json_object *jso)
  554. {
  555. int64_t cint;
  556. if (!jso)
  557. return 0;
  558. switch(jso->o_type)
  559. {
  560. case json_type_int:
  561. return jso->o.c_int64;
  562. case json_type_double:
  563. return (int64_t)jso->o.c_double;
  564. case json_type_boolean:
  565. return jso->o.c_boolean;
  566. case json_type_string:
  567. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  568. return cint;
  569. default:
  570. return 0;
  571. }
  572. }
  573. /* json_object_double */
  574. static int json_object_double_to_json_string_format(struct json_object* jso,
  575. struct printbuf *pb,
  576. int level,
  577. int flags,
  578. const char *format)
  579. {
  580. char buf[128], *p, *q;
  581. int size;
  582. /* Although JSON RFC does not support
  583. NaN or Infinity as numeric values
  584. ECMA 262 section 9.8.1 defines
  585. how to handle these cases as strings */
  586. if(isnan(jso->o.c_double))
  587. size = snprintf(buf, sizeof(buf), "NaN");
  588. else if(isinf(jso->o.c_double))
  589. if(jso->o.c_double > 0)
  590. size = snprintf(buf, sizeof(buf), "Infinity");
  591. else
  592. size = snprintf(buf, sizeof(buf), "-Infinity");
  593. else
  594. size = snprintf(buf, sizeof(buf),
  595. format ? format : "%.17g", jso->o.c_double);
  596. p = strchr(buf, ',');
  597. if (p) {
  598. *p = '.';
  599. } else {
  600. p = strchr(buf, '.');
  601. }
  602. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  603. /* last useful digit, always keep 1 zero */
  604. p++;
  605. for (q=p ; *q ; q++) {
  606. if (*q!='0') p=q;
  607. }
  608. /* drop trailing zeroes */
  609. *(++p) = 0;
  610. size = p-buf;
  611. }
  612. printbuf_memappend(pb, buf, size);
  613. return size;
  614. }
  615. static int json_object_double_to_json_string_default(struct json_object* jso,
  616. struct printbuf *pb,
  617. int level,
  618. int flags)
  619. {
  620. return json_object_double_to_json_string_format(jso, pb, level, flags,
  621. NULL);
  622. }
  623. int json_object_double_to_json_string(struct json_object* jso,
  624. struct printbuf *pb,
  625. int level,
  626. int flags)
  627. {
  628. return json_object_double_to_json_string_format(jso, pb, level, flags,
  629. jso->_userdata);
  630. }
  631. struct json_object* json_object_new_double(double d)
  632. {
  633. struct json_object *jso = json_object_new(json_type_double);
  634. if (!jso)
  635. return NULL;
  636. jso->_to_json_string = &json_object_double_to_json_string_default;
  637. jso->o.c_double = d;
  638. return jso;
  639. }
  640. struct json_object* json_object_new_double_s(double d, const char *ds)
  641. {
  642. struct json_object *jso = json_object_new_double(d);
  643. if (!jso)
  644. return NULL;
  645. char *new_ds = strdup(ds);
  646. if (!new_ds)
  647. {
  648. json_object_generic_delete(jso);
  649. errno = ENOMEM;
  650. return NULL;
  651. }
  652. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  653. new_ds, json_object_free_userdata);
  654. return jso;
  655. }
  656. int json_object_userdata_to_json_string(struct json_object *jso,
  657. struct printbuf *pb, int level, int flags)
  658. {
  659. int userdata_len = strlen((const char *)jso->_userdata);
  660. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  661. return userdata_len;
  662. }
  663. void json_object_free_userdata(struct json_object *jso, void *userdata)
  664. {
  665. free(userdata);
  666. }
  667. double json_object_get_double(const struct json_object *jso)
  668. {
  669. double cdouble;
  670. char *errPtr = NULL;
  671. if(!jso) return 0.0;
  672. switch(jso->o_type) {
  673. case json_type_double:
  674. return jso->o.c_double;
  675. case json_type_int:
  676. return jso->o.c_int64;
  677. case json_type_boolean:
  678. return jso->o.c_boolean;
  679. case json_type_string:
  680. errno = 0;
  681. cdouble = strtod(get_string_component(jso), &errPtr);
  682. /* if conversion stopped at the first character, return 0.0 */
  683. if (errPtr == get_string_component(jso))
  684. return 0.0;
  685. /*
  686. * Check that the conversion terminated on something sensible
  687. *
  688. * For example, { "pay" : 123AB } would parse as 123.
  689. */
  690. if (*errPtr != '\0')
  691. return 0.0;
  692. /*
  693. * If strtod encounters a string which would exceed the
  694. * capacity of a double, it returns +/- HUGE_VAL and sets
  695. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  696. * from a conversion, so we need to check errno.
  697. *
  698. * Underflow also sets errno to ERANGE, but it returns 0 in
  699. * that case, which is what we will return anyway.
  700. *
  701. * See CERT guideline ERR30-C
  702. */
  703. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  704. (ERANGE == errno))
  705. cdouble = 0.0;
  706. return cdouble;
  707. default:
  708. return 0.0;
  709. }
  710. }
  711. /* json_object_string */
  712. static int json_object_string_to_json_string(struct json_object* jso,
  713. struct printbuf *pb,
  714. int level,
  715. int flags)
  716. {
  717. sprintbuf(pb, "\"");
  718. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  719. sprintbuf(pb, "\"");
  720. return 0;
  721. }
  722. static void json_object_string_delete(struct json_object* jso)
  723. {
  724. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  725. free(jso->o.c_string.str.ptr);
  726. json_object_generic_delete(jso);
  727. }
  728. struct json_object* json_object_new_string(const char *s)
  729. {
  730. struct json_object *jso = json_object_new(json_type_string);
  731. if (!jso)
  732. return NULL;
  733. jso->_delete = &json_object_string_delete;
  734. jso->_to_json_string = &json_object_string_to_json_string;
  735. jso->o.c_string.len = strlen(s);
  736. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  737. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  738. } else {
  739. jso->o.c_string.str.ptr = strdup(s);
  740. if (!jso->o.c_string.str.ptr)
  741. {
  742. json_object_generic_delete(jso);
  743. errno = ENOMEM;
  744. return NULL;
  745. }
  746. }
  747. return jso;
  748. }
  749. struct json_object* json_object_new_string_len(const char *s, int len)
  750. {
  751. char *dstbuf;
  752. struct json_object *jso = json_object_new(json_type_string);
  753. if (!jso)
  754. return NULL;
  755. jso->_delete = &json_object_string_delete;
  756. jso->_to_json_string = &json_object_string_to_json_string;
  757. if(len < LEN_DIRECT_STRING_DATA) {
  758. dstbuf = jso->o.c_string.str.data;
  759. } else {
  760. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  761. if (!jso->o.c_string.str.ptr)
  762. {
  763. json_object_generic_delete(jso);
  764. errno = ENOMEM;
  765. return NULL;
  766. }
  767. dstbuf = jso->o.c_string.str.ptr;
  768. }
  769. memcpy(dstbuf, (const void *)s, len);
  770. dstbuf[len] = '\0';
  771. jso->o.c_string.len = len;
  772. return jso;
  773. }
  774. const char* json_object_get_string(struct json_object *jso)
  775. {
  776. if (!jso)
  777. return NULL;
  778. switch(jso->o_type)
  779. {
  780. case json_type_string:
  781. return get_string_component(jso);
  782. default:
  783. return json_object_to_json_string(jso);
  784. }
  785. }
  786. int json_object_get_string_len(const struct json_object *jso)
  787. {
  788. if (!jso)
  789. return 0;
  790. switch(jso->o_type)
  791. {
  792. case json_type_string:
  793. return jso->o.c_string.len;
  794. default:
  795. return 0;
  796. }
  797. }
  798. /* json_object_array */
  799. static int json_object_array_to_json_string(struct json_object* jso,
  800. struct printbuf *pb,
  801. int level,
  802. int flags)
  803. {
  804. int had_children = 0;
  805. size_t ii;
  806. sprintbuf(pb, "[");
  807. if (flags & JSON_C_TO_STRING_PRETTY)
  808. sprintbuf(pb, "\n");
  809. for(ii=0; ii < json_object_array_length(jso); ii++)
  810. {
  811. struct json_object *val;
  812. if (had_children)
  813. {
  814. sprintbuf(pb, ",");
  815. if (flags & JSON_C_TO_STRING_PRETTY)
  816. sprintbuf(pb, "\n");
  817. }
  818. had_children = 1;
  819. if (flags & JSON_C_TO_STRING_SPACED)
  820. sprintbuf(pb, " ");
  821. indent(pb, level + 1, flags);
  822. val = json_object_array_get_idx(jso, ii);
  823. if(val == NULL)
  824. sprintbuf(pb, "null");
  825. else
  826. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  827. return -1;
  828. }
  829. if (flags & JSON_C_TO_STRING_PRETTY)
  830. {
  831. if (had_children)
  832. sprintbuf(pb, "\n");
  833. indent(pb,level,flags);
  834. }
  835. if (flags & JSON_C_TO_STRING_SPACED)
  836. return sprintbuf(pb, " ]");
  837. return sprintbuf(pb, "]");
  838. }
  839. static void json_object_array_entry_free(void *data)
  840. {
  841. json_object_put((struct json_object*)data);
  842. }
  843. static void json_object_array_delete(struct json_object* jso)
  844. {
  845. array_list_free(jso->o.c_array);
  846. json_object_generic_delete(jso);
  847. }
  848. struct json_object* json_object_new_array(void)
  849. {
  850. struct json_object *jso = json_object_new(json_type_array);
  851. if (!jso)
  852. return NULL;
  853. jso->_delete = &json_object_array_delete;
  854. jso->_to_json_string = &json_object_array_to_json_string;
  855. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  856. if(jso->o.c_array == NULL)
  857. {
  858. free(jso);
  859. return NULL;
  860. }
  861. return jso;
  862. }
  863. struct array_list* json_object_get_array(const struct json_object *jso)
  864. {
  865. if (!jso)
  866. return NULL;
  867. switch(jso->o_type)
  868. {
  869. case json_type_array:
  870. return jso->o.c_array;
  871. default:
  872. return NULL;
  873. }
  874. }
  875. void json_object_array_sort(struct json_object *jso,
  876. int(*sort_fn)(const void *, const void *))
  877. {
  878. array_list_sort(jso->o.c_array, sort_fn);
  879. }
  880. struct json_object* json_object_array_bsearch(
  881. const struct json_object *key,
  882. const struct json_object *jso,
  883. int (*sort_fn)(const void *, const void *))
  884. {
  885. struct json_object **result;
  886. result = (struct json_object **)array_list_bsearch(
  887. (const void **)&key, jso->o.c_array, sort_fn);
  888. if (!result)
  889. return NULL;
  890. return *result;
  891. }
  892. size_t json_object_array_length(const struct json_object *jso)
  893. {
  894. return array_list_length(jso->o.c_array);
  895. }
  896. int json_object_array_add(struct json_object *jso,struct json_object *val)
  897. {
  898. return array_list_add(jso->o.c_array, val);
  899. }
  900. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  901. struct json_object *val)
  902. {
  903. return array_list_put_idx(jso->o.c_array, idx, val);
  904. }
  905. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  906. size_t idx)
  907. {
  908. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  909. }
  910. static int json_array_equal(struct json_object* jso1,
  911. struct json_object* jso2)
  912. {
  913. size_t len, i;
  914. len = json_object_array_length(jso1);
  915. if (len != json_object_array_length(jso2))
  916. return 0;
  917. for (i = 0; i < len; i++) {
  918. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  919. json_object_array_get_idx(jso2, i)))
  920. return 0;
  921. }
  922. return 1;
  923. }
  924. static int json_object_all_values_equal(struct json_object* jso1,
  925. struct json_object* jso2)
  926. {
  927. struct json_object_iter iter;
  928. struct json_object *sub;
  929. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  930. json_object_object_foreachC(jso1, iter) {
  931. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  932. (void**)&sub))
  933. return 0;
  934. if (!json_object_equal(iter.val, sub))
  935. return 0;
  936. }
  937. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  938. json_object_object_foreachC(jso2, iter) {
  939. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  940. (void**)&sub))
  941. return 0;
  942. }
  943. return 1;
  944. }
  945. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  946. {
  947. if (jso1 == jso2)
  948. return 1;
  949. if (!jso1 || !jso2)
  950. return 0;
  951. if (jso1->o_type != jso2->o_type)
  952. return 0;
  953. switch(jso1->o_type) {
  954. case json_type_boolean:
  955. return (jso1->o.c_boolean == jso2->o.c_boolean);
  956. case json_type_double:
  957. return (jso1->o.c_double == jso2->o.c_double);
  958. case json_type_int:
  959. return (jso1->o.c_int64 == jso2->o.c_int64);
  960. case json_type_string:
  961. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  962. memcmp(get_string_component(jso1),
  963. get_string_component(jso2),
  964. jso1->o.c_string.len) == 0);
  965. case json_type_object:
  966. return json_object_all_values_equal(jso1, jso2);
  967. case json_type_array:
  968. return json_array_equal(jso1, jso2);
  969. case json_type_null:
  970. return 1;
  971. };
  972. return 0;
  973. }
  974. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  975. {
  976. return array_list_del_idx(jso->o.c_array, idx, count);
  977. }