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

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