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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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 "strdup_compat.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. double dummy; /* needed for modf() */
  602. /* Although JSON RFC does not support
  603. NaN or Infinity as numeric values
  604. ECMA 262 section 9.8.1 defines
  605. how to handle these cases as strings */
  606. if(isnan(jso->o.c_double))
  607. size = snprintf(buf, sizeof(buf), "NaN");
  608. else if(isinf(jso->o.c_double))
  609. if(jso->o.c_double > 0)
  610. size = snprintf(buf, sizeof(buf), "Infinity");
  611. else
  612. size = snprintf(buf, sizeof(buf), "-Infinity");
  613. else
  614. size = snprintf(buf, sizeof(buf),
  615. format ? format :
  616. (modf(jso->o.c_double, &dummy) == 0) ? "%.17g.0" : "%.17g",
  617. jso->o.c_double);
  618. p = strchr(buf, ',');
  619. if (p) {
  620. *p = '.';
  621. } else {
  622. p = strchr(buf, '.');
  623. }
  624. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  625. /* last useful digit, always keep 1 zero */
  626. p++;
  627. for (q=p ; *q ; q++) {
  628. if (*q!='0') p=q;
  629. }
  630. /* drop trailing zeroes */
  631. *(++p) = 0;
  632. size = p-buf;
  633. }
  634. printbuf_memappend(pb, buf, size);
  635. return size;
  636. }
  637. static int json_object_double_to_json_string_default(struct json_object* jso,
  638. struct printbuf *pb,
  639. int level,
  640. int flags)
  641. {
  642. return json_object_double_to_json_string_format(jso, pb, level, flags,
  643. NULL);
  644. }
  645. int json_object_double_to_json_string(struct json_object* jso,
  646. struct printbuf *pb,
  647. int level,
  648. int flags)
  649. {
  650. return json_object_double_to_json_string_format(jso, pb, level, flags,
  651. (const char *)jso->_userdata);
  652. }
  653. struct json_object* json_object_new_double(double d)
  654. {
  655. struct json_object *jso = json_object_new(json_type_double);
  656. if (!jso)
  657. return NULL;
  658. jso->_to_json_string = &json_object_double_to_json_string_default;
  659. jso->o.c_double = d;
  660. return jso;
  661. }
  662. struct json_object* json_object_new_double_s(double d, const char *ds)
  663. {
  664. struct json_object *jso = json_object_new_double(d);
  665. if (!jso)
  666. return NULL;
  667. char *new_ds = strdup(ds);
  668. if (!new_ds)
  669. {
  670. json_object_generic_delete(jso);
  671. errno = ENOMEM;
  672. return NULL;
  673. }
  674. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  675. new_ds, json_object_free_userdata);
  676. return jso;
  677. }
  678. int json_object_userdata_to_json_string(struct json_object *jso,
  679. struct printbuf *pb, int level, int flags)
  680. {
  681. int userdata_len = strlen((const char *)jso->_userdata);
  682. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  683. return userdata_len;
  684. }
  685. void json_object_free_userdata(struct json_object *jso, void *userdata)
  686. {
  687. free(userdata);
  688. }
  689. double json_object_get_double(const struct json_object *jso)
  690. {
  691. double cdouble;
  692. char *errPtr = NULL;
  693. if(!jso) return 0.0;
  694. switch(jso->o_type) {
  695. case json_type_double:
  696. return jso->o.c_double;
  697. case json_type_int:
  698. return jso->o.c_int64;
  699. case json_type_boolean:
  700. return jso->o.c_boolean;
  701. case json_type_string:
  702. errno = 0;
  703. cdouble = strtod(get_string_component(jso), &errPtr);
  704. /* if conversion stopped at the first character, return 0.0 */
  705. if (errPtr == get_string_component(jso))
  706. return 0.0;
  707. /*
  708. * Check that the conversion terminated on something sensible
  709. *
  710. * For example, { "pay" : 123AB } would parse as 123.
  711. */
  712. if (*errPtr != '\0')
  713. return 0.0;
  714. /*
  715. * If strtod encounters a string which would exceed the
  716. * capacity of a double, it returns +/- HUGE_VAL and sets
  717. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  718. * from a conversion, so we need to check errno.
  719. *
  720. * Underflow also sets errno to ERANGE, but it returns 0 in
  721. * that case, which is what we will return anyway.
  722. *
  723. * See CERT guideline ERR30-C
  724. */
  725. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  726. (ERANGE == errno))
  727. cdouble = 0.0;
  728. return cdouble;
  729. default:
  730. return 0.0;
  731. }
  732. }
  733. int json_object_set_double(struct json_object *jso,double new_value){
  734. if (!jso || jso->o_type!=json_type_double)
  735. return 0;
  736. jso->o.c_double=new_value;
  737. return 1;
  738. }
  739. /* json_object_string */
  740. static int json_object_string_to_json_string(struct json_object* jso,
  741. struct printbuf *pb,
  742. int level,
  743. int flags)
  744. {
  745. sprintbuf(pb, "\"");
  746. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  747. sprintbuf(pb, "\"");
  748. return 0;
  749. }
  750. static void json_object_string_delete(struct json_object* jso)
  751. {
  752. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  753. free(jso->o.c_string.str.ptr);
  754. json_object_generic_delete(jso);
  755. }
  756. struct json_object* json_object_new_string(const char *s)
  757. {
  758. struct json_object *jso = json_object_new(json_type_string);
  759. if (!jso)
  760. return NULL;
  761. jso->_delete = &json_object_string_delete;
  762. jso->_to_json_string = &json_object_string_to_json_string;
  763. jso->o.c_string.len = strlen(s);
  764. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  765. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  766. } else {
  767. jso->o.c_string.str.ptr = strdup(s);
  768. if (!jso->o.c_string.str.ptr)
  769. {
  770. json_object_generic_delete(jso);
  771. errno = ENOMEM;
  772. return NULL;
  773. }
  774. }
  775. return jso;
  776. }
  777. struct json_object* json_object_new_string_len(const char *s, int len)
  778. {
  779. char *dstbuf;
  780. struct json_object *jso = json_object_new(json_type_string);
  781. if (!jso)
  782. return NULL;
  783. jso->_delete = &json_object_string_delete;
  784. jso->_to_json_string = &json_object_string_to_json_string;
  785. if(len < LEN_DIRECT_STRING_DATA) {
  786. dstbuf = jso->o.c_string.str.data;
  787. } else {
  788. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  789. if (!jso->o.c_string.str.ptr)
  790. {
  791. json_object_generic_delete(jso);
  792. errno = ENOMEM;
  793. return NULL;
  794. }
  795. dstbuf = jso->o.c_string.str.ptr;
  796. }
  797. memcpy(dstbuf, (const void *)s, len);
  798. dstbuf[len] = '\0';
  799. jso->o.c_string.len = len;
  800. return jso;
  801. }
  802. const char* json_object_get_string(struct json_object *jso)
  803. {
  804. if (!jso)
  805. return NULL;
  806. switch(jso->o_type)
  807. {
  808. case json_type_string:
  809. return get_string_component(jso);
  810. default:
  811. return json_object_to_json_string(jso);
  812. }
  813. }
  814. int json_object_get_string_len(const struct json_object *jso)
  815. {
  816. if (!jso)
  817. return 0;
  818. switch(jso->o_type)
  819. {
  820. case json_type_string:
  821. return jso->o.c_string.len;
  822. default:
  823. return 0;
  824. }
  825. }
  826. int json_object_set_string(json_object* jso, const char* s) {
  827. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  828. }
  829. int json_object_set_string_len(json_object* jso, const char* s, int len){
  830. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  831. char *dstbuf;
  832. if (len<LEN_DIRECT_STRING_DATA) {
  833. dstbuf=jso->o.c_string.str.data;
  834. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  835. } else {
  836. dstbuf=(char *)malloc(len+1);
  837. if (dstbuf==NULL) return 0;
  838. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  839. jso->o.c_string.str.ptr=dstbuf;
  840. }
  841. jso->o.c_string.len=len;
  842. memcpy(dstbuf, (const void *)s, len);
  843. dstbuf[len] = '\0';
  844. return 1;
  845. }
  846. /* json_object_array */
  847. static int json_object_array_to_json_string(struct json_object* jso,
  848. struct printbuf *pb,
  849. int level,
  850. int flags)
  851. {
  852. int had_children = 0;
  853. size_t ii;
  854. sprintbuf(pb, "[");
  855. if (flags & JSON_C_TO_STRING_PRETTY)
  856. sprintbuf(pb, "\n");
  857. for(ii=0; ii < json_object_array_length(jso); ii++)
  858. {
  859. struct json_object *val;
  860. if (had_children)
  861. {
  862. sprintbuf(pb, ",");
  863. if (flags & JSON_C_TO_STRING_PRETTY)
  864. sprintbuf(pb, "\n");
  865. }
  866. had_children = 1;
  867. if (flags & JSON_C_TO_STRING_SPACED)
  868. sprintbuf(pb, " ");
  869. indent(pb, level + 1, flags);
  870. val = json_object_array_get_idx(jso, ii);
  871. if(val == NULL)
  872. sprintbuf(pb, "null");
  873. else
  874. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  875. return -1;
  876. }
  877. if (flags & JSON_C_TO_STRING_PRETTY)
  878. {
  879. if (had_children)
  880. sprintbuf(pb, "\n");
  881. indent(pb,level,flags);
  882. }
  883. if (flags & JSON_C_TO_STRING_SPACED)
  884. return sprintbuf(pb, " ]");
  885. return sprintbuf(pb, "]");
  886. }
  887. static void json_object_array_entry_free(void *data)
  888. {
  889. json_object_put((struct json_object*)data);
  890. }
  891. static void json_object_array_delete(struct json_object* jso)
  892. {
  893. array_list_free(jso->o.c_array);
  894. json_object_generic_delete(jso);
  895. }
  896. struct json_object* json_object_new_array(void)
  897. {
  898. struct json_object *jso = json_object_new(json_type_array);
  899. if (!jso)
  900. return NULL;
  901. jso->_delete = &json_object_array_delete;
  902. jso->_to_json_string = &json_object_array_to_json_string;
  903. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  904. if(jso->o.c_array == NULL)
  905. {
  906. free(jso);
  907. return NULL;
  908. }
  909. return jso;
  910. }
  911. struct array_list* json_object_get_array(const struct json_object *jso)
  912. {
  913. if (!jso)
  914. return NULL;
  915. switch(jso->o_type)
  916. {
  917. case json_type_array:
  918. return jso->o.c_array;
  919. default:
  920. return NULL;
  921. }
  922. }
  923. void json_object_array_sort(struct json_object *jso,
  924. int(*sort_fn)(const void *, const void *))
  925. {
  926. assert(json_object_get_type(jso) == json_type_array);
  927. array_list_sort(jso->o.c_array, sort_fn);
  928. }
  929. struct json_object* json_object_array_bsearch(
  930. const struct json_object *key,
  931. const struct json_object *jso,
  932. int (*sort_fn)(const void *, const void *))
  933. {
  934. struct json_object **result;
  935. assert(json_object_get_type(jso) == json_type_array);
  936. result = (struct json_object **)array_list_bsearch(
  937. (const void **)&key, jso->o.c_array, sort_fn);
  938. if (!result)
  939. return NULL;
  940. return *result;
  941. }
  942. size_t json_object_array_length(const struct json_object *jso)
  943. {
  944. assert(json_object_get_type(jso) == json_type_array);
  945. return array_list_length(jso->o.c_array);
  946. }
  947. int json_object_array_add(struct json_object *jso,struct json_object *val)
  948. {
  949. assert(json_object_get_type(jso) == json_type_array);
  950. return array_list_add(jso->o.c_array, val);
  951. }
  952. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  953. struct json_object *val)
  954. {
  955. assert(json_object_get_type(jso) == json_type_array);
  956. return array_list_put_idx(jso->o.c_array, idx, val);
  957. }
  958. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  959. {
  960. assert(json_object_get_type(jso) == json_type_array);
  961. return array_list_del_idx(jso->o.c_array, idx, count);
  962. }
  963. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  964. size_t idx)
  965. {
  966. assert(json_object_get_type(jso) == json_type_array);
  967. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  968. }
  969. static int json_array_equal(struct json_object* jso1,
  970. struct json_object* jso2)
  971. {
  972. size_t len, i;
  973. len = json_object_array_length(jso1);
  974. if (len != json_object_array_length(jso2))
  975. return 0;
  976. for (i = 0; i < len; i++) {
  977. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  978. json_object_array_get_idx(jso2, i)))
  979. return 0;
  980. }
  981. return 1;
  982. }
  983. static int json_object_all_values_equal(struct json_object* jso1,
  984. struct json_object* jso2)
  985. {
  986. struct json_object_iter iter;
  987. struct json_object *sub;
  988. assert(json_object_get_type(jso1) == json_type_object);
  989. assert(json_object_get_type(jso2) == json_type_object);
  990. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  991. json_object_object_foreachC(jso1, iter) {
  992. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  993. (void**)&sub))
  994. return 0;
  995. if (!json_object_equal(iter.val, sub))
  996. return 0;
  997. }
  998. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  999. json_object_object_foreachC(jso2, iter) {
  1000. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1001. (void**)&sub))
  1002. return 0;
  1003. }
  1004. return 1;
  1005. }
  1006. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1007. {
  1008. if (jso1 == jso2)
  1009. return 1;
  1010. if (!jso1 || !jso2)
  1011. return 0;
  1012. if (jso1->o_type != jso2->o_type)
  1013. return 0;
  1014. switch(jso1->o_type) {
  1015. case json_type_boolean:
  1016. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1017. case json_type_double:
  1018. return (jso1->o.c_double == jso2->o.c_double);
  1019. case json_type_int:
  1020. return (jso1->o.c_int64 == jso2->o.c_int64);
  1021. case json_type_string:
  1022. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1023. memcmp(get_string_component(jso1),
  1024. get_string_component(jso2),
  1025. jso1->o.c_string.len) == 0);
  1026. case json_type_object:
  1027. return json_object_all_values_equal(jso1, jso2);
  1028. case json_type_array:
  1029. return json_array_equal(jso1, jso2);
  1030. case json_type_null:
  1031. return 1;
  1032. };
  1033. return 0;
  1034. }