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