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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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_int_to_json_string;
  49. static json_object_to_json_string_fn json_object_double_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 = (struct json_object*)ent->v;
  73. MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
  74. }
  75. }
  76. }
  77. MC_DEBUG("json_object_fini: freeing object table\n");
  78. lh_table_free(json_object_table);
  79. }
  80. #endif /* REFCOUNT_DEBUG */
  81. /* helper for accessing the optimized string data component in json_object
  82. */
  83. static const char *
  84. get_string_component(const struct json_object *jso)
  85. {
  86. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  87. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  88. }
  89. /* string escaping */
  90. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  91. {
  92. int pos = 0, start_offset = 0;
  93. unsigned char c;
  94. while (len--)
  95. {
  96. c = str[pos];
  97. switch(c)
  98. {
  99. case '\b':
  100. case '\n':
  101. case '\r':
  102. case '\t':
  103. case '\f':
  104. case '"':
  105. case '\\':
  106. case '/':
  107. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  108. {
  109. pos++;
  110. break;
  111. }
  112. if(pos - start_offset > 0)
  113. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  114. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  115. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  116. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  117. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  118. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  119. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  120. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  121. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  122. start_offset = ++pos;
  123. break;
  124. default:
  125. if(c < ' ')
  126. {
  127. if(pos - start_offset > 0)
  128. printbuf_memappend(pb, str + start_offset, 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. /* set a custom conversion to string */
  203. void json_object_set_serializer(json_object *jso,
  204. json_object_to_json_string_fn to_string_func,
  205. void *userdata,
  206. json_object_delete_fn *user_delete)
  207. {
  208. // First, clean up any previously existing user info
  209. if (jso->_user_delete)
  210. {
  211. jso->_user_delete(jso, jso->_userdata);
  212. }
  213. jso->_userdata = NULL;
  214. jso->_user_delete = NULL;
  215. if (to_string_func == NULL)
  216. {
  217. // Reset to the standard serialization function
  218. switch(jso->o_type)
  219. {
  220. case json_type_null:
  221. jso->_to_json_string = NULL;
  222. break;
  223. case json_type_boolean:
  224. jso->_to_json_string = &json_object_boolean_to_json_string;
  225. break;
  226. case json_type_double:
  227. jso->_to_json_string = &json_object_double_to_json_string;
  228. break;
  229. case json_type_int:
  230. jso->_to_json_string = &json_object_int_to_json_string;
  231. break;
  232. case json_type_object:
  233. jso->_to_json_string = &json_object_object_to_json_string;
  234. break;
  235. case json_type_array:
  236. jso->_to_json_string = &json_object_array_to_json_string;
  237. break;
  238. case json_type_string:
  239. jso->_to_json_string = &json_object_string_to_json_string;
  240. break;
  241. }
  242. return;
  243. }
  244. jso->_to_json_string = to_string_func;
  245. jso->_userdata = userdata;
  246. jso->_user_delete = user_delete;
  247. }
  248. /* extended conversion to string */
  249. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  250. {
  251. if (!jso)
  252. return "null";
  253. if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
  254. return NULL;
  255. printbuf_reset(jso->_pb);
  256. if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
  257. return NULL;
  258. return jso->_pb->buf;
  259. }
  260. /* backwards-compatible conversion to string */
  261. const char* json_object_to_json_string(struct json_object *jso)
  262. {
  263. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  264. }
  265. static void indent(struct printbuf *pb, int level, int flags)
  266. {
  267. if (flags & JSON_C_TO_STRING_PRETTY)
  268. {
  269. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  270. {
  271. printbuf_memset(pb, -1, '\t', level);
  272. }
  273. else
  274. {
  275. printbuf_memset(pb, -1, ' ', level * 2);
  276. }
  277. }
  278. }
  279. /* json_object_object */
  280. static int json_object_object_to_json_string(struct json_object* jso,
  281. struct printbuf *pb,
  282. int level,
  283. int flags)
  284. {
  285. int had_children = 0;
  286. struct json_object_iter iter;
  287. sprintbuf(pb, "{" /*}*/);
  288. if (flags & JSON_C_TO_STRING_PRETTY)
  289. sprintbuf(pb, "\n");
  290. json_object_object_foreachC(jso, iter)
  291. {
  292. if (had_children)
  293. {
  294. sprintbuf(pb, ",");
  295. if (flags & JSON_C_TO_STRING_PRETTY)
  296. sprintbuf(pb, "\n");
  297. }
  298. had_children = 1;
  299. if (flags & JSON_C_TO_STRING_SPACED)
  300. sprintbuf(pb, " ");
  301. indent(pb, level+1, flags);
  302. sprintbuf(pb, "\"");
  303. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  304. if (flags & JSON_C_TO_STRING_SPACED)
  305. sprintbuf(pb, "\": ");
  306. else
  307. sprintbuf(pb, "\":");
  308. if(iter.val == NULL)
  309. sprintbuf(pb, "null");
  310. else
  311. iter.val->_to_json_string(iter.val, pb, level+1,flags);
  312. }
  313. if (flags & JSON_C_TO_STRING_PRETTY)
  314. {
  315. if (had_children)
  316. sprintbuf(pb, "\n");
  317. indent(pb,level,flags);
  318. }
  319. if (flags & JSON_C_TO_STRING_SPACED)
  320. return sprintbuf(pb, /*{*/ " }");
  321. else
  322. return sprintbuf(pb, /*{*/ "}");
  323. }
  324. static void json_object_lh_entry_free(struct lh_entry *ent)
  325. {
  326. if (!ent->k_is_constant)
  327. free(ent->k);
  328. json_object_put((struct json_object*)ent->v);
  329. }
  330. static void json_object_object_delete(struct json_object* jso)
  331. {
  332. lh_table_free(jso->o.c_object);
  333. json_object_generic_delete(jso);
  334. }
  335. struct json_object* json_object_new_object(void)
  336. {
  337. struct json_object *jso = json_object_new(json_type_object);
  338. if (!jso)
  339. return NULL;
  340. jso->_delete = &json_object_object_delete;
  341. jso->_to_json_string = &json_object_object_to_json_string;
  342. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  343. &json_object_lh_entry_free);
  344. if (!jso->o.c_object)
  345. {
  346. json_object_generic_delete(jso);
  347. errno = ENOMEM;
  348. return NULL;
  349. }
  350. return jso;
  351. }
  352. struct lh_table* json_object_get_object(const struct json_object *jso)
  353. {
  354. if (!jso)
  355. return NULL;
  356. switch(jso->o_type)
  357. {
  358. case json_type_object:
  359. return jso->o.c_object;
  360. default:
  361. return NULL;
  362. }
  363. }
  364. void json_object_object_add_ex(struct json_object* jso,
  365. const char *const key,
  366. struct json_object *const val,
  367. const unsigned opts)
  368. {
  369. // We lookup the entry and replace the value, rather than just deleting
  370. // and re-adding it, so the existing key remains valid.
  371. json_object *existing_value = NULL;
  372. struct lh_entry *existing_entry;
  373. const unsigned long hash = lh_get_hash(jso->o.c_object, (void*)key);
  374. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  375. lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash);
  376. if (!existing_entry)
  377. {
  378. void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  379. (void*)key : strdup(key);
  380. lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  381. return;
  382. }
  383. existing_value = (json_object *)existing_entry->v;
  384. if (existing_value)
  385. json_object_put(existing_value);
  386. existing_entry->v = val;
  387. }
  388. int json_object_object_add(struct json_object* jso, const char *key,
  389. struct json_object *val)
  390. {
  391. // We lookup the entry and replace the value, rather than just deleting
  392. // and re-adding it, so the existing key remains valid.
  393. json_object *existing_value = NULL;
  394. struct lh_entry *existing_entry;
  395. const unsigned long hash = lh_get_hash(jso->o.c_object, (void*)key);
  396. existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash);
  397. if (!existing_entry)
  398. {
  399. char *keydup = strdup(key);
  400. if (keydup == NULL)
  401. return -1;
  402. return lh_table_insert_w_hash(jso->o.c_object, keydup, val, hash, 0);
  403. }
  404. existing_value = (json_object *)existing_entry->v;
  405. if (existing_value)
  406. json_object_put(existing_value);
  407. existing_entry->v = val;
  408. return 0;
  409. }
  410. int json_object_object_length(const struct json_object *jso)
  411. {
  412. return lh_table_length(jso->o.c_object);
  413. }
  414. struct json_object* json_object_object_get(const struct json_object* jso, const char *key)
  415. {
  416. struct json_object *result = NULL;
  417. json_object_object_get_ex(jso, key, &result);
  418. return result;
  419. }
  420. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key, struct json_object **value)
  421. {
  422. if (value != NULL)
  423. *value = NULL;
  424. if (NULL == jso)
  425. return FALSE;
  426. switch(jso->o_type)
  427. {
  428. case json_type_object:
  429. return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
  430. default:
  431. if (value != NULL)
  432. *value = NULL;
  433. return FALSE;
  434. }
  435. }
  436. void json_object_object_del(struct json_object* jso, const char *key)
  437. {
  438. lh_table_delete(jso->o.c_object, key);
  439. }
  440. /* json_object_boolean */
  441. static int json_object_boolean_to_json_string(struct json_object* jso,
  442. struct printbuf *pb,
  443. int level,
  444. int flags)
  445. {
  446. if (jso->o.c_boolean)
  447. return sprintbuf(pb, "true");
  448. else
  449. return sprintbuf(pb, "false");
  450. }
  451. struct json_object* json_object_new_boolean(json_bool b)
  452. {
  453. struct json_object *jso = json_object_new(json_type_boolean);
  454. if (!jso)
  455. return NULL;
  456. jso->_to_json_string = &json_object_boolean_to_json_string;
  457. jso->o.c_boolean = b;
  458. return jso;
  459. }
  460. json_bool json_object_get_boolean(const struct json_object *jso)
  461. {
  462. if (!jso)
  463. return FALSE;
  464. switch(jso->o_type)
  465. {
  466. case json_type_boolean:
  467. return jso->o.c_boolean;
  468. case json_type_int:
  469. return (jso->o.c_int64 != 0);
  470. case json_type_double:
  471. return (jso->o.c_double != 0);
  472. case json_type_string:
  473. return (jso->o.c_string.len != 0);
  474. default:
  475. return FALSE;
  476. }
  477. }
  478. /* json_object_int */
  479. static int json_object_int_to_json_string(struct json_object* jso,
  480. struct printbuf *pb,
  481. int level,
  482. int flags)
  483. {
  484. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  485. }
  486. struct json_object* json_object_new_int(int32_t i)
  487. {
  488. struct json_object *jso = json_object_new(json_type_int);
  489. if (!jso)
  490. return NULL;
  491. jso->_to_json_string = &json_object_int_to_json_string;
  492. jso->o.c_int64 = i;
  493. return jso;
  494. }
  495. int32_t json_object_get_int(const struct json_object *jso)
  496. {
  497. int64_t cint64;
  498. enum json_type o_type;
  499. if(!jso) return 0;
  500. o_type = jso->o_type;
  501. cint64 = jso->o.c_int64;
  502. if (o_type == json_type_string)
  503. {
  504. /*
  505. * Parse strings into 64-bit numbers, then use the
  506. * 64-to-32-bit number handling below.
  507. */
  508. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  509. return 0; /* whoops, it didn't work. */
  510. o_type = json_type_int;
  511. }
  512. switch(o_type) {
  513. case json_type_int:
  514. /* Make sure we return the correct values for out of range numbers. */
  515. if (cint64 <= INT32_MIN)
  516. return INT32_MIN;
  517. else if (cint64 >= INT32_MAX)
  518. return INT32_MAX;
  519. else
  520. return (int32_t)cint64;
  521. case json_type_double:
  522. return (int32_t)jso->o.c_double;
  523. case json_type_boolean:
  524. return jso->o.c_boolean;
  525. default:
  526. return 0;
  527. }
  528. }
  529. struct json_object* json_object_new_int64(int64_t i)
  530. {
  531. struct json_object *jso = json_object_new(json_type_int);
  532. if (!jso)
  533. return NULL;
  534. jso->_to_json_string = &json_object_int_to_json_string;
  535. jso->o.c_int64 = i;
  536. return jso;
  537. }
  538. int64_t json_object_get_int64(const struct json_object *jso)
  539. {
  540. int64_t cint;
  541. if (!jso)
  542. return 0;
  543. switch(jso->o_type)
  544. {
  545. case json_type_int:
  546. return jso->o.c_int64;
  547. case json_type_double:
  548. return (int64_t)jso->o.c_double;
  549. case json_type_boolean:
  550. return jso->o.c_boolean;
  551. case json_type_string:
  552. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  553. return cint;
  554. default:
  555. return 0;
  556. }
  557. }
  558. /* json_object_double */
  559. static int json_object_double_to_json_string(struct json_object* jso,
  560. struct printbuf *pb,
  561. int level,
  562. int flags)
  563. {
  564. char buf[128], *p, *q;
  565. int size;
  566. /* Although JSON RFC does not support
  567. NaN or Infinity as numeric values
  568. ECMA 262 section 9.8.1 defines
  569. how to handle these cases as strings */
  570. if(isnan(jso->o.c_double))
  571. size = snprintf(buf, sizeof(buf), "NaN");
  572. else if(isinf(jso->o.c_double))
  573. if(jso->o.c_double > 0)
  574. size = snprintf(buf, sizeof(buf), "Infinity");
  575. else
  576. size = snprintf(buf, sizeof(buf), "-Infinity");
  577. else
  578. size = snprintf(buf, sizeof(buf), "%.17g", jso->o.c_double);
  579. p = strchr(buf, ',');
  580. if (p) {
  581. *p = '.';
  582. } else {
  583. p = strchr(buf, '.');
  584. }
  585. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  586. /* last useful digit, always keep 1 zero */
  587. p++;
  588. for (q=p ; *q ; q++) {
  589. if (*q!='0') p=q;
  590. }
  591. /* drop trailing zeroes */
  592. *(++p) = 0;
  593. size = p-buf;
  594. }
  595. printbuf_memappend(pb, buf, size);
  596. return size;
  597. }
  598. struct json_object* json_object_new_double(double d)
  599. {
  600. struct json_object *jso = json_object_new(json_type_double);
  601. if (!jso)
  602. return NULL;
  603. jso->_to_json_string = &json_object_double_to_json_string;
  604. jso->o.c_double = d;
  605. return jso;
  606. }
  607. struct json_object* json_object_new_double_s(double d, const char *ds)
  608. {
  609. struct json_object *jso = json_object_new_double(d);
  610. if (!jso)
  611. return NULL;
  612. char *new_ds = strdup(ds);
  613. if (!new_ds)
  614. {
  615. json_object_generic_delete(jso);
  616. errno = ENOMEM;
  617. return NULL;
  618. }
  619. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  620. new_ds, json_object_free_userdata);
  621. return jso;
  622. }
  623. int json_object_userdata_to_json_string(struct json_object *jso,
  624. struct printbuf *pb, int level, int flags)
  625. {
  626. int userdata_len = strlen((const char *)jso->_userdata);
  627. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  628. return userdata_len;
  629. }
  630. void json_object_free_userdata(struct json_object *jso, void *userdata)
  631. {
  632. free(userdata);
  633. }
  634. double json_object_get_double(const struct json_object *jso)
  635. {
  636. double cdouble;
  637. char *errPtr = NULL;
  638. if(!jso) return 0.0;
  639. switch(jso->o_type) {
  640. case json_type_double:
  641. return jso->o.c_double;
  642. case json_type_int:
  643. return jso->o.c_int64;
  644. case json_type_boolean:
  645. return jso->o.c_boolean;
  646. case json_type_string:
  647. errno = 0;
  648. cdouble = strtod(get_string_component(jso), &errPtr);
  649. /* if conversion stopped at the first character, return 0.0 */
  650. if (errPtr == get_string_component(jso))
  651. return 0.0;
  652. /*
  653. * Check that the conversion terminated on something sensible
  654. *
  655. * For example, { "pay" : 123AB } would parse as 123.
  656. */
  657. if (*errPtr != '\0')
  658. return 0.0;
  659. /*
  660. * If strtod encounters a string which would exceed the
  661. * capacity of a double, it returns +/- HUGE_VAL and sets
  662. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  663. * from a conversion, so we need to check errno.
  664. *
  665. * Underflow also sets errno to ERANGE, but it returns 0 in
  666. * that case, which is what we will return anyway.
  667. *
  668. * See CERT guideline ERR30-C
  669. */
  670. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  671. (ERANGE == errno))
  672. cdouble = 0.0;
  673. return cdouble;
  674. default:
  675. return 0.0;
  676. }
  677. }
  678. /* json_object_string */
  679. static int json_object_string_to_json_string(struct json_object* jso,
  680. struct printbuf *pb,
  681. int level,
  682. int flags)
  683. {
  684. sprintbuf(pb, "\"");
  685. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  686. sprintbuf(pb, "\"");
  687. return 0;
  688. }
  689. static void json_object_string_delete(struct json_object* jso)
  690. {
  691. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  692. free(jso->o.c_string.str.ptr);
  693. json_object_generic_delete(jso);
  694. }
  695. struct json_object* json_object_new_string(const char *s)
  696. {
  697. struct json_object *jso = json_object_new(json_type_string);
  698. if (!jso)
  699. return NULL;
  700. jso->_delete = &json_object_string_delete;
  701. jso->_to_json_string = &json_object_string_to_json_string;
  702. jso->o.c_string.len = strlen(s);
  703. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  704. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  705. } else {
  706. jso->o.c_string.str.ptr = strdup(s);
  707. if (!jso->o.c_string.str.ptr)
  708. {
  709. json_object_generic_delete(jso);
  710. errno = ENOMEM;
  711. return NULL;
  712. }
  713. }
  714. return jso;
  715. }
  716. struct json_object* json_object_new_string_len(const char *s, int len)
  717. {
  718. char *dstbuf;
  719. struct json_object *jso = json_object_new(json_type_string);
  720. if (!jso)
  721. return NULL;
  722. jso->_delete = &json_object_string_delete;
  723. jso->_to_json_string = &json_object_string_to_json_string;
  724. if(len < LEN_DIRECT_STRING_DATA) {
  725. dstbuf = jso->o.c_string.str.data;
  726. } else {
  727. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  728. if (!jso->o.c_string.str.ptr)
  729. {
  730. json_object_generic_delete(jso);
  731. errno = ENOMEM;
  732. return NULL;
  733. }
  734. dstbuf = jso->o.c_string.str.ptr;
  735. }
  736. memcpy(dstbuf, (void *)s, len);
  737. dstbuf[len] = '\0';
  738. jso->o.c_string.len = len;
  739. return jso;
  740. }
  741. const char* json_object_get_string(struct json_object *jso)
  742. {
  743. if (!jso)
  744. return NULL;
  745. switch(jso->o_type)
  746. {
  747. case json_type_string:
  748. return get_string_component(jso);
  749. default:
  750. return json_object_to_json_string(jso);
  751. }
  752. }
  753. int json_object_get_string_len(const struct json_object *jso)
  754. {
  755. if (!jso)
  756. return 0;
  757. switch(jso->o_type)
  758. {
  759. case json_type_string:
  760. return jso->o.c_string.len;
  761. default:
  762. return 0;
  763. }
  764. }
  765. /* json_object_array */
  766. static int json_object_array_to_json_string(struct json_object* jso,
  767. struct printbuf *pb,
  768. int level,
  769. int flags)
  770. {
  771. int had_children = 0;
  772. int ii;
  773. sprintbuf(pb, "[");
  774. if (flags & JSON_C_TO_STRING_PRETTY)
  775. sprintbuf(pb, "\n");
  776. for(ii=0; ii < json_object_array_length(jso); ii++)
  777. {
  778. struct json_object *val;
  779. if (had_children)
  780. {
  781. sprintbuf(pb, ",");
  782. if (flags & JSON_C_TO_STRING_PRETTY)
  783. sprintbuf(pb, "\n");
  784. }
  785. had_children = 1;
  786. if (flags & JSON_C_TO_STRING_SPACED)
  787. sprintbuf(pb, " ");
  788. indent(pb, level + 1, flags);
  789. val = json_object_array_get_idx(jso, ii);
  790. if(val == NULL)
  791. sprintbuf(pb, "null");
  792. else
  793. val->_to_json_string(val, pb, level+1, flags);
  794. }
  795. if (flags & JSON_C_TO_STRING_PRETTY)
  796. {
  797. if (had_children)
  798. sprintbuf(pb, "\n");
  799. indent(pb,level,flags);
  800. }
  801. if (flags & JSON_C_TO_STRING_SPACED)
  802. return sprintbuf(pb, " ]");
  803. else
  804. return sprintbuf(pb, "]");
  805. }
  806. static void json_object_array_entry_free(void *data)
  807. {
  808. json_object_put((struct json_object*)data);
  809. }
  810. static void json_object_array_delete(struct json_object* jso)
  811. {
  812. array_list_free(jso->o.c_array);
  813. json_object_generic_delete(jso);
  814. }
  815. struct json_object* json_object_new_array(void)
  816. {
  817. struct json_object *jso = json_object_new(json_type_array);
  818. if (!jso)
  819. return NULL;
  820. jso->_delete = &json_object_array_delete;
  821. jso->_to_json_string = &json_object_array_to_json_string;
  822. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  823. if(jso->o.c_array == NULL)
  824. {
  825. free(jso);
  826. return NULL;
  827. }
  828. return jso;
  829. }
  830. struct array_list* json_object_get_array(const struct json_object *jso)
  831. {
  832. if (!jso)
  833. return NULL;
  834. switch(jso->o_type)
  835. {
  836. case json_type_array:
  837. return jso->o.c_array;
  838. default:
  839. return NULL;
  840. }
  841. }
  842. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  843. {
  844. array_list_sort(jso->o.c_array, sort_fn);
  845. }
  846. struct json_object* json_object_array_bsearch(
  847. const struct json_object *key,
  848. const struct json_object *jso,
  849. int (*sort_fn)(const void *, const void *))
  850. {
  851. struct json_object **result;
  852. result = (struct json_object **)array_list_bsearch(
  853. (const void **)&key, jso->o.c_array, sort_fn);
  854. if (!result)
  855. return NULL;
  856. return *result;
  857. }
  858. int json_object_array_length(const struct json_object *jso)
  859. {
  860. return array_list_length(jso->o.c_array);
  861. }
  862. int json_object_array_add(struct json_object *jso,struct json_object *val)
  863. {
  864. return array_list_add(jso->o.c_array, val);
  865. }
  866. int json_object_array_put_idx(struct json_object *jso, int idx,
  867. struct json_object *val)
  868. {
  869. return array_list_put_idx(jso->o.c_array, idx, val);
  870. }
  871. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  872. int idx)
  873. {
  874. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  875. }
  876. static int json_array_equal(struct json_object* jso1,
  877. struct json_object* jso2)
  878. {
  879. int len, i;
  880. len = json_object_array_length(jso1);
  881. if (len != json_object_array_length(jso2))
  882. return 0;
  883. for (i = 0; i < len; i++) {
  884. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  885. json_object_array_get_idx(jso2, i)))
  886. return 0;
  887. }
  888. return 1;
  889. }
  890. static int json_object_all_values_equal(struct json_object* jso1,
  891. struct json_object* jso2)
  892. {
  893. struct json_object_iter iter;
  894. struct json_object *sub;
  895. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  896. json_object_object_foreachC(jso1, iter) {
  897. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  898. (void**)&sub))
  899. return 0;
  900. if (!json_object_equal(iter.val, sub))
  901. return 0;
  902. }
  903. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  904. json_object_object_foreachC(jso2, iter) {
  905. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  906. (void**)&sub))
  907. return 0;
  908. }
  909. return 1;
  910. }
  911. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  912. {
  913. if (jso1 == jso2)
  914. return 1;
  915. if (!jso1 || !jso2)
  916. return 0;
  917. if (jso1->o_type != jso2->o_type)
  918. return 0;
  919. switch(jso1->o_type) {
  920. case json_type_boolean:
  921. return (jso1->o.c_boolean == jso2->o.c_boolean);
  922. case json_type_double:
  923. return (jso1->o.c_double == jso2->o.c_double);
  924. case json_type_int:
  925. return (jso1->o.c_int64 == jso2->o.c_int64);
  926. case json_type_string:
  927. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  928. memcmp(get_string_component(jso1),
  929. get_string_component(jso2),
  930. jso1->o.c_string.len) == 0);
  931. case json_type_object:
  932. return json_object_all_values_equal(jso1, jso2);
  933. case json_type_array:
  934. return json_array_equal(jso1, jso2);
  935. case json_type_null:
  936. return 1;
  937. };
  938. return 0;
  939. }