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

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