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

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