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

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