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

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