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

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