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

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