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

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