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.h 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 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. #ifndef _json_object_h_
  13. #define _json_object_h_
  14. #include "json_inttypes.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define JSON_OBJECT_DEF_HASH_ENTRIES 16
  19. /**
  20. * A flag for the json_object_to_json_string_ext() and
  21. * json_object_to_file_ext() functions which causes the output
  22. * to have no extra whitespace or formatting applied.
  23. */
  24. #define JSON_C_TO_STRING_PLAIN 0
  25. /**
  26. * A flag for the json_object_to_json_string_ext() and
  27. * json_object_to_file_ext() functions which causes the output to have
  28. * minimal whitespace inserted to make things slightly more readable.
  29. */
  30. #define JSON_C_TO_STRING_SPACED (1<<0)
  31. /**
  32. * A flag for the json_object_to_json_string_ext() and
  33. * json_object_to_file_ext() functions which causes
  34. * the output to be formatted.
  35. *
  36. * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/
  37. * for an example of the format.
  38. */
  39. #define JSON_C_TO_STRING_PRETTY (1<<1)
  40. /**
  41. * A flag to drop trailing zero for float values
  42. */
  43. #define JSON_C_TO_STRING_NOZERO (1<<2)
  44. #undef FALSE
  45. #define FALSE ((json_bool)0)
  46. #undef TRUE
  47. #define TRUE ((json_bool)1)
  48. extern const char *json_number_chars;
  49. extern const char *json_hex_chars;
  50. /* CAW: added for ANSI C iteration correctness */
  51. struct json_object_iter
  52. {
  53. char *key;
  54. struct json_object *val;
  55. struct lh_entry *entry;
  56. };
  57. /* forward structure definitions */
  58. typedef int json_bool;
  59. typedef struct printbuf printbuf;
  60. typedef struct lh_table lh_table;
  61. typedef struct array_list array_list;
  62. typedef struct json_object json_object;
  63. typedef struct json_object_iter json_object_iter;
  64. typedef struct json_tokener json_tokener;
  65. /**
  66. * Type of custom user delete functions. See json_object_set_serializer.
  67. */
  68. typedef void (json_object_delete_fn)(struct json_object *jso, void *userdata);
  69. /**
  70. * Type of a custom serialization function. See json_object_set_serializer.
  71. */
  72. typedef int (json_object_to_json_string_fn)(struct json_object *jso,
  73. struct printbuf *pb,
  74. int level,
  75. int flags);
  76. /* supported object types */
  77. typedef enum json_type {
  78. /* If you change this, be sure to update json_type_to_name() too */
  79. json_type_null,
  80. json_type_boolean,
  81. json_type_double,
  82. json_type_int,
  83. json_type_object,
  84. json_type_array,
  85. json_type_string,
  86. } json_type;
  87. /* reference counting functions */
  88. /**
  89. * Increment the reference count of json_object, thereby grabbing shared
  90. * ownership of obj.
  91. *
  92. * @param obj the json_object instance
  93. */
  94. extern struct json_object* json_object_get(struct json_object *obj);
  95. /**
  96. * Decrement the reference count of json_object and free if it reaches zero.
  97. * You must have ownership of obj prior to doing this or you will cause an
  98. * imbalance in the reference count.
  99. *
  100. * @param obj the json_object instance
  101. * @returns 1 if the object was freed.
  102. */
  103. int json_object_put(struct json_object *obj);
  104. /**
  105. * Check if the json_object is of a given type
  106. * @param obj the json_object instance
  107. * @param type one of:
  108. json_type_null (i.e. obj == NULL),
  109. json_type_boolean,
  110. json_type_double,
  111. json_type_int,
  112. json_type_object,
  113. json_type_array,
  114. json_type_string,
  115. */
  116. extern int json_object_is_type(struct json_object *obj, enum json_type type);
  117. /**
  118. * Get the type of the json_object. See also json_type_to_name() to turn this
  119. * into a string suitable, for instance, for logging.
  120. *
  121. * @param obj the json_object instance
  122. * @returns type being one of:
  123. json_type_null (i.e. obj == NULL),
  124. json_type_boolean,
  125. json_type_double,
  126. json_type_int,
  127. json_type_object,
  128. json_type_array,
  129. json_type_string,
  130. */
  131. extern enum json_type json_object_get_type(struct json_object *obj);
  132. /** Stringify object to json format.
  133. * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
  134. * @param obj the json_object instance
  135. * @returns a string in JSON format
  136. */
  137. extern const char* json_object_to_json_string(struct json_object *obj);
  138. /** Stringify object to json format
  139. * @param obj the json_object instance
  140. * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
  141. * @returns a string in JSON format
  142. */
  143. extern const char* json_object_to_json_string_ext(struct json_object *obj, int
  144. flags);
  145. /**
  146. * Set a custom serialization function to be used when this particular object
  147. * is converted to a string by json_object_to_json_string.
  148. *
  149. * If a custom serializer is already set on this object, any existing
  150. * user_delete function is called before the new one is set.
  151. *
  152. * If to_string_func is NULL, the other parameters are ignored
  153. * and the default behaviour is reset.
  154. *
  155. * The userdata parameter is optional and may be passed as NULL. If provided,
  156. * it is passed to to_string_func as-is. This parameter may be NULL even
  157. * if user_delete is non-NULL.
  158. *
  159. * The user_delete parameter is optional and may be passed as NULL, even if
  160. * the userdata parameter is non-NULL. It will be called just before the
  161. * json_object is deleted, after it's reference count goes to zero
  162. * (see json_object_put()).
  163. * If this is not provided, it is up to the caller to free the userdata at
  164. * an appropriate time. (i.e. after the json_object is deleted)
  165. *
  166. * @param jso the object to customize
  167. * @param to_string_func the custom serialization function
  168. * @param userdata an optional opaque cookie
  169. * @param user_delete an optional function from freeing userdata
  170. */
  171. void json_object_set_serializer(json_object *jso,
  172. json_object_to_json_string_fn to_string_func,
  173. void *userdata,
  174. json_object_delete_fn *user_delete);
  175. /* object type methods */
  176. /** Create a new empty object with a reference count of 1. The caller of
  177. * this object initially has sole ownership. Remember, when using
  178. * json_object_object_add or json_object_array_put_idx, ownership will
  179. * transfer to the object/array. Call json_object_get if you want to maintain
  180. * shared ownership or also add this object as a child of multiple objects or
  181. * arrays. Any ownerships you acquired but did not transfer must be released
  182. * through json_object_put.
  183. *
  184. * @returns a json_object of type json_type_object
  185. */
  186. extern struct json_object* json_object_new_object(void);
  187. /** Get the hashtable of a json_object of type json_type_object
  188. * @param obj the json_object instance
  189. * @returns a linkhash
  190. */
  191. extern struct lh_table* json_object_get_object(struct json_object *obj);
  192. /** Get the size of an object in terms of the number of fields it has.
  193. * @param obj the json_object whose length to return
  194. */
  195. extern int json_object_object_length(struct json_object* obj);
  196. /** Add an object field to a json_object of type json_type_object
  197. *
  198. * The reference count will *not* be incremented. This is to make adding
  199. * fields to objects in code more compact. If you want to retain a reference
  200. * to an added object, independent of the lifetime of obj, you must wrap the
  201. * passed object with json_object_get.
  202. *
  203. * Upon calling this, the ownership of val transfers to obj. Thus you must
  204. * make sure that you do in fact have ownership over this object. For instance,
  205. * json_object_new_object will give you ownership until you transfer it,
  206. * whereas json_object_object_get does not.
  207. *
  208. * @param obj the json_object instance
  209. * @param key the object field name (a private copy will be duplicated)
  210. * @param val a json_object or NULL member to associate with the given field
  211. */
  212. extern void json_object_object_add(struct json_object* obj, const char *key,
  213. struct json_object *val);
  214. /** Get the json_object associate with a given object field
  215. *
  216. * *No* reference counts will be changed. There is no need to manually adjust
  217. * reference counts through the json_object_put/json_object_get methods unless
  218. * you need to have the child (value) reference maintain a different lifetime
  219. * than the owning parent (obj). Ownership of the returned value is retained
  220. * by obj (do not do json_object_put unless you have done a json_object_get).
  221. * If you delete the value from obj (json_object_object_del) and wish to access
  222. * the returned reference afterwards, make sure you have first gotten shared
  223. * ownership through json_object_get (& don't forget to do a json_object_put
  224. * or transfer ownership to prevent a memory leak).
  225. *
  226. * @param obj the json_object instance
  227. * @param key the object field name
  228. * @returns the json_object associated with the given field name
  229. * @deprecated Please use json_object_object_get_ex
  230. */
  231. extern struct json_object* json_object_object_get(struct json_object* obj,
  232. const char *key);
  233. /** Get the json_object associated with a given object field.
  234. *
  235. * This returns true if the key is found, false in all other cases (including
  236. * if obj isn't a json_type_object).
  237. *
  238. * *No* reference counts will be changed. There is no need to manually adjust
  239. * reference counts through the json_object_put/json_object_get methods unless
  240. * you need to have the child (value) reference maintain a different lifetime
  241. * than the owning parent (obj). Ownership of value is retained by obj.
  242. *
  243. * @param obj the json_object instance
  244. * @param key the object field name
  245. * @param value a pointer where to store a reference to the json_object
  246. * associated with the given field name.
  247. *
  248. * It is safe to pass a NULL value.
  249. * @returns whether or not the key exists
  250. */
  251. extern json_bool json_object_object_get_ex(struct json_object* obj,
  252. const char *key,
  253. struct json_object **value);
  254. /** Delete the given json_object field
  255. *
  256. * The reference count will be decremented for the deleted object. If there
  257. * are no more owners of the value represented by this key, then the value is
  258. * freed. Otherwise, the reference to the value will remain in memory.
  259. *
  260. * @param obj the json_object instance
  261. * @param key the object field name
  262. */
  263. extern void json_object_object_del(struct json_object* obj, const char *key);
  264. /**
  265. * Iterate through all keys and values of an object.
  266. *
  267. * Adding keys to the object while iterating is NOT allowed.
  268. *
  269. * Deleting an existing key, or replacing an existing key with a
  270. * new value IS allowed.
  271. *
  272. * @param obj the json_object instance
  273. * @param key the local name for the char* key variable defined in the body
  274. * @param val the local name for the json_object* object variable defined in
  275. * the body
  276. */
  277. #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L
  278. # define json_object_object_foreach(obj,key,val) \
  279. char *key; \
  280. struct json_object *val __attribute__((__unused__)); \
  281. for(struct lh_entry *entry ## key = json_object_get_object(obj)->head, *entry_next ## key = NULL; \
  282. ({ if(entry ## key) { \
  283. key = (char*)entry ## key->k; \
  284. val = (struct json_object*)entry ## key->v; \
  285. entry_next ## key = entry ## key->next; \
  286. } ; entry ## key; }); \
  287. entry ## key = entry_next ## key )
  288. #else /* ANSI C or MSC */
  289. # define json_object_object_foreach(obj,key,val) \
  290. char *key;\
  291. struct json_object *val; \
  292. struct lh_entry *entry ## key; \
  293. struct lh_entry *entry_next ## key = NULL; \
  294. for(entry ## key = json_object_get_object(obj)->head; \
  295. (entry ## key ? ( \
  296. key = (char*)entry ## key->k, \
  297. val = (struct json_object*)entry ## key->v, \
  298. entry_next ## key = entry ## key->next, \
  299. entry ## key) : 0); \
  300. entry ## key = entry_next ## key)
  301. #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L */
  302. /** Iterate through all keys and values of an object (ANSI C Safe)
  303. * @param obj the json_object instance
  304. * @param iter the object iterator
  305. */
  306. #define json_object_object_foreachC(obj,iter) \
  307. for(iter.entry = json_object_get_object(obj)->head; (iter.entry ? (iter.key = (char*)iter.entry->k, iter.val = (struct json_object*)iter.entry->v, iter.entry) : 0); iter.entry = iter.entry->next)
  308. /* Array type methods */
  309. /** Create a new empty json_object of type json_type_array
  310. * @returns a json_object of type json_type_array
  311. */
  312. extern struct json_object* json_object_new_array(void);
  313. /** Get the arraylist of a json_object of type json_type_array
  314. * @param obj the json_object instance
  315. * @returns an arraylist
  316. */
  317. extern struct array_list* json_object_get_array(struct json_object *obj);
  318. /** Get the length of a json_object of type json_type_array
  319. * @param obj the json_object instance
  320. * @returns an int
  321. */
  322. extern int json_object_array_length(struct json_object *obj);
  323. /** Sorts the elements of jso of type json_type_array
  324. *
  325. * Pointers to the json_object pointers will be passed as the two arguments
  326. * to @sort_fn
  327. *
  328. * @param obj the json_object instance
  329. * @param sort_fn a sorting function
  330. */
  331. extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
  332. /** Add an element to the end of a json_object of type json_type_array
  333. *
  334. * The reference count will *not* be incremented. This is to make adding
  335. * fields to objects in code more compact. If you want to retain a reference
  336. * to an added object you must wrap the passed object with json_object_get
  337. *
  338. * @param obj the json_object instance
  339. * @param val the json_object to be added
  340. */
  341. extern int json_object_array_add(struct json_object *obj,
  342. struct json_object *val);
  343. /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
  344. *
  345. * The reference count will *not* be incremented. This is to make adding
  346. * fields to objects in code more compact. If you want to retain a reference
  347. * to an added object you must wrap the passed object with json_object_get
  348. *
  349. * The reference count of a replaced object will be decremented.
  350. *
  351. * The array size will be automatically be expanded to the size of the
  352. * index if the index is larger than the current size.
  353. *
  354. * @param obj the json_object instance
  355. * @param idx the index to insert the element at
  356. * @param val the json_object to be added
  357. */
  358. extern int json_object_array_put_idx(struct json_object *obj, int idx,
  359. struct json_object *val);
  360. /** Get the element at specificed index of the array (a json_object of type json_type_array)
  361. * @param obj the json_object instance
  362. * @param idx the index to get the element at
  363. * @returns the json_object at the specified index (or NULL)
  364. */
  365. extern struct json_object* json_object_array_get_idx(struct json_object *obj,
  366. int idx);
  367. /* json_bool type methods */
  368. /** Create a new empty json_object of type json_type_boolean
  369. * @param b a json_bool TRUE or FALSE (0 or 1)
  370. * @returns a json_object of type json_type_boolean
  371. */
  372. extern struct json_object* json_object_new_boolean(json_bool b);
  373. /** Get the json_bool value of a json_object
  374. *
  375. * The type is coerced to a json_bool if the passed object is not a json_bool.
  376. * integer and double objects will return FALSE if there value is zero
  377. * or TRUE otherwise. If the passed object is a string it will return
  378. * TRUE if it has a non zero length. If any other object type is passed
  379. * TRUE will be returned if the object is not NULL.
  380. *
  381. * @param obj the json_object instance
  382. * @returns a json_bool
  383. */
  384. extern json_bool json_object_get_boolean(struct json_object *obj);
  385. /* int type methods */
  386. /** Create a new empty json_object of type json_type_int
  387. * Note that values are stored as 64-bit values internally.
  388. * To ensure the full range is maintained, use json_object_new_int64 instead.
  389. * @param i the integer
  390. * @returns a json_object of type json_type_int
  391. */
  392. extern struct json_object* json_object_new_int(int32_t i);
  393. /** Create a new empty json_object of type json_type_int
  394. * @param i the integer
  395. * @returns a json_object of type json_type_int
  396. */
  397. extern struct json_object* json_object_new_int64(int64_t i);
  398. /** Get the int value of a json_object
  399. *
  400. * The type is coerced to a int if the passed object is not a int.
  401. * double objects will return their integer conversion. Strings will be
  402. * parsed as an integer. If no conversion exists then 0 is returned
  403. * and errno is set to EINVAL. null is equivalent to 0 (no error values set)
  404. *
  405. * Note that integers are stored internally as 64-bit values.
  406. * If the value of too big or too small to fit into 32-bit, INT32_MAX or
  407. * INT32_MIN are returned, respectively.
  408. *
  409. * @param obj the json_object instance
  410. * @returns an int
  411. */
  412. extern int32_t json_object_get_int(struct json_object *obj);
  413. /** Get the int value of a json_object
  414. *
  415. * The type is coerced to a int64 if the passed object is not a int64.
  416. * double objects will return their int64 conversion. Strings will be
  417. * parsed as an int64. If no conversion exists then 0 is returned.
  418. *
  419. * NOTE: Set errno to 0 directly before a call to this function to determine
  420. * whether or not conversion was successful (it does not clear the value for
  421. * you).
  422. *
  423. * @param obj the json_object instance
  424. * @returns an int64
  425. */
  426. extern int64_t json_object_get_int64(struct json_object *obj);
  427. /* double type methods */
  428. /** Create a new empty json_object of type json_type_double
  429. * @param d the double
  430. * @returns a json_object of type json_type_double
  431. */
  432. extern struct json_object* json_object_new_double(double d);
  433. /** Get the double floating point value of a json_object
  434. *
  435. * The type is coerced to a double if the passed object is not a double.
  436. * integer objects will return their double conversion. Strings will be
  437. * parsed as a double. If no conversion exists then 0.0 is returned and
  438. * errno is set to EINVAL. null is equivalent to 0 (no error values set)
  439. *
  440. * If the value is too big to fit in a double, then the value is set to
  441. * the closest infinity with errno set to ERANGE. If strings cannot be
  442. * converted to their double value, then EINVAL is set & NaN is returned.
  443. *
  444. * Arrays of length 0 are interpreted as 0 (with no error flags set).
  445. * Arrays of length 1 are effectively cast to the equivalent object and
  446. * converted using the above rules. All other arrays set the error to
  447. * EINVAL & return NaN.
  448. *
  449. * NOTE: Set errno to 0 directly before a call to this function to
  450. * determine whether or not conversion was successful (it does not clear
  451. * the value for you).
  452. *
  453. * @param obj the json_object instance
  454. * @returns a double floating point number
  455. */
  456. extern double json_object_get_double(struct json_object *obj);
  457. /* string type methods */
  458. /** Create a new empty json_object of type json_type_string
  459. *
  460. * A copy of the string is made and the memory is managed by the json_object
  461. *
  462. * @param s the string
  463. * @returns a json_object of type json_type_string
  464. */
  465. extern struct json_object* json_object_new_string(const char *s);
  466. extern struct json_object* json_object_new_string_len(const char *s, int len);
  467. /** Get the string value of a json_object
  468. *
  469. * If the passed object is not of type json_type_string then the JSON
  470. * representation of the object is returned.
  471. *
  472. * The returned string memory is managed by the json_object and will
  473. * be freed when the reference count of the json_object drops to zero.
  474. *
  475. * @param obj the json_object instance
  476. * @returns a string
  477. */
  478. extern const char* json_object_get_string(struct json_object *obj);
  479. /** Get the string length of a json_object
  480. *
  481. * If the passed object is not of type json_type_string then zero
  482. * will be returned.
  483. *
  484. * @param obj the json_object instance
  485. * @returns int
  486. */
  487. extern int json_object_get_string_len(struct json_object *obj);
  488. #ifdef __cplusplus
  489. }
  490. #endif
  491. #endif