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

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