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

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