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

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