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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  257. {
  258. printbuf_memset(pb, -1, '\t', level);
  259. }
  260. else
  261. {
  262. printbuf_memset(pb, -1, ' ', level * 2);
  263. }
  264. }
  265. }
  266. /* json_object_object */
  267. static int json_object_object_to_json_string(struct json_object* jso,
  268. struct printbuf *pb,
  269. int level,
  270. int flags)
  271. {
  272. int had_children = 0;
  273. struct json_object_iter iter;
  274. sprintbuf(pb, "{" /*}*/);
  275. if (flags & JSON_C_TO_STRING_PRETTY)
  276. sprintbuf(pb, "\n");
  277. json_object_object_foreachC(jso, iter)
  278. {
  279. if (had_children)
  280. {
  281. sprintbuf(pb, ",");
  282. if (flags & JSON_C_TO_STRING_PRETTY)
  283. sprintbuf(pb, "\n");
  284. }
  285. had_children = 1;
  286. if (flags & JSON_C_TO_STRING_SPACED)
  287. sprintbuf(pb, " ");
  288. indent(pb, level+1, flags);
  289. sprintbuf(pb, "\"");
  290. json_escape_str(pb, iter.key, strlen(iter.key));
  291. if (flags & JSON_C_TO_STRING_SPACED)
  292. sprintbuf(pb, "\": ");
  293. else
  294. sprintbuf(pb, "\":");
  295. if(iter.val == NULL)
  296. sprintbuf(pb, "null");
  297. else
  298. iter.val->_to_json_string(iter.val, pb, level+1,flags);
  299. }
  300. if (flags & JSON_C_TO_STRING_PRETTY)
  301. {
  302. if (had_children)
  303. sprintbuf(pb, "\n");
  304. indent(pb,level,flags);
  305. }
  306. if (flags & JSON_C_TO_STRING_SPACED)
  307. return sprintbuf(pb, /*{*/ " }");
  308. else
  309. return sprintbuf(pb, /*{*/ "}");
  310. }
  311. static void json_object_lh_entry_free(struct lh_entry *ent)
  312. {
  313. free(ent->k);
  314. json_object_put((struct json_object*)ent->v);
  315. }
  316. static void json_object_object_delete(struct json_object* jso)
  317. {
  318. lh_table_free(jso->o.c_object);
  319. json_object_generic_delete(jso);
  320. }
  321. struct json_object* json_object_new_object(void)
  322. {
  323. struct json_object *jso = json_object_new(json_type_object);
  324. if (!jso)
  325. return NULL;
  326. jso->_delete = &json_object_object_delete;
  327. jso->_to_json_string = &json_object_object_to_json_string;
  328. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  329. NULL, &json_object_lh_entry_free);
  330. if (!jso->o.c_object)
  331. {
  332. json_object_generic_delete(jso);
  333. errno = ENOMEM;
  334. return NULL;
  335. }
  336. return jso;
  337. }
  338. struct lh_table* json_object_get_object(struct json_object *jso)
  339. {
  340. if (!jso)
  341. return NULL;
  342. switch(jso->o_type)
  343. {
  344. case json_type_object:
  345. return jso->o.c_object;
  346. default:
  347. return NULL;
  348. }
  349. }
  350. void json_object_object_add(struct json_object* jso, const char *key,
  351. struct json_object *val)
  352. {
  353. // We lookup the entry and replace the value, rather than just deleting
  354. // and re-adding it, so the existing key remains valid.
  355. json_object *existing_value = NULL;
  356. struct lh_entry *existing_entry;
  357. existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
  358. if (!existing_entry)
  359. {
  360. lh_table_insert(jso->o.c_object, strdup(key), val);
  361. return;
  362. }
  363. existing_value = (json_object *)existing_entry->v;
  364. if (existing_value)
  365. json_object_put(existing_value);
  366. existing_entry->v = val;
  367. }
  368. int json_object_object_length(struct json_object *jso)
  369. {
  370. return lh_table_length(jso->o.c_object);
  371. }
  372. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  373. {
  374. struct json_object *result = NULL;
  375. json_object_object_get_ex(jso, key, &result);
  376. return result;
  377. }
  378. json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
  379. {
  380. if (value != NULL)
  381. *value = NULL;
  382. if (NULL == jso)
  383. return FALSE;
  384. switch(jso->o_type)
  385. {
  386. case json_type_object:
  387. return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
  388. default:
  389. if (value != NULL)
  390. *value = NULL;
  391. return FALSE;
  392. }
  393. }
  394. void json_object_object_del(struct json_object* jso, const char *key)
  395. {
  396. lh_table_delete(jso->o.c_object, key);
  397. }
  398. /* json_object_boolean */
  399. static int json_object_boolean_to_json_string(struct json_object* jso,
  400. struct printbuf *pb,
  401. int level,
  402. int flags)
  403. {
  404. if (jso->o.c_boolean)
  405. return sprintbuf(pb, "true");
  406. else
  407. return sprintbuf(pb, "false");
  408. }
  409. struct json_object* json_object_new_boolean(json_bool b)
  410. {
  411. struct json_object *jso = json_object_new(json_type_boolean);
  412. if (!jso)
  413. return NULL;
  414. jso->_to_json_string = &json_object_boolean_to_json_string;
  415. jso->o.c_boolean = b;
  416. return jso;
  417. }
  418. json_bool json_object_get_boolean(struct json_object *jso)
  419. {
  420. if (!jso)
  421. return FALSE;
  422. switch(jso->o_type)
  423. {
  424. case json_type_boolean:
  425. return jso->o.c_boolean;
  426. case json_type_int:
  427. return (jso->o.c_int64 != 0);
  428. case json_type_double:
  429. return (jso->o.c_double != 0);
  430. case json_type_string:
  431. return (jso->o.c_string.len != 0);
  432. default:
  433. return FALSE;
  434. }
  435. }
  436. /* json_object_int */
  437. static int json_object_int_to_json_string(struct json_object* jso,
  438. struct printbuf *pb,
  439. int level,
  440. int flags)
  441. {
  442. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  443. }
  444. struct json_object* json_object_new_int(int32_t i)
  445. {
  446. struct json_object *jso = json_object_new(json_type_int);
  447. if (!jso)
  448. return NULL;
  449. jso->_to_json_string = &json_object_int_to_json_string;
  450. jso->o.c_int64 = i;
  451. return jso;
  452. }
  453. int32_t json_object_get_int(struct json_object *jso)
  454. {
  455. int64_t cint64;
  456. enum json_type o_type;
  457. if(!jso) return 0;
  458. o_type = jso->o_type;
  459. cint64 = jso->o.c_int64;
  460. if (o_type == json_type_string)
  461. {
  462. /*
  463. * Parse strings into 64-bit numbers, then use the
  464. * 64-to-32-bit number handling below.
  465. */
  466. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  467. return 0; /* whoops, it didn't work. */
  468. o_type = json_type_int;
  469. }
  470. switch(o_type) {
  471. case json_type_int:
  472. /* Make sure we return the correct values for out of range numbers. */
  473. if (cint64 <= INT32_MIN)
  474. return INT32_MIN;
  475. else if (cint64 >= INT32_MAX)
  476. return INT32_MAX;
  477. else
  478. return (int32_t)cint64;
  479. case json_type_double:
  480. return (int32_t)jso->o.c_double;
  481. case json_type_boolean:
  482. return jso->o.c_boolean;
  483. default:
  484. return 0;
  485. }
  486. }
  487. struct json_object* json_object_new_int64(int64_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. int64_t json_object_get_int64(struct json_object *jso)
  497. {
  498. int64_t cint;
  499. if (!jso)
  500. return 0;
  501. switch(jso->o_type)
  502. {
  503. case json_type_int:
  504. return jso->o.c_int64;
  505. case json_type_double:
  506. return (int64_t)jso->o.c_double;
  507. case json_type_boolean:
  508. return jso->o.c_boolean;
  509. case json_type_string:
  510. if (json_parse_int64(jso->o.c_string.str, &cint) == 0)
  511. return cint;
  512. default:
  513. return 0;
  514. }
  515. }
  516. /* json_object_double */
  517. static int json_object_double_to_json_string(struct json_object* jso,
  518. struct printbuf *pb,
  519. int level,
  520. int flags)
  521. {
  522. char buf[128], *p, *q;
  523. int size;
  524. /* Although JSON RFC does not support
  525. NaN or Infinity as numeric values
  526. ECMA 262 section 9.8.1 defines
  527. how to handle these cases as strings */
  528. if(isnan(jso->o.c_double))
  529. size = snprintf(buf, sizeof(buf), "NaN");
  530. else if(isinf(jso->o.c_double))
  531. if(jso->o.c_double > 0)
  532. size = snprintf(buf, sizeof(buf), "Infinity");
  533. else
  534. size = snprintf(buf, sizeof(buf), "-Infinity");
  535. else
  536. size = snprintf(buf, sizeof(buf), "%.17g", jso->o.c_double);
  537. p = strchr(buf, ',');
  538. if (p) {
  539. *p = '.';
  540. } else {
  541. p = strchr(buf, '.');
  542. }
  543. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  544. /* last useful digit, always keep 1 zero */
  545. p++;
  546. for (q=p ; *q ; q++) {
  547. if (*q!='0') p=q;
  548. }
  549. /* drop trailing zeroes */
  550. *(++p) = 0;
  551. size = p-buf;
  552. }
  553. printbuf_memappend(pb, buf, size);
  554. return size;
  555. }
  556. struct json_object* json_object_new_double(double d)
  557. {
  558. struct json_object *jso = json_object_new(json_type_double);
  559. if (!jso)
  560. return NULL;
  561. jso->_to_json_string = &json_object_double_to_json_string;
  562. jso->o.c_double = d;
  563. return jso;
  564. }
  565. struct json_object* json_object_new_double_s(double d, const char *ds)
  566. {
  567. struct json_object *jso = json_object_new_double(d);
  568. if (!jso)
  569. return NULL;
  570. char *new_ds = strdup(ds);
  571. if (!new_ds)
  572. {
  573. json_object_generic_delete(jso);
  574. errno = ENOMEM;
  575. return NULL;
  576. }
  577. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  578. new_ds, json_object_free_userdata);
  579. return jso;
  580. }
  581. int json_object_userdata_to_json_string(struct json_object *jso,
  582. struct printbuf *pb, int level, int flags)
  583. {
  584. int userdata_len = strlen((const char *)jso->_userdata);
  585. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  586. return userdata_len;
  587. }
  588. void json_object_free_userdata(struct json_object *jso, void *userdata)
  589. {
  590. free(userdata);
  591. }
  592. double json_object_get_double(struct json_object *jso)
  593. {
  594. double cdouble;
  595. char *errPtr = NULL;
  596. if(!jso) return 0.0;
  597. switch(jso->o_type) {
  598. case json_type_double:
  599. return jso->o.c_double;
  600. case json_type_int:
  601. return jso->o.c_int64;
  602. case json_type_boolean:
  603. return jso->o.c_boolean;
  604. case json_type_string:
  605. errno = 0;
  606. cdouble = strtod(jso->o.c_string.str,&errPtr);
  607. /* if conversion stopped at the first character, return 0.0 */
  608. if (errPtr == jso->o.c_string.str)
  609. return 0.0;
  610. /*
  611. * Check that the conversion terminated on something sensible
  612. *
  613. * For example, { "pay" : 123AB } would parse as 123.
  614. */
  615. if (*errPtr != '\0')
  616. return 0.0;
  617. /*
  618. * If strtod encounters a string which would exceed the
  619. * capacity of a double, it returns +/- HUGE_VAL and sets
  620. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  621. * from a conversion, so we need to check errno.
  622. *
  623. * Underflow also sets errno to ERANGE, but it returns 0 in
  624. * that case, which is what we will return anyway.
  625. *
  626. * See CERT guideline ERR30-C
  627. */
  628. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  629. (ERANGE == errno))
  630. cdouble = 0.0;
  631. return cdouble;
  632. default:
  633. return 0.0;
  634. }
  635. }
  636. /* json_object_string */
  637. static int json_object_string_to_json_string(struct json_object* jso,
  638. struct printbuf *pb,
  639. int level,
  640. int flags)
  641. {
  642. sprintbuf(pb, "\"");
  643. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  644. sprintbuf(pb, "\"");
  645. return 0;
  646. }
  647. static void json_object_string_delete(struct json_object* jso)
  648. {
  649. free(jso->o.c_string.str);
  650. json_object_generic_delete(jso);
  651. }
  652. struct json_object* json_object_new_string(const char *s)
  653. {
  654. struct json_object *jso = json_object_new(json_type_string);
  655. if (!jso)
  656. return NULL;
  657. jso->_delete = &json_object_string_delete;
  658. jso->_to_json_string = &json_object_string_to_json_string;
  659. jso->o.c_string.str = strdup(s);
  660. if (!jso->o.c_string.str)
  661. {
  662. json_object_generic_delete(jso);
  663. errno = ENOMEM;
  664. return NULL;
  665. }
  666. jso->o.c_string.len = strlen(s);
  667. return jso;
  668. }
  669. struct json_object* json_object_new_string_len(const char *s, int len)
  670. {
  671. struct json_object *jso = json_object_new(json_type_string);
  672. if (!jso)
  673. return NULL;
  674. jso->_delete = &json_object_string_delete;
  675. jso->_to_json_string = &json_object_string_to_json_string;
  676. jso->o.c_string.str = (char*)malloc(len + 1);
  677. if (!jso->o.c_string.str)
  678. {
  679. json_object_generic_delete(jso);
  680. errno = ENOMEM;
  681. return NULL;
  682. }
  683. memcpy(jso->o.c_string.str, (void *)s, len);
  684. jso->o.c_string.str[len] = '\0';
  685. jso->o.c_string.len = len;
  686. return jso;
  687. }
  688. const char* json_object_get_string(struct json_object *jso)
  689. {
  690. if (!jso)
  691. return NULL;
  692. switch(jso->o_type)
  693. {
  694. case json_type_string:
  695. return jso->o.c_string.str;
  696. default:
  697. return json_object_to_json_string(jso);
  698. }
  699. }
  700. int json_object_get_string_len(struct json_object *jso)
  701. {
  702. if (!jso)
  703. return 0;
  704. switch(jso->o_type)
  705. {
  706. case json_type_string:
  707. return jso->o.c_string.len;
  708. default:
  709. return 0;
  710. }
  711. }
  712. /* json_object_array */
  713. static int json_object_array_to_json_string(struct json_object* jso,
  714. struct printbuf *pb,
  715. int level,
  716. int flags)
  717. {
  718. int had_children = 0;
  719. int ii;
  720. sprintbuf(pb, "[");
  721. if (flags & JSON_C_TO_STRING_PRETTY)
  722. sprintbuf(pb, "\n");
  723. for(ii=0; ii < json_object_array_length(jso); ii++)
  724. {
  725. struct json_object *val;
  726. if (had_children)
  727. {
  728. sprintbuf(pb, ",");
  729. if (flags & JSON_C_TO_STRING_PRETTY)
  730. sprintbuf(pb, "\n");
  731. }
  732. had_children = 1;
  733. if (flags & JSON_C_TO_STRING_SPACED)
  734. sprintbuf(pb, " ");
  735. indent(pb, level + 1, flags);
  736. val = json_object_array_get_idx(jso, ii);
  737. if(val == NULL)
  738. sprintbuf(pb, "null");
  739. else
  740. val->_to_json_string(val, pb, level+1, flags);
  741. }
  742. if (flags & JSON_C_TO_STRING_PRETTY)
  743. {
  744. if (had_children)
  745. sprintbuf(pb, "\n");
  746. indent(pb,level,flags);
  747. }
  748. if (flags & JSON_C_TO_STRING_SPACED)
  749. return sprintbuf(pb, " ]");
  750. else
  751. return sprintbuf(pb, "]");
  752. }
  753. static void json_object_array_entry_free(void *data)
  754. {
  755. json_object_put((struct json_object*)data);
  756. }
  757. static void json_object_array_delete(struct json_object* jso)
  758. {
  759. array_list_free(jso->o.c_array);
  760. json_object_generic_delete(jso);
  761. }
  762. struct json_object* json_object_new_array(void)
  763. {
  764. struct json_object *jso = json_object_new(json_type_array);
  765. if (!jso)
  766. return NULL;
  767. jso->_delete = &json_object_array_delete;
  768. jso->_to_json_string = &json_object_array_to_json_string;
  769. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  770. return jso;
  771. }
  772. struct array_list* json_object_get_array(struct json_object *jso)
  773. {
  774. if (!jso)
  775. return NULL;
  776. switch(jso->o_type)
  777. {
  778. case json_type_array:
  779. return jso->o.c_array;
  780. default:
  781. return NULL;
  782. }
  783. }
  784. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  785. {
  786. array_list_sort(jso->o.c_array, sort_fn);
  787. }
  788. struct json_object* json_object_array_bsearch(
  789. const struct json_object *key,
  790. const struct json_object *jso,
  791. int (*sort_fn)(const void *, const void *))
  792. {
  793. struct json_object **result;
  794. result = (struct json_object **)array_list_bsearch(
  795. (const void **)&key, jso->o.c_array, sort_fn);
  796. if (!result)
  797. return NULL;
  798. return *result;
  799. }
  800. int json_object_array_length(struct json_object *jso)
  801. {
  802. return array_list_length(jso->o.c_array);
  803. }
  804. int json_object_array_add(struct json_object *jso,struct json_object *val)
  805. {
  806. return array_list_add(jso->o.c_array, val);
  807. }
  808. int json_object_array_put_idx(struct json_object *jso, int idx,
  809. struct json_object *val)
  810. {
  811. return array_list_put_idx(jso->o.c_array, idx, val);
  812. }
  813. struct json_object* json_object_array_get_idx(struct json_object *jso,
  814. int idx)
  815. {
  816. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  817. }