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

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