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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 NULL;
  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_ext(struct json_object *jso, int flags)
  258. {
  259. if (!jso)
  260. return "null";
  261. if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
  262. return NULL;
  263. printbuf_reset(jso->_pb);
  264. if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
  265. return NULL;
  266. return jso->_pb->buf;
  267. }
  268. /* backwards-compatible conversion to string */
  269. const char* json_object_to_json_string(struct json_object *jso)
  270. {
  271. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  272. }
  273. static void indent(struct printbuf *pb, int level, int flags)
  274. {
  275. if (flags & JSON_C_TO_STRING_PRETTY)
  276. {
  277. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  278. {
  279. printbuf_memset(pb, -1, '\t', level);
  280. }
  281. else
  282. {
  283. printbuf_memset(pb, -1, ' ', level * 2);
  284. }
  285. }
  286. }
  287. /* json_object_object */
  288. static int json_object_object_to_json_string(struct json_object* jso,
  289. struct printbuf *pb,
  290. int level,
  291. int flags)
  292. {
  293. int had_children = 0;
  294. struct json_object_iter iter;
  295. sprintbuf(pb, "{" /*}*/);
  296. if (flags & JSON_C_TO_STRING_PRETTY)
  297. sprintbuf(pb, "\n");
  298. json_object_object_foreachC(jso, iter)
  299. {
  300. if (had_children)
  301. {
  302. sprintbuf(pb, ",");
  303. if (flags & JSON_C_TO_STRING_PRETTY)
  304. sprintbuf(pb, "\n");
  305. }
  306. had_children = 1;
  307. if (flags & JSON_C_TO_STRING_SPACED)
  308. sprintbuf(pb, " ");
  309. indent(pb, level+1, flags);
  310. sprintbuf(pb, "\"");
  311. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  312. if (flags & JSON_C_TO_STRING_SPACED)
  313. sprintbuf(pb, "\": ");
  314. else
  315. sprintbuf(pb, "\":");
  316. if(iter.val == NULL)
  317. sprintbuf(pb, "null");
  318. else
  319. iter.val->_to_json_string(iter.val, pb, level+1,flags);
  320. }
  321. if (flags & JSON_C_TO_STRING_PRETTY)
  322. {
  323. if (had_children)
  324. sprintbuf(pb, "\n");
  325. indent(pb,level,flags);
  326. }
  327. if (flags & JSON_C_TO_STRING_SPACED)
  328. return sprintbuf(pb, /*{*/ " }");
  329. else
  330. return sprintbuf(pb, /*{*/ "}");
  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,
  384. (const void *)key, hash);
  385. if (!existing_entry)
  386. {
  387. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  388. (const void *)key : strdup(key);
  389. lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  390. return;
  391. }
  392. existing_value = (json_object *) lh_entry_v(existing_entry);
  393. if (existing_value)
  394. json_object_put(existing_value);
  395. existing_entry->v = val;
  396. }
  397. int json_object_object_add(struct json_object* jso, const char *key,
  398. struct json_object *val)
  399. {
  400. // We lookup the entry and replace the value, rather than just deleting
  401. // and re-adding it, so the existing key remains valid.
  402. json_object *existing_value = NULL;
  403. struct lh_entry *existing_entry;
  404. const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
  405. existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object,
  406. (const void *)key, hash);
  407. if (!existing_entry)
  408. {
  409. char *keydup = strdup(key);
  410. if (keydup == NULL)
  411. return -1;
  412. return lh_table_insert_w_hash(jso->o.c_object, keydup, val, hash, 0);
  413. }
  414. existing_value = (json_object *)lh_entry_v(existing_entry);
  415. if (existing_value)
  416. json_object_put(existing_value);
  417. existing_entry->v = val;
  418. return 0;
  419. }
  420. int json_object_object_length(const struct json_object *jso)
  421. {
  422. return lh_table_length(jso->o.c_object);
  423. }
  424. struct json_object* json_object_object_get(const struct json_object* jso,
  425. const char *key)
  426. {
  427. struct json_object *result = NULL;
  428. json_object_object_get_ex(jso, key, &result);
  429. return result;
  430. }
  431. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  432. struct json_object **value)
  433. {
  434. if (value != NULL)
  435. *value = NULL;
  436. if (NULL == jso)
  437. return FALSE;
  438. switch(jso->o_type)
  439. {
  440. case json_type_object:
  441. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  442. (void**) value);
  443. default:
  444. if (value != NULL)
  445. *value = NULL;
  446. return FALSE;
  447. }
  448. }
  449. void json_object_object_del(struct json_object* jso, const char *key)
  450. {
  451. lh_table_delete(jso->o.c_object, key);
  452. }
  453. /* json_object_boolean */
  454. static int json_object_boolean_to_json_string(struct json_object* jso,
  455. struct printbuf *pb,
  456. int level,
  457. int flags)
  458. {
  459. if (jso->o.c_boolean)
  460. return sprintbuf(pb, "true");
  461. return sprintbuf(pb, "false");
  462. }
  463. struct json_object* json_object_new_boolean(json_bool b)
  464. {
  465. struct json_object *jso = json_object_new(json_type_boolean);
  466. if (!jso)
  467. return NULL;
  468. jso->_to_json_string = &json_object_boolean_to_json_string;
  469. jso->o.c_boolean = b;
  470. return jso;
  471. }
  472. json_bool json_object_get_boolean(const struct json_object *jso)
  473. {
  474. if (!jso)
  475. return FALSE;
  476. switch(jso->o_type)
  477. {
  478. case json_type_boolean:
  479. return jso->o.c_boolean;
  480. case json_type_int:
  481. return (jso->o.c_int64 != 0);
  482. case json_type_double:
  483. return (jso->o.c_double != 0);
  484. case json_type_string:
  485. return (jso->o.c_string.len != 0);
  486. default:
  487. return FALSE;
  488. }
  489. }
  490. /* json_object_int */
  491. static int json_object_int_to_json_string(struct json_object* jso,
  492. struct printbuf *pb,
  493. int level,
  494. int flags)
  495. {
  496. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  497. }
  498. struct json_object* json_object_new_int(int32_t i)
  499. {
  500. struct json_object *jso = json_object_new(json_type_int);
  501. if (!jso)
  502. return NULL;
  503. jso->_to_json_string = &json_object_int_to_json_string;
  504. jso->o.c_int64 = i;
  505. return jso;
  506. }
  507. int32_t json_object_get_int(const struct json_object *jso)
  508. {
  509. int64_t cint64;
  510. enum json_type o_type;
  511. if(!jso) return 0;
  512. o_type = jso->o_type;
  513. cint64 = jso->o.c_int64;
  514. if (o_type == json_type_string)
  515. {
  516. /*
  517. * Parse strings into 64-bit numbers, then use the
  518. * 64-to-32-bit number handling below.
  519. */
  520. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  521. return 0; /* whoops, it didn't work. */
  522. o_type = json_type_int;
  523. }
  524. switch(o_type) {
  525. case json_type_int:
  526. /* Make sure we return the correct values for out of range numbers. */
  527. if (cint64 <= INT32_MIN)
  528. return INT32_MIN;
  529. if (cint64 >= INT32_MAX)
  530. return INT32_MAX;
  531. return (int32_t) cint64;
  532. case json_type_double:
  533. return (int32_t)jso->o.c_double;
  534. case json_type_boolean:
  535. return jso->o.c_boolean;
  536. default:
  537. return 0;
  538. }
  539. }
  540. struct json_object* json_object_new_int64(int64_t i)
  541. {
  542. struct json_object *jso = json_object_new(json_type_int);
  543. if (!jso)
  544. return NULL;
  545. jso->_to_json_string = &json_object_int_to_json_string;
  546. jso->o.c_int64 = i;
  547. return jso;
  548. }
  549. int64_t json_object_get_int64(const struct json_object *jso)
  550. {
  551. int64_t cint;
  552. if (!jso)
  553. return 0;
  554. switch(jso->o_type)
  555. {
  556. case json_type_int:
  557. return jso->o.c_int64;
  558. case json_type_double:
  559. return (int64_t)jso->o.c_double;
  560. case json_type_boolean:
  561. return jso->o.c_boolean;
  562. case json_type_string:
  563. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  564. return cint;
  565. default:
  566. return 0;
  567. }
  568. }
  569. /* json_object_double */
  570. static int json_object_double_to_json_string_format(struct json_object* jso,
  571. struct printbuf *pb,
  572. int level,
  573. int flags,
  574. const char *format)
  575. {
  576. char buf[128], *p, *q;
  577. int size;
  578. /* Although JSON RFC does not support
  579. NaN or Infinity as numeric values
  580. ECMA 262 section 9.8.1 defines
  581. how to handle these cases as strings */
  582. if(isnan(jso->o.c_double))
  583. size = snprintf(buf, sizeof(buf), "NaN");
  584. else if(isinf(jso->o.c_double))
  585. if(jso->o.c_double > 0)
  586. size = snprintf(buf, sizeof(buf), "Infinity");
  587. else
  588. size = snprintf(buf, sizeof(buf), "-Infinity");
  589. else
  590. size = snprintf(buf, sizeof(buf),
  591. format ? format : "%.17g", jso->o.c_double);
  592. p = strchr(buf, ',');
  593. if (p) {
  594. *p = '.';
  595. } else {
  596. p = strchr(buf, '.');
  597. }
  598. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  599. /* last useful digit, always keep 1 zero */
  600. p++;
  601. for (q=p ; *q ; q++) {
  602. if (*q!='0') p=q;
  603. }
  604. /* drop trailing zeroes */
  605. *(++p) = 0;
  606. size = p-buf;
  607. }
  608. printbuf_memappend(pb, buf, size);
  609. return size;
  610. }
  611. static int json_object_double_to_json_string_default(struct json_object* jso,
  612. struct printbuf *pb,
  613. int level,
  614. int flags)
  615. {
  616. return json_object_double_to_json_string_format(jso, pb, level, flags,
  617. NULL);
  618. }
  619. int json_object_double_to_json_string(struct json_object* jso,
  620. struct printbuf *pb,
  621. int level,
  622. int flags)
  623. {
  624. return json_object_double_to_json_string_format(jso, pb, level, flags,
  625. jso->_userdata);
  626. }
  627. struct json_object* json_object_new_double(double d)
  628. {
  629. struct json_object *jso = json_object_new(json_type_double);
  630. if (!jso)
  631. return NULL;
  632. jso->_to_json_string = &json_object_double_to_json_string_default;
  633. jso->o.c_double = d;
  634. return jso;
  635. }
  636. struct json_object* json_object_new_double_s(double d, const char *ds)
  637. {
  638. struct json_object *jso = json_object_new_double(d);
  639. if (!jso)
  640. return NULL;
  641. char *new_ds = strdup(ds);
  642. if (!new_ds)
  643. {
  644. json_object_generic_delete(jso);
  645. errno = ENOMEM;
  646. return NULL;
  647. }
  648. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  649. new_ds, json_object_free_userdata);
  650. return jso;
  651. }
  652. int json_object_userdata_to_json_string(struct json_object *jso,
  653. struct printbuf *pb, int level, int flags)
  654. {
  655. int userdata_len = strlen((const char *)jso->_userdata);
  656. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  657. return userdata_len;
  658. }
  659. void json_object_free_userdata(struct json_object *jso, void *userdata)
  660. {
  661. free(userdata);
  662. }
  663. double json_object_get_double(const struct json_object *jso)
  664. {
  665. double cdouble;
  666. char *errPtr = NULL;
  667. if(!jso) return 0.0;
  668. switch(jso->o_type) {
  669. case json_type_double:
  670. return jso->o.c_double;
  671. case json_type_int:
  672. return jso->o.c_int64;
  673. case json_type_boolean:
  674. return jso->o.c_boolean;
  675. case json_type_string:
  676. errno = 0;
  677. cdouble = strtod(get_string_component(jso), &errPtr);
  678. /* if conversion stopped at the first character, return 0.0 */
  679. if (errPtr == get_string_component(jso))
  680. return 0.0;
  681. /*
  682. * Check that the conversion terminated on something sensible
  683. *
  684. * For example, { "pay" : 123AB } would parse as 123.
  685. */
  686. if (*errPtr != '\0')
  687. return 0.0;
  688. /*
  689. * If strtod encounters a string which would exceed the
  690. * capacity of a double, it returns +/- HUGE_VAL and sets
  691. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  692. * from a conversion, so we need to check errno.
  693. *
  694. * Underflow also sets errno to ERANGE, but it returns 0 in
  695. * that case, which is what we will return anyway.
  696. *
  697. * See CERT guideline ERR30-C
  698. */
  699. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  700. (ERANGE == errno))
  701. cdouble = 0.0;
  702. return cdouble;
  703. default:
  704. return 0.0;
  705. }
  706. }
  707. /* json_object_string */
  708. static int json_object_string_to_json_string(struct json_object* jso,
  709. struct printbuf *pb,
  710. int level,
  711. int flags)
  712. {
  713. sprintbuf(pb, "\"");
  714. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  715. sprintbuf(pb, "\"");
  716. return 0;
  717. }
  718. static void json_object_string_delete(struct json_object* jso)
  719. {
  720. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  721. free(jso->o.c_string.str.ptr);
  722. json_object_generic_delete(jso);
  723. }
  724. struct json_object* json_object_new_string(const char *s)
  725. {
  726. struct json_object *jso = json_object_new(json_type_string);
  727. if (!jso)
  728. return NULL;
  729. jso->_delete = &json_object_string_delete;
  730. jso->_to_json_string = &json_object_string_to_json_string;
  731. jso->o.c_string.len = strlen(s);
  732. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  733. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  734. } else {
  735. jso->o.c_string.str.ptr = strdup(s);
  736. if (!jso->o.c_string.str.ptr)
  737. {
  738. json_object_generic_delete(jso);
  739. errno = ENOMEM;
  740. return NULL;
  741. }
  742. }
  743. return jso;
  744. }
  745. struct json_object* json_object_new_string_len(const char *s, int len)
  746. {
  747. char *dstbuf;
  748. struct json_object *jso = json_object_new(json_type_string);
  749. if (!jso)
  750. return NULL;
  751. jso->_delete = &json_object_string_delete;
  752. jso->_to_json_string = &json_object_string_to_json_string;
  753. if(len < LEN_DIRECT_STRING_DATA) {
  754. dstbuf = jso->o.c_string.str.data;
  755. } else {
  756. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  757. if (!jso->o.c_string.str.ptr)
  758. {
  759. json_object_generic_delete(jso);
  760. errno = ENOMEM;
  761. return NULL;
  762. }
  763. dstbuf = jso->o.c_string.str.ptr;
  764. }
  765. memcpy(dstbuf, (const void *)s, len);
  766. dstbuf[len] = '\0';
  767. jso->o.c_string.len = len;
  768. return jso;
  769. }
  770. const char* json_object_get_string(struct json_object *jso)
  771. {
  772. if (!jso)
  773. return NULL;
  774. switch(jso->o_type)
  775. {
  776. case json_type_string:
  777. return get_string_component(jso);
  778. default:
  779. return json_object_to_json_string(jso);
  780. }
  781. }
  782. int json_object_get_string_len(const struct json_object *jso)
  783. {
  784. if (!jso)
  785. return 0;
  786. switch(jso->o_type)
  787. {
  788. case json_type_string:
  789. return jso->o.c_string.len;
  790. default:
  791. return 0;
  792. }
  793. }
  794. /* json_object_array */
  795. static int json_object_array_to_json_string(struct json_object* jso,
  796. struct printbuf *pb,
  797. int level,
  798. int flags)
  799. {
  800. int had_children = 0;
  801. size_t ii;
  802. sprintbuf(pb, "[");
  803. if (flags & JSON_C_TO_STRING_PRETTY)
  804. sprintbuf(pb, "\n");
  805. for(ii=0; ii < json_object_array_length(jso); ii++)
  806. {
  807. struct json_object *val;
  808. if (had_children)
  809. {
  810. sprintbuf(pb, ",");
  811. if (flags & JSON_C_TO_STRING_PRETTY)
  812. sprintbuf(pb, "\n");
  813. }
  814. had_children = 1;
  815. if (flags & JSON_C_TO_STRING_SPACED)
  816. sprintbuf(pb, " ");
  817. indent(pb, level + 1, flags);
  818. val = json_object_array_get_idx(jso, ii);
  819. if(val == NULL)
  820. sprintbuf(pb, "null");
  821. else
  822. val->_to_json_string(val, pb, level+1, flags);
  823. }
  824. if (flags & JSON_C_TO_STRING_PRETTY)
  825. {
  826. if (had_children)
  827. sprintbuf(pb, "\n");
  828. indent(pb,level,flags);
  829. }
  830. if (flags & JSON_C_TO_STRING_SPACED)
  831. return sprintbuf(pb, " ]");
  832. return sprintbuf(pb, "]");
  833. }
  834. static void json_object_array_entry_free(void *data)
  835. {
  836. json_object_put((struct json_object*)data);
  837. }
  838. static void json_object_array_delete(struct json_object* jso)
  839. {
  840. array_list_free(jso->o.c_array);
  841. json_object_generic_delete(jso);
  842. }
  843. struct json_object* json_object_new_array(void)
  844. {
  845. struct json_object *jso = json_object_new(json_type_array);
  846. if (!jso)
  847. return NULL;
  848. jso->_delete = &json_object_array_delete;
  849. jso->_to_json_string = &json_object_array_to_json_string;
  850. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  851. if(jso->o.c_array == NULL)
  852. {
  853. free(jso);
  854. return NULL;
  855. }
  856. return jso;
  857. }
  858. struct array_list* json_object_get_array(const struct json_object *jso)
  859. {
  860. if (!jso)
  861. return NULL;
  862. switch(jso->o_type)
  863. {
  864. case json_type_array:
  865. return jso->o.c_array;
  866. default:
  867. return NULL;
  868. }
  869. }
  870. void json_object_array_sort(struct json_object *jso,
  871. int(*sort_fn)(const void *, const void *))
  872. {
  873. array_list_sort(jso->o.c_array, sort_fn);
  874. }
  875. struct json_object* json_object_array_bsearch(
  876. const struct json_object *key,
  877. const struct json_object *jso,
  878. int (*sort_fn)(const void *, const void *))
  879. {
  880. struct json_object **result;
  881. result = (struct json_object **)array_list_bsearch(
  882. (const void **)&key, jso->o.c_array, sort_fn);
  883. if (!result)
  884. return NULL;
  885. return *result;
  886. }
  887. size_t json_object_array_length(const struct json_object *jso)
  888. {
  889. return array_list_length(jso->o.c_array);
  890. }
  891. int json_object_array_add(struct json_object *jso,struct json_object *val)
  892. {
  893. return array_list_add(jso->o.c_array, val);
  894. }
  895. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  896. struct json_object *val)
  897. {
  898. return array_list_put_idx(jso->o.c_array, idx, val);
  899. }
  900. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  901. size_t idx)
  902. {
  903. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  904. }
  905. static int json_array_equal(struct json_object* jso1,
  906. struct json_object* jso2)
  907. {
  908. size_t len, i;
  909. len = json_object_array_length(jso1);
  910. if (len != json_object_array_length(jso2))
  911. return 0;
  912. for (i = 0; i < len; i++) {
  913. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  914. json_object_array_get_idx(jso2, i)))
  915. return 0;
  916. }
  917. return 1;
  918. }
  919. static int json_object_all_values_equal(struct json_object* jso1,
  920. struct json_object* jso2)
  921. {
  922. struct json_object_iter iter;
  923. struct json_object *sub;
  924. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  925. json_object_object_foreachC(jso1, iter) {
  926. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  927. (void**)&sub))
  928. return 0;
  929. if (!json_object_equal(iter.val, sub))
  930. return 0;
  931. }
  932. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  933. json_object_object_foreachC(jso2, iter) {
  934. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  935. (void**)&sub))
  936. return 0;
  937. }
  938. return 1;
  939. }
  940. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  941. {
  942. if (jso1 == jso2)
  943. return 1;
  944. if (!jso1 || !jso2)
  945. return 0;
  946. if (jso1->o_type != jso2->o_type)
  947. return 0;
  948. switch(jso1->o_type) {
  949. case json_type_boolean:
  950. return (jso1->o.c_boolean == jso2->o.c_boolean);
  951. case json_type_double:
  952. return (jso1->o.c_double == jso2->o.c_double);
  953. case json_type_int:
  954. return (jso1->o.c_int64 == jso2->o.c_int64);
  955. case json_type_string:
  956. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  957. memcmp(get_string_component(jso1),
  958. get_string_component(jso2),
  959. jso1->o.c_string.len) == 0);
  960. case json_type_object:
  961. return json_object_all_values_equal(jso1, jso2);
  962. case json_type_array:
  963. return json_array_equal(jso1, jso2);
  964. case json_type_null:
  965. return 1;
  966. };
  967. return 0;
  968. }
  969. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  970. {
  971. return array_list_del_idx(jso->o.c_array, idx, count);
  972. }