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

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