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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 <assert.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <errno.h>
  20. #include "debug.h"
  21. #include "printbuf.h"
  22. #include "linkhash.h"
  23. #include "arraylist.h"
  24. #include "json_inttypes.h"
  25. #include "json_object.h"
  26. #include "json_object_private.h"
  27. #include "json_util.h"
  28. #include "math_compat.h"
  29. #if !defined(HAVE_STRDUP) && defined(_MSC_VER)
  30. /* MSC has the version as _strdup */
  31. # define strdup _strdup
  32. #elif !defined(HAVE_STRDUP)
  33. # error You do not have strdup on your system.
  34. #endif /* HAVE_STRDUP */
  35. #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
  36. /* MSC has the version as _snprintf */
  37. # define snprintf _snprintf
  38. #elif !defined(HAVE_SNPRINTF)
  39. # error You do not have snprintf on your system.
  40. #endif /* HAVE_SNPRINTF */
  41. // Don't define this. It's not thread-safe.
  42. /* #define REFCOUNT_DEBUG 1 */
  43. const char *json_number_chars = "0123456789.+-eE";
  44. const char *json_hex_chars = "0123456789abcdefABCDEF";
  45. static void json_object_generic_delete(struct json_object* jso);
  46. static struct json_object* json_object_new(enum json_type o_type);
  47. static json_object_to_json_string_fn json_object_object_to_json_string;
  48. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  49. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  50. static json_object_to_json_string_fn json_object_int_to_json_string;
  51. static json_object_to_json_string_fn json_object_string_to_json_string;
  52. static json_object_to_json_string_fn json_object_array_to_json_string;
  53. /* ref count debugging */
  54. #ifdef REFCOUNT_DEBUG
  55. static struct lh_table *json_object_table;
  56. static void json_object_init(void) __attribute__ ((constructor));
  57. static void json_object_init(void) {
  58. MC_DEBUG("json_object_init: creating object table\n");
  59. json_object_table = lh_kptr_table_new(128, NULL);
  60. }
  61. static void json_object_fini(void) __attribute__ ((destructor));
  62. static void json_object_fini(void)
  63. {
  64. struct lh_entry *ent;
  65. if (MC_GET_DEBUG())
  66. {
  67. if (json_object_table->count)
  68. {
  69. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  70. json_object_table->count);
  71. lh_foreach(json_object_table, ent)
  72. {
  73. struct json_object* obj =
  74. (struct json_object*) lh_entry_v(ent);
  75. MC_DEBUG("\t%s:%p\n",
  76. json_type_to_name(obj->o_type), obj);
  77. }
  78. }
  79. }
  80. MC_DEBUG("json_object_fini: freeing object table\n");
  81. lh_table_free(json_object_table);
  82. }
  83. #endif /* REFCOUNT_DEBUG */
  84. /* helper for accessing the optimized string data component in json_object
  85. */
  86. static const char *
  87. get_string_component(const struct json_object *jso)
  88. {
  89. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  90. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  91. }
  92. /* string escaping */
  93. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  94. {
  95. int pos = 0, start_offset = 0;
  96. unsigned char c;
  97. while (len--)
  98. {
  99. c = str[pos];
  100. switch(c)
  101. {
  102. case '\b':
  103. case '\n':
  104. case '\r':
  105. case '\t':
  106. case '\f':
  107. case '"':
  108. case '\\':
  109. case '/':
  110. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  111. {
  112. pos++;
  113. break;
  114. }
  115. if(pos - start_offset > 0)
  116. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  117. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  118. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  119. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  120. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  121. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  122. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  123. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  124. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  125. start_offset = ++pos;
  126. break;
  127. default:
  128. if(c < ' ')
  129. {
  130. if(pos - start_offset > 0)
  131. printbuf_memappend(pb,
  132. str + start_offset,
  133. pos - start_offset);
  134. sprintbuf(pb, "\\u00%c%c",
  135. json_hex_chars[c >> 4],
  136. json_hex_chars[c & 0xf]);
  137. start_offset = ++pos;
  138. } else
  139. pos++;
  140. }
  141. }
  142. if (pos - start_offset > 0)
  143. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  144. return 0;
  145. }
  146. /* reference counting */
  147. extern struct json_object* json_object_get(struct json_object *jso)
  148. {
  149. if (jso)
  150. jso->_ref_count++;
  151. return jso;
  152. }
  153. int json_object_put(struct json_object *jso)
  154. {
  155. if(jso)
  156. {
  157. jso->_ref_count--;
  158. if(!jso->_ref_count)
  159. {
  160. if (jso->_user_delete)
  161. jso->_user_delete(jso, jso->_userdata);
  162. jso->_delete(jso);
  163. return 1;
  164. }
  165. }
  166. return 0;
  167. }
  168. /* generic object construction and destruction parts */
  169. static void json_object_generic_delete(struct json_object* jso)
  170. {
  171. #ifdef REFCOUNT_DEBUG
  172. MC_DEBUG("json_object_delete_%s: %p\n",
  173. json_type_to_name(jso->o_type), jso);
  174. lh_table_delete(json_object_table, jso);
  175. #endif /* REFCOUNT_DEBUG */
  176. printbuf_free(jso->_pb);
  177. free(jso);
  178. }
  179. static struct json_object* json_object_new(enum json_type o_type)
  180. {
  181. struct json_object *jso;
  182. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  183. if (!jso)
  184. return NULL;
  185. jso->o_type = o_type;
  186. jso->_ref_count = 1;
  187. jso->_delete = &json_object_generic_delete;
  188. #ifdef REFCOUNT_DEBUG
  189. lh_table_insert(json_object_table, jso, jso);
  190. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  191. #endif /* REFCOUNT_DEBUG */
  192. return jso;
  193. }
  194. /* type checking functions */
  195. int json_object_is_type(const struct json_object *jso, enum json_type type)
  196. {
  197. if (!jso)
  198. return (type == json_type_null);
  199. return (jso->o_type == type);
  200. }
  201. enum json_type json_object_get_type(const struct json_object *jso)
  202. {
  203. if (!jso)
  204. return json_type_null;
  205. return jso->o_type;
  206. }
  207. void* json_object_get_userdata(json_object *jso) {
  208. return jso ? jso->_userdata : NULL;
  209. }
  210. void json_object_set_userdata(json_object *jso, void *userdata,
  211. json_object_delete_fn *user_delete)
  212. {
  213. // Can't return failure, so abort if we can't perform the operation.
  214. assert(jso != NULL);
  215. // First, clean up any previously existing user info
  216. if (jso->_user_delete)
  217. jso->_user_delete(jso, jso->_userdata);
  218. jso->_userdata = userdata;
  219. jso->_user_delete = user_delete;
  220. }
  221. /* set a custom conversion to string */
  222. void json_object_set_serializer(json_object *jso,
  223. json_object_to_json_string_fn to_string_func,
  224. void *userdata,
  225. json_object_delete_fn *user_delete)
  226. {
  227. json_object_set_userdata(jso, userdata, user_delete);
  228. if (to_string_func == NULL)
  229. {
  230. // Reset to the standard serialization function
  231. switch(jso->o_type)
  232. {
  233. case json_type_null:
  234. jso->_to_json_string = NULL;
  235. break;
  236. case json_type_boolean:
  237. jso->_to_json_string = &json_object_boolean_to_json_string;
  238. break;
  239. case json_type_double:
  240. jso->_to_json_string = &json_object_double_to_json_string_default;
  241. break;
  242. case json_type_int:
  243. jso->_to_json_string = &json_object_int_to_json_string;
  244. break;
  245. case json_type_object:
  246. jso->_to_json_string = &json_object_object_to_json_string;
  247. break;
  248. case json_type_array:
  249. jso->_to_json_string = &json_object_array_to_json_string;
  250. break;
  251. case json_type_string:
  252. jso->_to_json_string = &json_object_string_to_json_string;
  253. break;
  254. }
  255. return;
  256. }
  257. jso->_to_json_string = to_string_func;
  258. }
  259. /* extended conversion to string */
  260. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  261. {
  262. const char *r = NULL;
  263. size_t s = 0;
  264. if (!jso)
  265. {
  266. s = 4;
  267. r = "null";
  268. }
  269. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  270. {
  271. printbuf_reset(jso->_pb);
  272. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  273. {
  274. s = (size_t)jso->_pb->bpos;
  275. r = jso->_pb->buf;
  276. }
  277. }
  278. if (length)
  279. *length = s;
  280. return r;
  281. }
  282. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  283. {
  284. return json_object_to_json_string_length(jso, flags, NULL);
  285. }
  286. /* backwards-compatible conversion to string */
  287. const char* json_object_to_json_string(struct json_object *jso)
  288. {
  289. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  290. }
  291. static void indent(struct printbuf *pb, int level, int flags)
  292. {
  293. if (flags & JSON_C_TO_STRING_PRETTY)
  294. {
  295. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  296. {
  297. printbuf_memset(pb, -1, '\t', level);
  298. }
  299. else
  300. {
  301. printbuf_memset(pb, -1, ' ', level * 2);
  302. }
  303. }
  304. }
  305. /* json_object_object */
  306. static int json_object_object_to_json_string(struct json_object* jso,
  307. struct printbuf *pb,
  308. int level,
  309. int flags)
  310. {
  311. int had_children = 0;
  312. struct json_object_iter iter;
  313. sprintbuf(pb, "{" /*}*/);
  314. if (flags & JSON_C_TO_STRING_PRETTY)
  315. sprintbuf(pb, "\n");
  316. json_object_object_foreachC(jso, iter)
  317. {
  318. if (had_children)
  319. {
  320. sprintbuf(pb, ",");
  321. if (flags & JSON_C_TO_STRING_PRETTY)
  322. sprintbuf(pb, "\n");
  323. }
  324. had_children = 1;
  325. if (flags & JSON_C_TO_STRING_SPACED)
  326. sprintbuf(pb, " ");
  327. indent(pb, level+1, flags);
  328. sprintbuf(pb, "\"");
  329. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  330. if (flags & JSON_C_TO_STRING_SPACED)
  331. sprintbuf(pb, "\": ");
  332. else
  333. sprintbuf(pb, "\":");
  334. if(iter.val == NULL)
  335. sprintbuf(pb, "null");
  336. else
  337. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  338. return -1;
  339. }
  340. if (flags & JSON_C_TO_STRING_PRETTY)
  341. {
  342. if (had_children)
  343. sprintbuf(pb, "\n");
  344. indent(pb,level,flags);
  345. }
  346. if (flags & JSON_C_TO_STRING_SPACED)
  347. return sprintbuf(pb, /*{*/ " }");
  348. else
  349. return sprintbuf(pb, /*{*/ "}");
  350. }
  351. static void json_object_lh_entry_free(struct lh_entry *ent)
  352. {
  353. if (!ent->k_is_constant)
  354. free(lh_entry_k(ent));
  355. json_object_put((struct json_object*)lh_entry_v(ent));
  356. }
  357. static void json_object_object_delete(struct json_object* jso)
  358. {
  359. lh_table_free(jso->o.c_object);
  360. json_object_generic_delete(jso);
  361. }
  362. struct json_object* json_object_new_object(void)
  363. {
  364. struct json_object *jso = json_object_new(json_type_object);
  365. if (!jso)
  366. return NULL;
  367. jso->_delete = &json_object_object_delete;
  368. jso->_to_json_string = &json_object_object_to_json_string;
  369. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  370. &json_object_lh_entry_free);
  371. if (!jso->o.c_object)
  372. {
  373. json_object_generic_delete(jso);
  374. errno = ENOMEM;
  375. return NULL;
  376. }
  377. return jso;
  378. }
  379. struct lh_table* json_object_get_object(const struct json_object *jso)
  380. {
  381. if (!jso)
  382. return NULL;
  383. switch(jso->o_type)
  384. {
  385. case json_type_object:
  386. return jso->o.c_object;
  387. default:
  388. return NULL;
  389. }
  390. }
  391. int json_object_object_add_ex(struct json_object* jso,
  392. const char *const key,
  393. struct json_object *const val,
  394. const unsigned opts)
  395. {
  396. assert(json_object_get_type(jso) == json_type_object);
  397. // We lookup the entry and replace the value, rather than just deleting
  398. // and re-adding it, so the existing key remains valid.
  399. json_object *existing_value = NULL;
  400. struct lh_entry *existing_entry;
  401. const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
  402. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  403. lh_table_lookup_entry_w_hash(jso->o.c_object,
  404. (const void *)key, hash);
  405. // The caller must avoid creating loops in the object tree, but do a
  406. // quick check anyway to make sure we're not creating a trivial loop.
  407. if (jso == val)
  408. return -1;
  409. if (!existing_entry)
  410. {
  411. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  412. (const void *)key : strdup(key);
  413. if (k == NULL)
  414. return -1;
  415. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  416. }
  417. existing_value = (json_object *) lh_entry_v(existing_entry);
  418. if (existing_value)
  419. json_object_put(existing_value);
  420. existing_entry->v = val;
  421. return 0;
  422. }
  423. int json_object_object_add(struct json_object* jso, const char *key,
  424. struct json_object *val)
  425. {
  426. return json_object_object_add_ex(jso, key, val, 0);
  427. }
  428. int json_object_object_length(const struct json_object *jso)
  429. {
  430. assert(json_object_get_type(jso) == json_type_object);
  431. return lh_table_length(jso->o.c_object);
  432. }
  433. struct json_object* json_object_object_get(const struct json_object* jso,
  434. const char *key)
  435. {
  436. struct json_object *result = NULL;
  437. json_object_object_get_ex(jso, key, &result);
  438. return result;
  439. }
  440. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  441. struct json_object **value)
  442. {
  443. if (value != NULL)
  444. *value = NULL;
  445. if (NULL == jso)
  446. return FALSE;
  447. switch(jso->o_type)
  448. {
  449. case json_type_object:
  450. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  451. (void**) value);
  452. default:
  453. if (value != NULL)
  454. *value = NULL;
  455. return FALSE;
  456. }
  457. }
  458. void json_object_object_del(struct json_object* jso, const char *key)
  459. {
  460. assert(json_object_get_type(jso) == json_type_object);
  461. lh_table_delete(jso->o.c_object, key);
  462. }
  463. /* json_object_boolean */
  464. static int json_object_boolean_to_json_string(struct json_object* jso,
  465. struct printbuf *pb,
  466. int level,
  467. int flags)
  468. {
  469. if (jso->o.c_boolean)
  470. return sprintbuf(pb, "true");
  471. return sprintbuf(pb, "false");
  472. }
  473. struct json_object* json_object_new_boolean(json_bool b)
  474. {
  475. struct json_object *jso = json_object_new(json_type_boolean);
  476. if (!jso)
  477. return NULL;
  478. jso->_to_json_string = &json_object_boolean_to_json_string;
  479. jso->o.c_boolean = b;
  480. return jso;
  481. }
  482. json_bool json_object_get_boolean(const struct json_object *jso)
  483. {
  484. if (!jso)
  485. return FALSE;
  486. switch(jso->o_type)
  487. {
  488. case json_type_boolean:
  489. return jso->o.c_boolean;
  490. case json_type_int:
  491. return (jso->o.c_int64 != 0);
  492. case json_type_double:
  493. return (jso->o.c_double != 0);
  494. case json_type_string:
  495. return (jso->o.c_string.len != 0);
  496. default:
  497. return FALSE;
  498. }
  499. }
  500. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  501. if (!jso || jso->o_type!=json_type_boolean)
  502. return 0;
  503. jso->o.c_boolean=new_value;
  504. return 1;
  505. }
  506. /* json_object_int */
  507. static int json_object_int_to_json_string(struct json_object* jso,
  508. struct printbuf *pb,
  509. int level,
  510. int flags)
  511. {
  512. return sprintbuf(pb, "%" PRId64, jso->o.c_int64);
  513. }
  514. struct json_object* json_object_new_int(int32_t i)
  515. {
  516. struct json_object *jso = json_object_new(json_type_int);
  517. if (!jso)
  518. return NULL;
  519. jso->_to_json_string = &json_object_int_to_json_string;
  520. jso->o.c_int64 = i;
  521. return jso;
  522. }
  523. int32_t json_object_get_int(const struct json_object *jso)
  524. {
  525. int64_t cint64;
  526. enum json_type o_type;
  527. if(!jso) return 0;
  528. o_type = jso->o_type;
  529. cint64 = jso->o.c_int64;
  530. if (o_type == json_type_string)
  531. {
  532. /*
  533. * Parse strings into 64-bit numbers, then use the
  534. * 64-to-32-bit number handling below.
  535. */
  536. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  537. return 0; /* whoops, it didn't work. */
  538. o_type = json_type_int;
  539. }
  540. switch(o_type) {
  541. case json_type_int:
  542. /* Make sure we return the correct values for out of range numbers. */
  543. if (cint64 <= INT32_MIN)
  544. return INT32_MIN;
  545. if (cint64 >= INT32_MAX)
  546. return INT32_MAX;
  547. return (int32_t) cint64;
  548. case json_type_double:
  549. return (int32_t)jso->o.c_double;
  550. case json_type_boolean:
  551. return jso->o.c_boolean;
  552. default:
  553. return 0;
  554. }
  555. }
  556. int json_object_set_int(struct json_object *jso,int new_value){
  557. if (!jso || jso->o_type!=json_type_int)
  558. return 0;
  559. jso->o.c_int64=new_value;
  560. return 1;
  561. }
  562. struct json_object* json_object_new_int64(int64_t i)
  563. {
  564. struct json_object *jso = json_object_new(json_type_int);
  565. if (!jso)
  566. return NULL;
  567. jso->_to_json_string = &json_object_int_to_json_string;
  568. jso->o.c_int64 = i;
  569. return jso;
  570. }
  571. int64_t json_object_get_int64(const struct json_object *jso)
  572. {
  573. int64_t cint;
  574. if (!jso)
  575. return 0;
  576. switch(jso->o_type)
  577. {
  578. case json_type_int:
  579. return jso->o.c_int64;
  580. case json_type_double:
  581. return (int64_t)jso->o.c_double;
  582. case json_type_boolean:
  583. return jso->o.c_boolean;
  584. case json_type_string:
  585. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  586. return cint;
  587. default:
  588. return 0;
  589. }
  590. }
  591. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  592. if (!jso || jso->o_type!=json_type_int)
  593. return 0;
  594. jso->o.c_int64=new_value;
  595. return 1;
  596. }
  597. /* json_object_double */
  598. static int json_object_double_to_json_string_format(struct json_object* jso,
  599. struct printbuf *pb,
  600. int level,
  601. int flags,
  602. const char *format)
  603. {
  604. char buf[128], *p, *q;
  605. int size;
  606. /* Although JSON RFC does not support
  607. NaN or Infinity as numeric values
  608. ECMA 262 section 9.8.1 defines
  609. how to handle these cases as strings */
  610. if(isnan(jso->o.c_double))
  611. size = snprintf(buf, sizeof(buf), "NaN");
  612. else if(isinf(jso->o.c_double))
  613. if(jso->o.c_double > 0)
  614. size = snprintf(buf, sizeof(buf), "Infinity");
  615. else
  616. size = snprintf(buf, sizeof(buf), "-Infinity");
  617. else
  618. size = snprintf(buf, sizeof(buf),
  619. format ? format : "%.17g", jso->o.c_double);
  620. p = strchr(buf, ',');
  621. if (p) {
  622. *p = '.';
  623. } else {
  624. p = strchr(buf, '.');
  625. }
  626. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  627. /* last useful digit, always keep 1 zero */
  628. p++;
  629. for (q=p ; *q ; q++) {
  630. if (*q!='0') p=q;
  631. }
  632. /* drop trailing zeroes */
  633. *(++p) = 0;
  634. size = p-buf;
  635. }
  636. printbuf_memappend(pb, buf, size);
  637. return size;
  638. }
  639. static int json_object_double_to_json_string_default(struct json_object* jso,
  640. struct printbuf *pb,
  641. int level,
  642. int flags)
  643. {
  644. return json_object_double_to_json_string_format(jso, pb, level, flags,
  645. NULL);
  646. }
  647. int json_object_double_to_json_string(struct json_object* jso,
  648. struct printbuf *pb,
  649. int level,
  650. int flags)
  651. {
  652. return json_object_double_to_json_string_format(jso, pb, level, flags,
  653. (const char *)jso->_userdata);
  654. }
  655. struct json_object* json_object_new_double(double d)
  656. {
  657. struct json_object *jso = json_object_new(json_type_double);
  658. if (!jso)
  659. return NULL;
  660. jso->_to_json_string = &json_object_double_to_json_string_default;
  661. jso->o.c_double = d;
  662. return jso;
  663. }
  664. struct json_object* json_object_new_double_s(double d, const char *ds)
  665. {
  666. struct json_object *jso = json_object_new_double(d);
  667. if (!jso)
  668. return NULL;
  669. char *new_ds = strdup(ds);
  670. if (!new_ds)
  671. {
  672. json_object_generic_delete(jso);
  673. errno = ENOMEM;
  674. return NULL;
  675. }
  676. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  677. new_ds, json_object_free_userdata);
  678. return jso;
  679. }
  680. int json_object_userdata_to_json_string(struct json_object *jso,
  681. struct printbuf *pb, int level, int flags)
  682. {
  683. int userdata_len = strlen((const char *)jso->_userdata);
  684. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  685. return userdata_len;
  686. }
  687. void json_object_free_userdata(struct json_object *jso, void *userdata)
  688. {
  689. free(userdata);
  690. }
  691. double json_object_get_double(const struct json_object *jso)
  692. {
  693. double cdouble;
  694. char *errPtr = NULL;
  695. if(!jso) return 0.0;
  696. switch(jso->o_type) {
  697. case json_type_double:
  698. return jso->o.c_double;
  699. case json_type_int:
  700. return jso->o.c_int64;
  701. case json_type_boolean:
  702. return jso->o.c_boolean;
  703. case json_type_string:
  704. errno = 0;
  705. cdouble = strtod(get_string_component(jso), &errPtr);
  706. /* if conversion stopped at the first character, return 0.0 */
  707. if (errPtr == get_string_component(jso))
  708. return 0.0;
  709. /*
  710. * Check that the conversion terminated on something sensible
  711. *
  712. * For example, { "pay" : 123AB } would parse as 123.
  713. */
  714. if (*errPtr != '\0')
  715. return 0.0;
  716. /*
  717. * If strtod encounters a string which would exceed the
  718. * capacity of a double, it returns +/- HUGE_VAL and sets
  719. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  720. * from a conversion, so we need to check errno.
  721. *
  722. * Underflow also sets errno to ERANGE, but it returns 0 in
  723. * that case, which is what we will return anyway.
  724. *
  725. * See CERT guideline ERR30-C
  726. */
  727. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  728. (ERANGE == errno))
  729. cdouble = 0.0;
  730. return cdouble;
  731. default:
  732. return 0.0;
  733. }
  734. }
  735. int json_object_set_double(struct json_object *jso,double new_value){
  736. if (!jso || jso->o_type!=json_type_double)
  737. return 0;
  738. jso->o.c_double=new_value;
  739. return 1;
  740. }
  741. /* json_object_string */
  742. static int json_object_string_to_json_string(struct json_object* jso,
  743. struct printbuf *pb,
  744. int level,
  745. int flags)
  746. {
  747. sprintbuf(pb, "\"");
  748. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  749. sprintbuf(pb, "\"");
  750. return 0;
  751. }
  752. static void json_object_string_delete(struct json_object* jso)
  753. {
  754. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  755. free(jso->o.c_string.str.ptr);
  756. json_object_generic_delete(jso);
  757. }
  758. struct json_object* json_object_new_string(const char *s)
  759. {
  760. struct json_object *jso = json_object_new(json_type_string);
  761. if (!jso)
  762. return NULL;
  763. jso->_delete = &json_object_string_delete;
  764. jso->_to_json_string = &json_object_string_to_json_string;
  765. jso->o.c_string.len = strlen(s);
  766. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  767. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  768. } else {
  769. jso->o.c_string.str.ptr = strdup(s);
  770. if (!jso->o.c_string.str.ptr)
  771. {
  772. json_object_generic_delete(jso);
  773. errno = ENOMEM;
  774. return NULL;
  775. }
  776. }
  777. return jso;
  778. }
  779. struct json_object* json_object_new_string_len(const char *s, int len)
  780. {
  781. char *dstbuf;
  782. struct json_object *jso = json_object_new(json_type_string);
  783. if (!jso)
  784. return NULL;
  785. jso->_delete = &json_object_string_delete;
  786. jso->_to_json_string = &json_object_string_to_json_string;
  787. if(len < LEN_DIRECT_STRING_DATA) {
  788. dstbuf = jso->o.c_string.str.data;
  789. } else {
  790. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  791. if (!jso->o.c_string.str.ptr)
  792. {
  793. json_object_generic_delete(jso);
  794. errno = ENOMEM;
  795. return NULL;
  796. }
  797. dstbuf = jso->o.c_string.str.ptr;
  798. }
  799. memcpy(dstbuf, (const void *)s, len);
  800. dstbuf[len] = '\0';
  801. jso->o.c_string.len = len;
  802. return jso;
  803. }
  804. const char* json_object_get_string(struct json_object *jso)
  805. {
  806. if (!jso)
  807. return NULL;
  808. switch(jso->o_type)
  809. {
  810. case json_type_string:
  811. return get_string_component(jso);
  812. default:
  813. return json_object_to_json_string(jso);
  814. }
  815. }
  816. int json_object_get_string_len(const struct json_object *jso)
  817. {
  818. if (!jso)
  819. return 0;
  820. switch(jso->o_type)
  821. {
  822. case json_type_string:
  823. return jso->o.c_string.len;
  824. default:
  825. return 0;
  826. }
  827. }
  828. int json_object_set_string(json_object* jso, const char* s) {
  829. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  830. }
  831. int json_object_set_string_len(json_object* jso, const char* s, int len){
  832. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  833. char *dstbuf;
  834. if (len<LEN_DIRECT_STRING_DATA) {
  835. dstbuf=jso->o.c_string.str.data;
  836. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  837. } else {
  838. dstbuf=(char *)malloc(len+1);
  839. if (dstbuf==NULL) return 0;
  840. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  841. jso->o.c_string.str.ptr=dstbuf;
  842. }
  843. jso->o.c_string.len=len;
  844. memcpy(dstbuf, (const void *)s, len);
  845. dstbuf[len] = '\0';
  846. return 1;
  847. }
  848. /* json_object_array */
  849. static int json_object_array_to_json_string(struct json_object* jso,
  850. struct printbuf *pb,
  851. int level,
  852. int flags)
  853. {
  854. int had_children = 0;
  855. size_t ii;
  856. sprintbuf(pb, "[");
  857. if (flags & JSON_C_TO_STRING_PRETTY)
  858. sprintbuf(pb, "\n");
  859. for(ii=0; ii < json_object_array_length(jso); ii++)
  860. {
  861. struct json_object *val;
  862. if (had_children)
  863. {
  864. sprintbuf(pb, ",");
  865. if (flags & JSON_C_TO_STRING_PRETTY)
  866. sprintbuf(pb, "\n");
  867. }
  868. had_children = 1;
  869. if (flags & JSON_C_TO_STRING_SPACED)
  870. sprintbuf(pb, " ");
  871. indent(pb, level + 1, flags);
  872. val = json_object_array_get_idx(jso, ii);
  873. if(val == NULL)
  874. sprintbuf(pb, "null");
  875. else
  876. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  877. return -1;
  878. }
  879. if (flags & JSON_C_TO_STRING_PRETTY)
  880. {
  881. if (had_children)
  882. sprintbuf(pb, "\n");
  883. indent(pb,level,flags);
  884. }
  885. if (flags & JSON_C_TO_STRING_SPACED)
  886. return sprintbuf(pb, " ]");
  887. return sprintbuf(pb, "]");
  888. }
  889. static void json_object_array_entry_free(void *data)
  890. {
  891. json_object_put((struct json_object*)data);
  892. }
  893. static void json_object_array_delete(struct json_object* jso)
  894. {
  895. array_list_free(jso->o.c_array);
  896. json_object_generic_delete(jso);
  897. }
  898. struct json_object* json_object_new_array(void)
  899. {
  900. struct json_object *jso = json_object_new(json_type_array);
  901. if (!jso)
  902. return NULL;
  903. jso->_delete = &json_object_array_delete;
  904. jso->_to_json_string = &json_object_array_to_json_string;
  905. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  906. if(jso->o.c_array == NULL)
  907. {
  908. free(jso);
  909. return NULL;
  910. }
  911. return jso;
  912. }
  913. struct array_list* json_object_get_array(const struct json_object *jso)
  914. {
  915. if (!jso)
  916. return NULL;
  917. switch(jso->o_type)
  918. {
  919. case json_type_array:
  920. return jso->o.c_array;
  921. default:
  922. return NULL;
  923. }
  924. }
  925. void json_object_array_sort(struct json_object *jso,
  926. int(*sort_fn)(const void *, const void *))
  927. {
  928. assert(json_object_get_type(jso) == json_type_array);
  929. array_list_sort(jso->o.c_array, sort_fn);
  930. }
  931. struct json_object* json_object_array_bsearch(
  932. const struct json_object *key,
  933. const struct json_object *jso,
  934. int (*sort_fn)(const void *, const void *))
  935. {
  936. struct json_object **result;
  937. assert(json_object_get_type(jso) == json_type_array);
  938. result = (struct json_object **)array_list_bsearch(
  939. (const void **)&key, jso->o.c_array, sort_fn);
  940. if (!result)
  941. return NULL;
  942. return *result;
  943. }
  944. size_t json_object_array_length(const struct json_object *jso)
  945. {
  946. assert(json_object_get_type(jso) == json_type_array);
  947. return array_list_length(jso->o.c_array);
  948. }
  949. int json_object_array_add(struct json_object *jso,struct json_object *val)
  950. {
  951. assert(json_object_get_type(jso) == json_type_array);
  952. return array_list_add(jso->o.c_array, val);
  953. }
  954. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  955. struct json_object *val)
  956. {
  957. assert(json_object_get_type(jso) == json_type_array);
  958. return array_list_put_idx(jso->o.c_array, idx, val);
  959. }
  960. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  961. {
  962. assert(json_object_get_type(jso) == json_type_array);
  963. return array_list_del_idx(jso->o.c_array, idx, count);
  964. }
  965. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  966. size_t idx)
  967. {
  968. assert(json_object_get_type(jso) == json_type_array);
  969. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  970. }
  971. static int json_array_equal(struct json_object* jso1,
  972. struct json_object* jso2)
  973. {
  974. size_t len, i;
  975. len = json_object_array_length(jso1);
  976. if (len != json_object_array_length(jso2))
  977. return 0;
  978. for (i = 0; i < len; i++) {
  979. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  980. json_object_array_get_idx(jso2, i)))
  981. return 0;
  982. }
  983. return 1;
  984. }
  985. static int json_object_all_values_equal(struct json_object* jso1,
  986. struct json_object* jso2)
  987. {
  988. struct json_object_iter iter;
  989. struct json_object *sub;
  990. assert(json_object_get_type(jso1) == json_type_object);
  991. assert(json_object_get_type(jso2) == json_type_object);
  992. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  993. json_object_object_foreachC(jso1, iter) {
  994. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  995. (void**)&sub))
  996. return 0;
  997. if (!json_object_equal(iter.val, sub))
  998. return 0;
  999. }
  1000. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1001. json_object_object_foreachC(jso2, iter) {
  1002. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1003. (void**)&sub))
  1004. return 0;
  1005. }
  1006. return 1;
  1007. }
  1008. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1009. {
  1010. if (jso1 == jso2)
  1011. return 1;
  1012. if (!jso1 || !jso2)
  1013. return 0;
  1014. if (jso1->o_type != jso2->o_type)
  1015. return 0;
  1016. switch(jso1->o_type) {
  1017. case json_type_boolean:
  1018. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1019. case json_type_double:
  1020. return (jso1->o.c_double == jso2->o.c_double);
  1021. case json_type_int:
  1022. return (jso1->o.c_int64 == jso2->o.c_int64);
  1023. case json_type_string:
  1024. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1025. memcmp(get_string_component(jso1),
  1026. get_string_component(jso2),
  1027. jso1->o.c_string.len) == 0);
  1028. case json_type_object:
  1029. return json_object_all_values_equal(jso1, jso2);
  1030. case json_type_array:
  1031. return json_array_equal(jso1, jso2);
  1032. case json_type_null:
  1033. return 1;
  1034. };
  1035. return 0;
  1036. }