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

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