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

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