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 29 kB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 <assert.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <errno.h>
  20. #include "debug.h"
  21. #include "printbuf.h"
  22. #include "linkhash.h"
  23. #include "arraylist.h"
  24. #include "json_inttypes.h"
  25. #include "json_object.h"
  26. #include "json_object_private.h"
  27. #include "json_util.h"
  28. #include "math_compat.h"
  29. #include "compat/strdup.h"
  30. #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
  31. /* MSC has the version as _snprintf */
  32. # define snprintf _snprintf
  33. #elif !defined(HAVE_SNPRINTF)
  34. # error You do not have snprintf on your system.
  35. #endif /* HAVE_SNPRINTF */
  36. // Don't define this. It's not thread-safe.
  37. /* #define REFCOUNT_DEBUG 1 */
  38. const char *json_number_chars = "0123456789.+-eE";
  39. const char *json_hex_chars = "0123456789abcdefABCDEF";
  40. static void json_object_generic_delete(struct json_object* jso);
  41. static struct json_object* json_object_new(enum json_type o_type);
  42. static json_object_to_json_string_fn json_object_object_to_json_string;
  43. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  44. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  45. static json_object_to_json_string_fn json_object_int_to_json_string;
  46. static json_object_to_json_string_fn json_object_string_to_json_string;
  47. static json_object_to_json_string_fn json_object_array_to_json_string;
  48. /* ref count debugging */
  49. #ifdef REFCOUNT_DEBUG
  50. static struct lh_table *json_object_table;
  51. static void json_object_init(void) __attribute__ ((constructor));
  52. static void json_object_init(void) {
  53. MC_DEBUG("json_object_init: creating object table\n");
  54. json_object_table = lh_kptr_table_new(128, NULL);
  55. }
  56. static void json_object_fini(void) __attribute__ ((destructor));
  57. static void json_object_fini(void)
  58. {
  59. struct lh_entry *ent;
  60. if (MC_GET_DEBUG())
  61. {
  62. if (json_object_table->count)
  63. {
  64. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  65. json_object_table->count);
  66. lh_foreach(json_object_table, ent)
  67. {
  68. struct json_object* obj =
  69. (struct json_object*) lh_entry_v(ent);
  70. MC_DEBUG("\t%s:%p\n",
  71. json_type_to_name(obj->o_type), obj);
  72. }
  73. }
  74. }
  75. MC_DEBUG("json_object_fini: freeing object table\n");
  76. lh_table_free(json_object_table);
  77. }
  78. #endif /* REFCOUNT_DEBUG */
  79. /* helper for accessing the optimized string data component in json_object
  80. */
  81. static const char *
  82. get_string_component(const struct json_object *jso)
  83. {
  84. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  85. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  86. }
  87. /* string escaping */
  88. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  89. {
  90. int pos = 0, start_offset = 0;
  91. unsigned char c;
  92. while (len--)
  93. {
  94. c = str[pos];
  95. switch(c)
  96. {
  97. case '\b':
  98. case '\n':
  99. case '\r':
  100. case '\t':
  101. case '\f':
  102. case '"':
  103. case '\\':
  104. case '/':
  105. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  106. {
  107. pos++;
  108. break;
  109. }
  110. if(pos - start_offset > 0)
  111. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  112. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  113. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  114. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  115. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  116. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  117. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  118. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  119. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  120. start_offset = ++pos;
  121. break;
  122. default:
  123. if(c < ' ')
  124. {
  125. if(pos - start_offset > 0)
  126. printbuf_memappend(pb,
  127. str + start_offset,
  128. pos - start_offset);
  129. sprintbuf(pb, "\\u00%c%c",
  130. json_hex_chars[c >> 4],
  131. json_hex_chars[c & 0xf]);
  132. start_offset = ++pos;
  133. } else
  134. pos++;
  135. }
  136. }
  137. if (pos - start_offset > 0)
  138. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  139. return 0;
  140. }
  141. /* reference counting */
  142. extern struct json_object* json_object_get(struct json_object *jso)
  143. {
  144. if (jso)
  145. jso->_ref_count++;
  146. return jso;
  147. }
  148. int json_object_put(struct json_object *jso)
  149. {
  150. if(jso)
  151. {
  152. jso->_ref_count--;
  153. if(!jso->_ref_count)
  154. {
  155. if (jso->_user_delete)
  156. jso->_user_delete(jso, jso->_userdata);
  157. jso->_delete(jso);
  158. return 1;
  159. }
  160. }
  161. return 0;
  162. }
  163. /* generic object construction and destruction parts */
  164. static void json_object_generic_delete(struct json_object* jso)
  165. {
  166. #ifdef REFCOUNT_DEBUG
  167. MC_DEBUG("json_object_delete_%s: %p\n",
  168. json_type_to_name(jso->o_type), jso);
  169. lh_table_delete(json_object_table, jso);
  170. #endif /* REFCOUNT_DEBUG */
  171. printbuf_free(jso->_pb);
  172. free(jso);
  173. }
  174. static struct json_object* json_object_new(enum json_type o_type)
  175. {
  176. struct json_object *jso;
  177. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  178. if (!jso)
  179. return NULL;
  180. jso->o_type = o_type;
  181. jso->_ref_count = 1;
  182. jso->_delete = &json_object_generic_delete;
  183. #ifdef REFCOUNT_DEBUG
  184. lh_table_insert(json_object_table, jso, jso);
  185. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  186. #endif /* REFCOUNT_DEBUG */
  187. return jso;
  188. }
  189. /* type checking functions */
  190. int json_object_is_type(const struct json_object *jso, enum json_type type)
  191. {
  192. if (!jso)
  193. return (type == json_type_null);
  194. return (jso->o_type == type);
  195. }
  196. enum json_type json_object_get_type(const struct json_object *jso)
  197. {
  198. if (!jso)
  199. return json_type_null;
  200. return jso->o_type;
  201. }
  202. void* json_object_get_userdata(json_object *jso) {
  203. return jso ? jso->_userdata : NULL;
  204. }
  205. void json_object_set_userdata(json_object *jso, void *userdata,
  206. json_object_delete_fn *user_delete)
  207. {
  208. // Can't return failure, so abort if we can't perform the operation.
  209. assert(jso != NULL);
  210. // First, clean up any previously existing user info
  211. if (jso->_user_delete)
  212. jso->_user_delete(jso, jso->_userdata);
  213. jso->_userdata = userdata;
  214. jso->_user_delete = user_delete;
  215. }
  216. /* set a custom conversion to string */
  217. void json_object_set_serializer(json_object *jso,
  218. json_object_to_json_string_fn to_string_func,
  219. void *userdata,
  220. json_object_delete_fn *user_delete)
  221. {
  222. json_object_set_userdata(jso, userdata, user_delete);
  223. if (to_string_func == NULL)
  224. {
  225. // Reset to the standard serialization function
  226. switch(jso->o_type)
  227. {
  228. case json_type_null:
  229. jso->_to_json_string = NULL;
  230. break;
  231. case json_type_boolean:
  232. jso->_to_json_string = &json_object_boolean_to_json_string;
  233. break;
  234. case json_type_double:
  235. jso->_to_json_string = &json_object_double_to_json_string_default;
  236. break;
  237. case json_type_int:
  238. jso->_to_json_string = &json_object_int_to_json_string;
  239. break;
  240. case json_type_object:
  241. jso->_to_json_string = &json_object_object_to_json_string;
  242. break;
  243. case json_type_array:
  244. jso->_to_json_string = &json_object_array_to_json_string;
  245. break;
  246. case json_type_string:
  247. jso->_to_json_string = &json_object_string_to_json_string;
  248. break;
  249. }
  250. return;
  251. }
  252. jso->_to_json_string = to_string_func;
  253. }
  254. /* extended conversion to string */
  255. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  256. {
  257. const char *r = NULL;
  258. size_t s = 0;
  259. if (!jso)
  260. {
  261. s = 4;
  262. r = "null";
  263. }
  264. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  265. {
  266. printbuf_reset(jso->_pb);
  267. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  268. {
  269. s = (size_t)jso->_pb->bpos;
  270. r = jso->_pb->buf;
  271. }
  272. }
  273. if (length)
  274. *length = s;
  275. return r;
  276. }
  277. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  278. {
  279. return json_object_to_json_string_length(jso, flags, NULL);
  280. }
  281. /* backwards-compatible conversion to string */
  282. const char* json_object_to_json_string(struct json_object *jso)
  283. {
  284. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  285. }
  286. static void indent(struct printbuf *pb, int level, int flags)
  287. {
  288. if (flags & JSON_C_TO_STRING_PRETTY)
  289. {
  290. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  291. {
  292. printbuf_memset(pb, -1, '\t', level);
  293. }
  294. else
  295. {
  296. printbuf_memset(pb, -1, ' ', level * 2);
  297. }
  298. }
  299. }
  300. /* json_object_object */
  301. static int json_object_object_to_json_string(struct json_object* jso,
  302. struct printbuf *pb,
  303. int level,
  304. int flags)
  305. {
  306. int had_children = 0;
  307. struct json_object_iter iter;
  308. sprintbuf(pb, "{" /*}*/);
  309. if (flags & JSON_C_TO_STRING_PRETTY)
  310. sprintbuf(pb, "\n");
  311. json_object_object_foreachC(jso, iter)
  312. {
  313. if (had_children)
  314. {
  315. sprintbuf(pb, ",");
  316. if (flags & JSON_C_TO_STRING_PRETTY)
  317. sprintbuf(pb, "\n");
  318. }
  319. had_children = 1;
  320. if (flags & JSON_C_TO_STRING_SPACED)
  321. sprintbuf(pb, " ");
  322. indent(pb, level+1, flags);
  323. sprintbuf(pb, "\"");
  324. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  325. if (flags & JSON_C_TO_STRING_SPACED)
  326. sprintbuf(pb, "\": ");
  327. else
  328. sprintbuf(pb, "\":");
  329. if(iter.val == NULL)
  330. sprintbuf(pb, "null");
  331. else
  332. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  333. return -1;
  334. }
  335. if (flags & JSON_C_TO_STRING_PRETTY)
  336. {
  337. if (had_children)
  338. sprintbuf(pb, "\n");
  339. indent(pb,level,flags);
  340. }
  341. if (flags & JSON_C_TO_STRING_SPACED)
  342. return sprintbuf(pb, /*{*/ " }");
  343. else
  344. return sprintbuf(pb, /*{*/ "}");
  345. }
  346. static void json_object_lh_entry_free(struct lh_entry *ent)
  347. {
  348. if (!ent->k_is_constant)
  349. free(lh_entry_k(ent));
  350. json_object_put((struct json_object*)lh_entry_v(ent));
  351. }
  352. static void json_object_object_delete(struct json_object* jso)
  353. {
  354. lh_table_free(jso->o.c_object);
  355. json_object_generic_delete(jso);
  356. }
  357. struct json_object* json_object_new_object(void)
  358. {
  359. struct json_object *jso = json_object_new(json_type_object);
  360. if (!jso)
  361. return NULL;
  362. jso->_delete = &json_object_object_delete;
  363. jso->_to_json_string = &json_object_object_to_json_string;
  364. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  365. &json_object_lh_entry_free);
  366. if (!jso->o.c_object)
  367. {
  368. json_object_generic_delete(jso);
  369. errno = ENOMEM;
  370. return NULL;
  371. }
  372. return jso;
  373. }
  374. struct lh_table* json_object_get_object(const struct json_object *jso)
  375. {
  376. if (!jso)
  377. return NULL;
  378. switch(jso->o_type)
  379. {
  380. case json_type_object:
  381. return jso->o.c_object;
  382. default:
  383. return NULL;
  384. }
  385. }
  386. int json_object_object_add_ex(struct json_object* jso,
  387. const char *const key,
  388. struct json_object *const val,
  389. const unsigned opts)
  390. {
  391. assert(json_object_get_type(jso) == json_type_object);
  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. // The caller must avoid creating loops in the object tree, but do a
  401. // quick check anyway to make sure we're not creating a trivial loop.
  402. if (jso == val)
  403. return -1;
  404. if (!existing_entry)
  405. {
  406. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  407. (const void *)key : strdup(key);
  408. if (k == NULL)
  409. return -1;
  410. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  411. }
  412. existing_value = (json_object *) lh_entry_v(existing_entry);
  413. if (existing_value)
  414. json_object_put(existing_value);
  415. existing_entry->v = val;
  416. return 0;
  417. }
  418. int json_object_object_add(struct json_object* jso, const char *key,
  419. struct json_object *val)
  420. {
  421. return json_object_object_add_ex(jso, key, val, 0);
  422. }
  423. int json_object_object_length(const struct json_object *jso)
  424. {
  425. assert(json_object_get_type(jso) == json_type_object);
  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. assert(json_object_get_type(jso) == json_type_object);
  456. lh_table_delete(jso->o.c_object, key);
  457. }
  458. /* json_object_boolean */
  459. static int json_object_boolean_to_json_string(struct json_object* jso,
  460. struct printbuf *pb,
  461. int level,
  462. int flags)
  463. {
  464. if (jso->o.c_boolean)
  465. return sprintbuf(pb, "true");
  466. return sprintbuf(pb, "false");
  467. }
  468. struct json_object* json_object_new_boolean(json_bool b)
  469. {
  470. struct json_object *jso = json_object_new(json_type_boolean);
  471. if (!jso)
  472. return NULL;
  473. jso->_to_json_string = &json_object_boolean_to_json_string;
  474. jso->o.c_boolean = b;
  475. return jso;
  476. }
  477. json_bool json_object_get_boolean(const struct json_object *jso)
  478. {
  479. if (!jso)
  480. return FALSE;
  481. switch(jso->o_type)
  482. {
  483. case json_type_boolean:
  484. return jso->o.c_boolean;
  485. case json_type_int:
  486. return (jso->o.c_int64 != 0);
  487. case json_type_double:
  488. return (jso->o.c_double != 0);
  489. case json_type_string:
  490. return (jso->o.c_string.len != 0);
  491. default:
  492. return FALSE;
  493. }
  494. }
  495. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  496. if (!jso || jso->o_type!=json_type_boolean)
  497. return 0;
  498. jso->o.c_boolean=new_value;
  499. return 1;
  500. }
  501. /* json_object_int */
  502. static int json_object_int_to_json_string(struct json_object* jso,
  503. struct printbuf *pb,
  504. int level,
  505. int flags)
  506. {
  507. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  508. }
  509. struct json_object* json_object_new_int(int32_t i)
  510. {
  511. struct json_object *jso = json_object_new(json_type_int);
  512. if (!jso)
  513. return NULL;
  514. jso->_to_json_string = &json_object_int_to_json_string;
  515. jso->o.c_int64 = i;
  516. return jso;
  517. }
  518. int32_t json_object_get_int(const struct json_object *jso)
  519. {
  520. int64_t cint64;
  521. enum json_type o_type;
  522. if(!jso) return 0;
  523. o_type = jso->o_type;
  524. cint64 = jso->o.c_int64;
  525. if (o_type == json_type_string)
  526. {
  527. /*
  528. * Parse strings into 64-bit numbers, then use the
  529. * 64-to-32-bit number handling below.
  530. */
  531. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  532. return 0; /* whoops, it didn't work. */
  533. o_type = json_type_int;
  534. }
  535. switch(o_type) {
  536. case json_type_int:
  537. /* Make sure we return the correct values for out of range numbers. */
  538. if (cint64 <= INT32_MIN)
  539. return INT32_MIN;
  540. if (cint64 >= INT32_MAX)
  541. return INT32_MAX;
  542. return (int32_t) cint64;
  543. case json_type_double:
  544. return (int32_t)jso->o.c_double;
  545. case json_type_boolean:
  546. return jso->o.c_boolean;
  547. default:
  548. return 0;
  549. }
  550. }
  551. int json_object_set_int(struct json_object *jso,int new_value){
  552. if (!jso || jso->o_type!=json_type_int)
  553. return 0;
  554. jso->o.c_int64=new_value;
  555. return 1;
  556. }
  557. struct json_object* json_object_new_int64(int64_t i)
  558. {
  559. struct json_object *jso = json_object_new(json_type_int);
  560. if (!jso)
  561. return NULL;
  562. jso->_to_json_string = &json_object_int_to_json_string;
  563. jso->o.c_int64 = i;
  564. return jso;
  565. }
  566. int64_t json_object_get_int64(const struct json_object *jso)
  567. {
  568. int64_t cint;
  569. if (!jso)
  570. return 0;
  571. switch(jso->o_type)
  572. {
  573. case json_type_int:
  574. return jso->o.c_int64;
  575. case json_type_double:
  576. return (int64_t)jso->o.c_double;
  577. case json_type_boolean:
  578. return jso->o.c_boolean;
  579. case json_type_string:
  580. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  581. return cint;
  582. default:
  583. return 0;
  584. }
  585. }
  586. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  587. if (!jso || jso->o_type!=json_type_int)
  588. return 0;
  589. jso->o.c_int64=new_value;
  590. return 1;
  591. }
  592. /* json_object_double */
  593. static int json_object_double_to_json_string_format(struct json_object* jso,
  594. struct printbuf *pb,
  595. int level,
  596. int flags,
  597. const char *format)
  598. {
  599. char buf[128], *p, *q;
  600. int size;
  601. /* Although JSON RFC does not support
  602. NaN or Infinity as numeric values
  603. ECMA 262 section 9.8.1 defines
  604. how to handle these cases as strings */
  605. if(isnan(jso->o.c_double))
  606. size = snprintf(buf, sizeof(buf), "NaN");
  607. else if(isinf(jso->o.c_double))
  608. if(jso->o.c_double > 0)
  609. size = snprintf(buf, sizeof(buf), "Infinity");
  610. else
  611. size = snprintf(buf, sizeof(buf), "-Infinity");
  612. else
  613. size = snprintf(buf, sizeof(buf),
  614. format ? format : "%.17g", jso->o.c_double);
  615. p = strchr(buf, ',');
  616. if (p) {
  617. *p = '.';
  618. } else {
  619. p = strchr(buf, '.');
  620. }
  621. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  622. /* last useful digit, always keep 1 zero */
  623. p++;
  624. for (q=p ; *q ; q++) {
  625. if (*q!='0') p=q;
  626. }
  627. /* drop trailing zeroes */
  628. *(++p) = 0;
  629. size = p-buf;
  630. }
  631. printbuf_memappend(pb, buf, size);
  632. return size;
  633. }
  634. static int json_object_double_to_json_string_default(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. NULL);
  641. }
  642. int json_object_double_to_json_string(struct json_object* jso,
  643. struct printbuf *pb,
  644. int level,
  645. int flags)
  646. {
  647. return json_object_double_to_json_string_format(jso, pb, level, flags,
  648. (const char *)jso->_userdata);
  649. }
  650. struct json_object* json_object_new_double(double d)
  651. {
  652. struct json_object *jso = json_object_new(json_type_double);
  653. if (!jso)
  654. return NULL;
  655. jso->_to_json_string = &json_object_double_to_json_string_default;
  656. jso->o.c_double = d;
  657. return jso;
  658. }
  659. struct json_object* json_object_new_double_s(double d, const char *ds)
  660. {
  661. struct json_object *jso = json_object_new_double(d);
  662. if (!jso)
  663. return NULL;
  664. char *new_ds = strdup(ds);
  665. if (!new_ds)
  666. {
  667. json_object_generic_delete(jso);
  668. errno = ENOMEM;
  669. return NULL;
  670. }
  671. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  672. new_ds, json_object_free_userdata);
  673. return jso;
  674. }
  675. int json_object_userdata_to_json_string(struct json_object *jso,
  676. struct printbuf *pb, int level, int flags)
  677. {
  678. int userdata_len = strlen((const char *)jso->_userdata);
  679. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  680. return userdata_len;
  681. }
  682. void json_object_free_userdata(struct json_object *jso, void *userdata)
  683. {
  684. free(userdata);
  685. }
  686. double json_object_get_double(const struct json_object *jso)
  687. {
  688. double cdouble;
  689. char *errPtr = NULL;
  690. if(!jso) return 0.0;
  691. switch(jso->o_type) {
  692. case json_type_double:
  693. return jso->o.c_double;
  694. case json_type_int:
  695. return jso->o.c_int64;
  696. case json_type_boolean:
  697. return jso->o.c_boolean;
  698. case json_type_string:
  699. errno = 0;
  700. cdouble = strtod(get_string_component(jso), &errPtr);
  701. /* if conversion stopped at the first character, return 0.0 */
  702. if (errPtr == get_string_component(jso))
  703. return 0.0;
  704. /*
  705. * Check that the conversion terminated on something sensible
  706. *
  707. * For example, { "pay" : 123AB } would parse as 123.
  708. */
  709. if (*errPtr != '\0')
  710. return 0.0;
  711. /*
  712. * If strtod encounters a string which would exceed the
  713. * capacity of a double, it returns +/- HUGE_VAL and sets
  714. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  715. * from a conversion, so we need to check errno.
  716. *
  717. * Underflow also sets errno to ERANGE, but it returns 0 in
  718. * that case, which is what we will return anyway.
  719. *
  720. * See CERT guideline ERR30-C
  721. */
  722. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  723. (ERANGE == errno))
  724. cdouble = 0.0;
  725. return cdouble;
  726. default:
  727. return 0.0;
  728. }
  729. }
  730. int json_object_set_double(struct json_object *jso,double new_value){
  731. if (!jso || jso->o_type!=json_type_double)
  732. return 0;
  733. jso->o.c_double=new_value;
  734. return 1;
  735. }
  736. /* json_object_string */
  737. static int json_object_string_to_json_string(struct json_object* jso,
  738. struct printbuf *pb,
  739. int level,
  740. int flags)
  741. {
  742. sprintbuf(pb, "\"");
  743. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  744. sprintbuf(pb, "\"");
  745. return 0;
  746. }
  747. static void json_object_string_delete(struct json_object* jso)
  748. {
  749. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  750. free(jso->o.c_string.str.ptr);
  751. json_object_generic_delete(jso);
  752. }
  753. struct json_object* json_object_new_string(const char *s)
  754. {
  755. struct json_object *jso = json_object_new(json_type_string);
  756. if (!jso)
  757. return NULL;
  758. jso->_delete = &json_object_string_delete;
  759. jso->_to_json_string = &json_object_string_to_json_string;
  760. jso->o.c_string.len = strlen(s);
  761. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  762. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  763. } else {
  764. jso->o.c_string.str.ptr = strdup(s);
  765. if (!jso->o.c_string.str.ptr)
  766. {
  767. json_object_generic_delete(jso);
  768. errno = ENOMEM;
  769. return NULL;
  770. }
  771. }
  772. return jso;
  773. }
  774. struct json_object* json_object_new_string_len(const char *s, int len)
  775. {
  776. char *dstbuf;
  777. struct json_object *jso = json_object_new(json_type_string);
  778. if (!jso)
  779. return NULL;
  780. jso->_delete = &json_object_string_delete;
  781. jso->_to_json_string = &json_object_string_to_json_string;
  782. if(len < LEN_DIRECT_STRING_DATA) {
  783. dstbuf = jso->o.c_string.str.data;
  784. } else {
  785. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  786. if (!jso->o.c_string.str.ptr)
  787. {
  788. json_object_generic_delete(jso);
  789. errno = ENOMEM;
  790. return NULL;
  791. }
  792. dstbuf = jso->o.c_string.str.ptr;
  793. }
  794. memcpy(dstbuf, (const void *)s, len);
  795. dstbuf[len] = '\0';
  796. jso->o.c_string.len = len;
  797. return jso;
  798. }
  799. const char* json_object_get_string(struct json_object *jso)
  800. {
  801. if (!jso)
  802. return NULL;
  803. switch(jso->o_type)
  804. {
  805. case json_type_string:
  806. return get_string_component(jso);
  807. default:
  808. return json_object_to_json_string(jso);
  809. }
  810. }
  811. int json_object_get_string_len(const struct json_object *jso)
  812. {
  813. if (!jso)
  814. return 0;
  815. switch(jso->o_type)
  816. {
  817. case json_type_string:
  818. return jso->o.c_string.len;
  819. default:
  820. return 0;
  821. }
  822. }
  823. int json_object_set_string(json_object* jso, const char* s) {
  824. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  825. }
  826. int json_object_set_string_len(json_object* jso, const char* s, int len){
  827. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  828. char *dstbuf;
  829. if (len<LEN_DIRECT_STRING_DATA) {
  830. dstbuf=jso->o.c_string.str.data;
  831. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  832. } else {
  833. dstbuf=(char *)malloc(len+1);
  834. if (dstbuf==NULL) return 0;
  835. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  836. jso->o.c_string.str.ptr=dstbuf;
  837. }
  838. jso->o.c_string.len=len;
  839. memcpy(dstbuf, (const void *)s, len);
  840. dstbuf[len] = '\0';
  841. return 1;
  842. }
  843. /* json_object_array */
  844. static int json_object_array_to_json_string(struct json_object* jso,
  845. struct printbuf *pb,
  846. int level,
  847. int flags)
  848. {
  849. int had_children = 0;
  850. size_t ii;
  851. sprintbuf(pb, "[");
  852. if (flags & JSON_C_TO_STRING_PRETTY)
  853. sprintbuf(pb, "\n");
  854. for(ii=0; ii < json_object_array_length(jso); ii++)
  855. {
  856. struct json_object *val;
  857. if (had_children)
  858. {
  859. sprintbuf(pb, ",");
  860. if (flags & JSON_C_TO_STRING_PRETTY)
  861. sprintbuf(pb, "\n");
  862. }
  863. had_children = 1;
  864. if (flags & JSON_C_TO_STRING_SPACED)
  865. sprintbuf(pb, " ");
  866. indent(pb, level + 1, flags);
  867. val = json_object_array_get_idx(jso, ii);
  868. if(val == NULL)
  869. sprintbuf(pb, "null");
  870. else
  871. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  872. return -1;
  873. }
  874. if (flags & JSON_C_TO_STRING_PRETTY)
  875. {
  876. if (had_children)
  877. sprintbuf(pb, "\n");
  878. indent(pb,level,flags);
  879. }
  880. if (flags & JSON_C_TO_STRING_SPACED)
  881. return sprintbuf(pb, " ]");
  882. return sprintbuf(pb, "]");
  883. }
  884. static void json_object_array_entry_free(void *data)
  885. {
  886. json_object_put((struct json_object*)data);
  887. }
  888. static void json_object_array_delete(struct json_object* jso)
  889. {
  890. array_list_free(jso->o.c_array);
  891. json_object_generic_delete(jso);
  892. }
  893. struct json_object* json_object_new_array(void)
  894. {
  895. struct json_object *jso = json_object_new(json_type_array);
  896. if (!jso)
  897. return NULL;
  898. jso->_delete = &json_object_array_delete;
  899. jso->_to_json_string = &json_object_array_to_json_string;
  900. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  901. if(jso->o.c_array == NULL)
  902. {
  903. free(jso);
  904. return NULL;
  905. }
  906. return jso;
  907. }
  908. struct array_list* json_object_get_array(const struct json_object *jso)
  909. {
  910. if (!jso)
  911. return NULL;
  912. switch(jso->o_type)
  913. {
  914. case json_type_array:
  915. return jso->o.c_array;
  916. default:
  917. return NULL;
  918. }
  919. }
  920. void json_object_array_sort(struct json_object *jso,
  921. int(*sort_fn)(const void *, const void *))
  922. {
  923. assert(json_object_get_type(jso) == json_type_array);
  924. array_list_sort(jso->o.c_array, sort_fn);
  925. }
  926. struct json_object* json_object_array_bsearch(
  927. const struct json_object *key,
  928. const struct json_object *jso,
  929. int (*sort_fn)(const void *, const void *))
  930. {
  931. struct json_object **result;
  932. assert(json_object_get_type(jso) == json_type_array);
  933. result = (struct json_object **)array_list_bsearch(
  934. (const void **)&key, jso->o.c_array, sort_fn);
  935. if (!result)
  936. return NULL;
  937. return *result;
  938. }
  939. size_t json_object_array_length(const struct json_object *jso)
  940. {
  941. assert(json_object_get_type(jso) == json_type_array);
  942. return array_list_length(jso->o.c_array);
  943. }
  944. int json_object_array_add(struct json_object *jso,struct json_object *val)
  945. {
  946. assert(json_object_get_type(jso) == json_type_array);
  947. return array_list_add(jso->o.c_array, val);
  948. }
  949. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  950. struct json_object *val)
  951. {
  952. assert(json_object_get_type(jso) == json_type_array);
  953. return array_list_put_idx(jso->o.c_array, idx, val);
  954. }
  955. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  956. {
  957. assert(json_object_get_type(jso) == json_type_array);
  958. return array_list_del_idx(jso->o.c_array, idx, count);
  959. }
  960. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  961. size_t idx)
  962. {
  963. assert(json_object_get_type(jso) == json_type_array);
  964. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  965. }
  966. static int json_array_equal(struct json_object* jso1,
  967. struct json_object* jso2)
  968. {
  969. size_t len, i;
  970. len = json_object_array_length(jso1);
  971. if (len != json_object_array_length(jso2))
  972. return 0;
  973. for (i = 0; i < len; i++) {
  974. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  975. json_object_array_get_idx(jso2, i)))
  976. return 0;
  977. }
  978. return 1;
  979. }
  980. static int json_object_all_values_equal(struct json_object* jso1,
  981. struct json_object* jso2)
  982. {
  983. struct json_object_iter iter;
  984. struct json_object *sub;
  985. assert(json_object_get_type(jso1) == json_type_object);
  986. assert(json_object_get_type(jso2) == json_type_object);
  987. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  988. json_object_object_foreachC(jso1, iter) {
  989. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  990. (void**)&sub))
  991. return 0;
  992. if (!json_object_equal(iter.val, sub))
  993. return 0;
  994. }
  995. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  996. json_object_object_foreachC(jso2, iter) {
  997. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  998. (void**)&sub))
  999. return 0;
  1000. }
  1001. return 1;
  1002. }
  1003. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1004. {
  1005. if (jso1 == jso2)
  1006. return 1;
  1007. if (!jso1 || !jso2)
  1008. return 0;
  1009. if (jso1->o_type != jso2->o_type)
  1010. return 0;
  1011. switch(jso1->o_type) {
  1012. case json_type_boolean:
  1013. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1014. case json_type_double:
  1015. return (jso1->o.c_double == jso2->o.c_double);
  1016. case json_type_int:
  1017. return (jso1->o.c_int64 == jso2->o.c_int64);
  1018. case json_type_string:
  1019. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1020. memcmp(get_string_component(jso1),
  1021. get_string_component(jso2),
  1022. jso1->o.c_string.len) == 0);
  1023. case json_type_object:
  1024. return json_object_all_values_equal(jso1, jso2);
  1025. case json_type_array:
  1026. return json_array_equal(jso1, jso2);
  1027. case json_type_null:
  1028. return 1;
  1029. };
  1030. return 0;
  1031. }