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