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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. #ifdef __GNUC__
  15. #define THIS_FUNCTION_IS_DEPRECATED(func) func __attribute__ ((deprecated))
  16. #elif defined(_MSC_VER)
  17. #define THIS_FUNCTION_IS_DEPRECATED(func) __declspec(deprecated) func
  18. #else
  19. #define THIS_FUNCTION_IS_DEPRECATED(func) func
  20. #endif
  21. #include <stddef.h>
  22. #include "json_inttypes.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #define JSON_OBJECT_DEF_HASH_ENTRIES 16
  27. /**
  28. * A flag for the json_object_to_json_string_ext() and
  29. * json_object_to_file_ext() functions which causes the output
  30. * to have no extra whitespace or formatting applied.
  31. */
  32. #define JSON_C_TO_STRING_PLAIN 0
  33. /**
  34. * A flag for the json_object_to_json_string_ext() and
  35. * json_object_to_file_ext() functions which causes the output to have
  36. * minimal whitespace inserted to make things slightly more readable.
  37. */
  38. #define JSON_C_TO_STRING_SPACED (1<<0)
  39. /**
  40. * A flag for the json_object_to_json_string_ext() and
  41. * json_object_to_file_ext() functions which causes
  42. * the output to be formatted.
  43. *
  44. * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/
  45. * for an example of the format.
  46. */
  47. #define JSON_C_TO_STRING_PRETTY (1<<1)
  48. /**
  49. * A flag for the json_object_to_json_string_ext() and
  50. * json_object_to_file_ext() functions which causes
  51. * the output to be formatted.
  52. *
  53. * Instead of a "Two Space Tab" this gives a single tab character.
  54. */
  55. #define JSON_C_TO_STRING_PRETTY_TAB (1<<3)
  56. /**
  57. * A flag to drop trailing zero for float values
  58. */
  59. #define JSON_C_TO_STRING_NOZERO (1<<2)
  60. /**
  61. * Don't escape forward slashes.
  62. */
  63. #define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
  64. /**
  65. * A flag for the json_object_object_add_ex function which
  66. * causes the value to be added without a check if it already exists.
  67. * Note: it is the responsibilty of the caller to ensure that no
  68. * key is added multiple times. If this is done, results are
  69. * unpredictable. While this option is somewhat dangerous, it
  70. * permits potentially large performance savings in code that
  71. * knows for sure the key values are unique (e.g. because the
  72. * code adds a well-known set of constant key values).
  73. */
  74. #define JSON_C_OBJECT_ADD_KEY_IS_NEW (1<<1)
  75. /**
  76. * A flag for the json_object_object_add_ex function which
  77. * flags the key as being constant memory. This means that
  78. * the key will NOT be copied via strdup(), resulting in a
  79. * potentially huge performance win (malloc, strdup and
  80. * free are usually performance hogs). It is acceptable to
  81. * use this flag for keys in non-constant memory blocks if
  82. * the caller ensure that the memory holding the key lives
  83. * longer than the corresponding json object. However, this
  84. * is somewhat dangerous and should only be done if really
  85. * justified.
  86. * The general use-case for this flag is cases where the
  87. * key is given as a real constant value in the function
  88. * call, e.g. as in
  89. * json_object_object_add_ex(obj, "ip", json,
  90. * JSON_C_OBJECT_KEY_IS_CONSTANT);
  91. */
  92. #define JSON_C_OBJECT_KEY_IS_CONSTANT (1<<2)
  93. #undef FALSE
  94. #define FALSE ((json_bool)0)
  95. #undef TRUE
  96. #define TRUE ((json_bool)1)
  97. extern const char *json_number_chars;
  98. extern const char *json_hex_chars;
  99. /* CAW: added for ANSI C iteration correctness */
  100. struct json_object_iter
  101. {
  102. char *key;
  103. struct json_object *val;
  104. struct lh_entry *entry;
  105. };
  106. /* forward structure definitions */
  107. typedef int json_bool;
  108. typedef struct printbuf printbuf;
  109. typedef struct lh_table lh_table;
  110. typedef struct array_list array_list;
  111. typedef struct json_object json_object;
  112. typedef struct json_object_iter json_object_iter;
  113. typedef struct json_tokener json_tokener;
  114. /**
  115. * Type of custom user delete functions. See json_object_set_serializer.
  116. */
  117. typedef void (json_object_delete_fn)(struct json_object *jso, void *userdata);
  118. /**
  119. * Type of a custom serialization function. See json_object_set_serializer.
  120. */
  121. typedef int (json_object_to_json_string_fn)(struct json_object *jso,
  122. struct printbuf *pb,
  123. int level,
  124. int flags);
  125. /* supported object types */
  126. typedef enum json_type {
  127. /* If you change this, be sure to update json_type_to_name() too */
  128. json_type_null,
  129. json_type_boolean,
  130. json_type_double,
  131. json_type_int,
  132. json_type_object,
  133. json_type_array,
  134. json_type_string
  135. } json_type;
  136. /* reference counting functions */
  137. /**
  138. * Increment the reference count of json_object, thereby grabbing shared
  139. * ownership of obj.
  140. *
  141. * @param obj the json_object instance
  142. */
  143. extern struct json_object* json_object_get(struct json_object *obj);
  144. /**
  145. * Decrement the reference count of json_object and free if it reaches zero.
  146. * You must have ownership of obj prior to doing this or you will cause an
  147. * imbalance in the reference count.
  148. *
  149. * @param obj the json_object instance
  150. * @returns 1 if the object was freed.
  151. */
  152. int json_object_put(struct json_object *obj);
  153. /**
  154. * Check if the json_object is of a given type
  155. * @param obj the json_object instance
  156. * @param type one of:
  157. json_type_null (i.e. obj == NULL),
  158. json_type_boolean,
  159. json_type_double,
  160. json_type_int,
  161. json_type_object,
  162. json_type_array,
  163. json_type_string
  164. */
  165. extern int json_object_is_type(const struct json_object *obj, enum json_type type);
  166. /**
  167. * Get the type of the json_object. See also json_type_to_name() to turn this
  168. * into a string suitable, for instance, for logging.
  169. *
  170. * @param obj the json_object instance
  171. * @returns type being one of:
  172. json_type_null (i.e. obj == NULL),
  173. json_type_boolean,
  174. json_type_double,
  175. json_type_int,
  176. json_type_object,
  177. json_type_array,
  178. json_type_string
  179. */
  180. extern enum json_type json_object_get_type(const struct json_object *obj);
  181. /** Stringify object to json format.
  182. * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
  183. * The pointer you get is an internal of your json object. You don't
  184. * have to free it, later use of json_object_put() should be sufficient.
  185. * If you can not ensure there's no concurrent access to *obj use
  186. * strdup().
  187. * @param obj the json_object instance
  188. * @returns a string in JSON format
  189. */
  190. extern const char* json_object_to_json_string(struct json_object *obj);
  191. /** Stringify object to json format
  192. * @see json_object_to_json_string() for details on how to free string.
  193. * @param obj the json_object instance
  194. * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
  195. * @returns a string in JSON format
  196. */
  197. extern const char* json_object_to_json_string_ext(struct json_object *obj, int
  198. flags);
  199. /**
  200. * Returns the userdata set by json_object_set_userdata() or
  201. * json_object_set_serializer()
  202. *
  203. * @param jso the object to return the userdata for
  204. */
  205. extern void* json_object_get_userdata(json_object *jso);
  206. /**
  207. * Set an opaque userdata value for an object
  208. *
  209. * The userdata can be retrieved using json_object_get_userdata().
  210. *
  211. * If custom userdata is already set on this object, any existing user_delete
  212. * function is called before the new one is set.
  213. *
  214. * The user_delete parameter is optional and may be passed as NULL, even if
  215. * the userdata parameter is non-NULL. It will be called just before the
  216. * json_object is deleted, after it's reference count goes to zero
  217. * (see json_object_put()).
  218. * If this is not provided, it is up to the caller to free the userdata at
  219. * an appropriate time. (i.e. after the json_object is deleted)
  220. *
  221. * Note: Objects created by parsing strings may have custom serializers set
  222. * which expect the userdata to contain specific data (due to use of
  223. * json_object_new_double_s()). In this case, json_object_set_serialiser() with
  224. * NULL as to_string_func should be used instead to set the userdata and reset
  225. * the serializer to its default value.
  226. *
  227. * @param jso the object to set the userdata for
  228. * @param userdata an optional opaque cookie
  229. * @param user_delete an optional function from freeing userdata
  230. */
  231. extern void json_object_set_userdata(json_object *jso, void *userdata,
  232. json_object_delete_fn *user_delete);
  233. /**
  234. * Set a custom serialization function to be used when this particular object
  235. * is converted to a string by json_object_to_json_string.
  236. *
  237. * If custom userdata is already set on this object, any existing user_delete
  238. * function is called before the new one is set.
  239. *
  240. * If to_string_func is NULL the default behaviour is reset (but the userdata
  241. * and user_delete fields are still set).
  242. *
  243. * The userdata parameter is optional and may be passed as NULL. It can be used
  244. * to provide additional data for to_string_func to use. This parameter may
  245. * be NULL even if user_delete is non-NULL.
  246. *
  247. * The user_delete parameter is optional and may be passed as NULL, even if
  248. * the userdata parameter is non-NULL. It will be called just before the
  249. * json_object is deleted, after it's reference count goes to zero
  250. * (see json_object_put()).
  251. * If this is not provided, it is up to the caller to free the userdata at
  252. * an appropriate time. (i.e. after the json_object is deleted)
  253. *
  254. * Note that the userdata is the same as set by json_object_set_userdata(), so
  255. * care must be taken not to overwrite the value when both a custom serializer
  256. * and json_object_set_userdata() are used.
  257. *
  258. * @param jso the object to customize
  259. * @param to_string_func the custom serialization function
  260. * @param userdata an optional opaque cookie
  261. * @param user_delete an optional function from freeing userdata
  262. */
  263. extern void json_object_set_serializer(json_object *jso,
  264. json_object_to_json_string_fn to_string_func,
  265. void *userdata,
  266. json_object_delete_fn *user_delete);
  267. /**
  268. * Simply call free on the userdata pointer.
  269. * Can be used with json_object_set_serializer().
  270. *
  271. * @param jso unused
  272. * @param userdata the pointer that is passed to free().
  273. */
  274. json_object_delete_fn json_object_free_userdata;
  275. /**
  276. * Copy the jso->_userdata string over to pb as-is.
  277. * Can be used with json_object_set_serializer().
  278. *
  279. * @param jso The object whose _userdata is used.
  280. * @param pb The destination buffer.
  281. * @param level Ignored.
  282. * @param flags Ignored.
  283. */
  284. json_object_to_json_string_fn json_object_userdata_to_json_string;
  285. /* object type methods */
  286. /** Create a new empty object with a reference count of 1. The caller of
  287. * this object initially has sole ownership. Remember, when using
  288. * json_object_object_add or json_object_array_put_idx, ownership will
  289. * transfer to the object/array. Call json_object_get if you want to maintain
  290. * shared ownership or also add this object as a child of multiple objects or
  291. * arrays. Any ownerships you acquired but did not transfer must be released
  292. * through json_object_put.
  293. *
  294. * @returns a json_object of type json_type_object
  295. */
  296. extern struct json_object* json_object_new_object(void);
  297. /** Get the hashtable of a json_object of type json_type_object
  298. * @param obj the json_object instance
  299. * @returns a linkhash
  300. */
  301. extern struct lh_table* json_object_get_object(const struct json_object *obj);
  302. /** Get the size of an object in terms of the number of fields it has.
  303. * @param obj the json_object whose length to return
  304. */
  305. extern int json_object_object_length(const struct json_object* obj);
  306. /** Add an object field to a json_object of type json_type_object
  307. *
  308. * The reference count will *not* be incremented. This is to make adding
  309. * fields to objects in code more compact. If you want to retain a reference
  310. * to an added object, independent of the lifetime of obj, you must wrap the
  311. * passed object with json_object_get.
  312. *
  313. * Upon calling this, the ownership of val transfers to obj. Thus you must
  314. * make sure that you do in fact have ownership over this object. For instance,
  315. * json_object_new_object will give you ownership until you transfer it,
  316. * whereas json_object_object_get does not.
  317. *
  318. * @param obj the json_object instance
  319. * @param key the object field name (a private copy will be duplicated)
  320. * @param val a json_object or NULL member to associate with the given field
  321. *
  322. * @return On success, <code>0</code> is returned.
  323. * On error, a negative value is returned.
  324. */
  325. extern int json_object_object_add(struct json_object* obj, const char *key,
  326. struct json_object *val);
  327. /** Add an object field to a json_object of type json_type_object
  328. *
  329. * The semantics are identical to json_object_object_add, except that an
  330. * additional flag fields gives you more control over some detail aspects
  331. * of processing. See the description of JSON_C_OBJECT_ADD_* flags for more
  332. * details.
  333. *
  334. * @param obj the json_object instance
  335. * @param key the object field name (a private copy will be duplicated)
  336. * @param val a json_object or NULL member to associate with the given field
  337. * @param opts process-modifying options. To specify multiple options, use
  338. * arithmetic or (OPT1|OPT2)
  339. */
  340. extern void json_object_object_add_ex(struct json_object* obj, const char *key,
  341. struct json_object *val, const unsigned opts);
  342. /** Get the json_object associate with a given object field.
  343. *
  344. * This returns NULL if the field is found but its value is null, or if
  345. * the field is not found, or if obj is not a json_type_object. If you
  346. * need to distinguis between these cases, use json_object_object_get_ex().
  347. *
  348. * *No* reference counts will be changed. There is no need to manually adjust
  349. * reference counts through the json_object_put/json_object_get methods unless
  350. * you need to have the child (value) reference maintain a different lifetime
  351. * than the owning parent (obj). Ownership of the returned value is retained
  352. * by obj (do not do json_object_put unless you have done a json_object_get).
  353. * If you delete the value from obj (json_object_object_del) and wish to access
  354. * the returned reference afterwards, make sure you have first gotten shared
  355. * ownership through json_object_get (& don't forget to do a json_object_put
  356. * or transfer ownership to prevent a memory leak).
  357. *
  358. * @param obj the json_object instance
  359. * @param key the object field name
  360. * @returns the json_object associated with the given field name
  361. * @deprecated Please use json_object_object_get_ex
  362. */
  363. extern struct json_object* json_object_object_get(const struct json_object* obj,
  364. const char *key);
  365. /** Get the json_object associated with a given object field.
  366. *
  367. * This returns true if the key is found, false in all other cases (including
  368. * if obj isn't a json_type_object).
  369. *
  370. * *No* reference counts will be changed. There is no need to manually adjust
  371. * reference counts through the json_object_put/json_object_get methods unless
  372. * you need to have the child (value) reference maintain a different lifetime
  373. * than the owning parent (obj). Ownership of value is retained by obj.
  374. *
  375. * @param obj the json_object instance
  376. * @param key the object field name
  377. * @param value a pointer where to store a reference to the json_object
  378. * associated with the given field name.
  379. *
  380. * It is safe to pass a NULL value.
  381. * @returns whether or not the key exists
  382. */
  383. extern json_bool json_object_object_get_ex(const struct json_object* obj,
  384. const char *key,
  385. struct json_object **value);
  386. /** Delete the given json_object field
  387. *
  388. * The reference count will be decremented for the deleted object. If there
  389. * are no more owners of the value represented by this key, then the value is
  390. * freed. Otherwise, the reference to the value will remain in memory.
  391. *
  392. * @param obj the json_object instance
  393. * @param key the object field name
  394. */
  395. extern void json_object_object_del(struct json_object* obj, const char *key);
  396. /**
  397. * Iterate through all keys and values of an object.
  398. *
  399. * Adding keys to the object while iterating is NOT allowed.
  400. *
  401. * Deleting an existing key, or replacing an existing key with a
  402. * new value IS allowed.
  403. *
  404. * @param obj the json_object instance
  405. * @param key the local name for the char* key variable defined in the body
  406. * @param val the local name for the json_object* object variable defined in
  407. * the body
  408. */
  409. #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L
  410. # define json_object_object_foreach(obj,key,val) \
  411. char *key = NULL; \
  412. struct json_object *val __attribute__((__unused__)) = NULL; \
  413. for(struct lh_entry *entry ## key = json_object_get_object(obj)->head, *entry_next ## key = NULL; \
  414. ({ if(entry ## key) { \
  415. key = (char*)lh_entry_k(entry ## key); \
  416. val = (struct json_object*)lh_entry_v(entry ## key); \
  417. entry_next ## key = entry ## key->next; \
  418. } ; entry ## key; }); \
  419. entry ## key = entry_next ## key )
  420. #else /* ANSI C or MSC */
  421. # define json_object_object_foreach(obj,key,val) \
  422. char *key;\
  423. struct json_object *val; \
  424. struct lh_entry *entry ## key; \
  425. struct lh_entry *entry_next ## key = NULL; \
  426. for(entry ## key = json_object_get_object(obj)->head; \
  427. (entry ## key ? ( \
  428. key = (char*)lh_entry_k(entry ## key), \
  429. val = (struct json_object*)lh_entry_v(entry ## key), \
  430. entry_next ## key = entry ## key->next, \
  431. entry ## key) : 0); \
  432. entry ## key = entry_next ## key)
  433. #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L */
  434. /** Iterate through all keys and values of an object (ANSI C Safe)
  435. * @param obj the json_object instance
  436. * @param iter the object iterator
  437. */
  438. #define json_object_object_foreachC(obj,iter) \
  439. for(iter.entry = json_object_get_object(obj)->head; \
  440. (iter.entry ? (iter.key = (char*)lh_entry_k(iter.entry), iter.val = (struct json_object*)lh_entry_v(iter.entry), iter.entry) : 0); \
  441. iter.entry = iter.entry->next)
  442. /* Array type methods */
  443. /** Create a new empty json_object of type json_type_array
  444. * @returns a json_object of type json_type_array
  445. */
  446. extern struct json_object* json_object_new_array(void);
  447. /** Get the arraylist of a json_object of type json_type_array
  448. * @param obj the json_object instance
  449. * @returns an arraylist
  450. */
  451. extern struct array_list* json_object_get_array(const struct json_object *obj);
  452. /** Get the length of a json_object of type json_type_array
  453. * @param obj the json_object instance
  454. * @returns an int
  455. */
  456. extern size_t json_object_array_length(const struct json_object *obj);
  457. /** Sorts the elements of jso of type json_type_array
  458. *
  459. * Pointers to the json_object pointers will be passed as the two arguments
  460. * to @sort_fn
  461. *
  462. * @param obj the json_object instance
  463. * @param sort_fn a sorting function
  464. */
  465. extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
  466. /** Binary search a sorted array for a specified key object.
  467. *
  468. * It depends on your compare function what's sufficient as a key.
  469. * Usually you create some dummy object with the parameter compared in
  470. * it, to identify the right item you're actually looking for.
  471. *
  472. * @see json_object_array_sort() for hints on the compare function.
  473. *
  474. * @param key a dummy json_object with the right key
  475. * @param jso the array object we're searching
  476. * @param sort_fn the sort/compare function
  477. *
  478. * @return the wanted json_object instance
  479. */
  480. extern struct json_object* json_object_array_bsearch(
  481. const struct json_object *key,
  482. const struct json_object *jso,
  483. int (*sort_fn)(const void *, const void *));
  484. /** Add an element to the end of a json_object of type json_type_array
  485. *
  486. * The reference count will *not* be incremented. This is to make adding
  487. * fields to objects in code more compact. If you want to retain a reference
  488. * to an added object you must wrap the passed object with json_object_get
  489. *
  490. * @param obj the json_object instance
  491. * @param val the json_object to be added
  492. */
  493. extern int json_object_array_add(struct json_object *obj,
  494. struct json_object *val);
  495. /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
  496. *
  497. * The reference count will *not* be incremented. This is to make adding
  498. * fields to objects in code more compact. If you want to retain a reference
  499. * to an added object you must wrap the passed object with json_object_get
  500. *
  501. * The reference count of a replaced object will be decremented.
  502. *
  503. * The array size will be automatically be expanded to the size of the
  504. * index if the index is larger than the current size.
  505. *
  506. * @param obj the json_object instance
  507. * @param idx the index to insert the element at
  508. * @param val the json_object to be added
  509. */
  510. extern int json_object_array_put_idx(struct json_object *obj, size_t idx,
  511. struct json_object *val);
  512. /** Get the element at specificed index of the array (a json_object of type json_type_array)
  513. * @param obj the json_object instance
  514. * @param idx the index to get the element at
  515. * @returns the json_object at the specified index (or NULL)
  516. */
  517. extern struct json_object* json_object_array_get_idx(const struct json_object *obj,
  518. size_t idx);
  519. /** Delete an elements from a specified index in an array (a json_object of type json_type_array)
  520. *
  521. * The reference count will be decremented for each of the deleted objects. If there
  522. * are no more owners of an element that is being deleted, then the value is
  523. * freed. Otherwise, the reference to the value will remain in memory.
  524. *
  525. * @param obj the json_object instance
  526. * @param idx the index to start deleting elements at
  527. * @param count the number of elements to delete
  528. * @returns 0 if the elements were successfully deleted
  529. */
  530. extern int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count);
  531. /* json_bool type methods */
  532. /** Create a new empty json_object of type json_type_boolean
  533. * @param b a json_bool TRUE or FALSE (1 or 0)
  534. * @returns a json_object of type json_type_boolean
  535. */
  536. extern struct json_object* json_object_new_boolean(json_bool b);
  537. /** Get the json_bool value of a json_object
  538. *
  539. * The type is coerced to a json_bool if the passed object is not a json_bool.
  540. * integer and double objects will return FALSE if there value is zero
  541. * or TRUE otherwise. If the passed object is a string it will return
  542. * TRUE if it has a non zero length. If any other object type is passed
  543. * TRUE will be returned if the object is not NULL.
  544. *
  545. * @param obj the json_object instance
  546. * @returns a json_bool
  547. */
  548. extern json_bool json_object_get_boolean(const struct json_object *obj);
  549. /* int type methods */
  550. /** Create a new empty json_object of type json_type_int
  551. * Note that values are stored as 64-bit values internally.
  552. * To ensure the full range is maintained, use json_object_new_int64 instead.
  553. * @param i the integer
  554. * @returns a json_object of type json_type_int
  555. */
  556. extern struct json_object* json_object_new_int(int32_t i);
  557. /** Create a new empty json_object of type json_type_int
  558. * @param i the integer
  559. * @returns a json_object of type json_type_int
  560. */
  561. extern struct json_object* json_object_new_int64(int64_t i);
  562. /** Get the int value of a json_object
  563. *
  564. * The type is coerced to a int if the passed object is not a int.
  565. * double objects will return their integer conversion. Strings will be
  566. * parsed as an integer. If no conversion exists then 0 is returned
  567. * and errno is set to EINVAL. null is equivalent to 0 (no error values set)
  568. *
  569. * Note that integers are stored internally as 64-bit values.
  570. * If the value of too big or too small to fit into 32-bit, INT32_MAX or
  571. * INT32_MIN are returned, respectively.
  572. *
  573. * @param obj the json_object instance
  574. * @returns an int
  575. */
  576. extern int32_t json_object_get_int(const struct json_object *obj);
  577. /** Get the int value of a json_object
  578. *
  579. * The type is coerced to a int64 if the passed object is not a int64.
  580. * double objects will return their int64 conversion. Strings will be
  581. * parsed as an int64. If no conversion exists then 0 is returned.
  582. *
  583. * NOTE: Set errno to 0 directly before a call to this function to determine
  584. * whether or not conversion was successful (it does not clear the value for
  585. * you).
  586. *
  587. * @param obj the json_object instance
  588. * @returns an int64
  589. */
  590. extern int64_t json_object_get_int64(const struct json_object *obj);
  591. /* double type methods */
  592. /** Create a new empty json_object of type json_type_double
  593. *
  594. * @see json_object_double_to_json_string() for how to set a custom format string.
  595. *
  596. * @param d the double
  597. * @returns a json_object of type json_type_double
  598. */
  599. extern struct json_object* json_object_new_double(double d);
  600. /**
  601. * Create a new json_object of type json_type_double, using
  602. * the exact serialized representation of the value.
  603. *
  604. * This allows for numbers that would otherwise get displayed
  605. * inefficiently (e.g. 12.3 => "12.300000000000001") to be
  606. * serialized with the more convenient form.
  607. *
  608. * Notes:
  609. *
  610. * This is used by json_tokener_parse_ex() to allow for
  611. * an exact re-serialization of a parsed object.
  612. *
  613. * The userdata field is used to store the string representation, so it
  614. * can't be used for other data if this function is used.
  615. *
  616. * An equivalent sequence of calls is:
  617. * @code
  618. * jso = json_object_new_double(d);
  619. * json_object_set_serializer(jso, json_object_userdata_to_json_string,
  620. * strdup(ds), json_object_free_userdata);
  621. * @endcode
  622. *
  623. * @param d the numeric value of the double.
  624. * @param ds the string representation of the double. This will be copied.
  625. */
  626. extern struct json_object* json_object_new_double_s(double d, const char *ds);
  627. /** Serialize a json_object of type json_type_double to a string.
  628. *
  629. * This function isn't meant to be called directly. Instead, you can set a
  630. * custom format string for the serialization of this double using the
  631. * following call (where "%.17g" actually is the default):
  632. *
  633. * @code
  634. * jso = json_object_new_double(d);
  635. * json_object_set_serializer(jso, json_object_double_to_json_string,
  636. * "%.17g", NULL);
  637. * @endcode
  638. *
  639. * @see printf(3) man page for format strings
  640. *
  641. * @param jso The json_type_double object that is serialized.
  642. * @param pb The destination buffer.
  643. * @param level Ignored.
  644. * @param flags Ignored.
  645. */
  646. extern int json_object_double_to_json_string(struct json_object* jso,
  647. struct printbuf *pb,
  648. int level,
  649. int flags);
  650. /** Get the double floating point value of a json_object
  651. *
  652. * The type is coerced to a double if the passed object is not a double.
  653. * integer objects will return their double conversion. Strings will be
  654. * parsed as a double. If no conversion exists then 0.0 is returned and
  655. * errno is set to EINVAL. null is equivalent to 0 (no error values set)
  656. *
  657. * If the value is too big to fit in a double, then the value is set to
  658. * the closest infinity with errno set to ERANGE. If strings cannot be
  659. * converted to their double value, then EINVAL is set & NaN is returned.
  660. *
  661. * Arrays of length 0 are interpreted as 0 (with no error flags set).
  662. * Arrays of length 1 are effectively cast to the equivalent object and
  663. * converted using the above rules. All other arrays set the error to
  664. * EINVAL & return NaN.
  665. *
  666. * NOTE: Set errno to 0 directly before a call to this function to
  667. * determine whether or not conversion was successful (it does not clear
  668. * the value for you).
  669. *
  670. * @param obj the json_object instance
  671. * @returns a double floating point number
  672. */
  673. extern double json_object_get_double(const struct json_object *obj);
  674. /* string type methods */
  675. /** Create a new empty json_object of type json_type_string
  676. *
  677. * A copy of the string is made and the memory is managed by the json_object
  678. *
  679. * @param s the string
  680. * @returns a json_object of type json_type_string
  681. */
  682. extern struct json_object* json_object_new_string(const char *s);
  683. extern struct json_object* json_object_new_string_len(const char *s, int len);
  684. /** Get the string value of a json_object
  685. *
  686. * If the passed object is of type json_type_null (i.e. obj == NULL),
  687. * NULL is returned.
  688. *
  689. * If the passed object of type json_type_string, the string contents
  690. * are returned.
  691. *
  692. * Otherwise the JSON representation of the object is returned.
  693. *
  694. * The returned string memory is managed by the json_object and will
  695. * be freed when the reference count of the json_object drops to zero.
  696. *
  697. * @param obj the json_object instance
  698. * @returns a string or NULL
  699. */
  700. extern const char* json_object_get_string(struct json_object *obj);
  701. /** Get the string length of a json_object
  702. *
  703. * If the passed object is not of type json_type_string then zero
  704. * will be returned.
  705. *
  706. * @param obj the json_object instance
  707. * @returns int
  708. */
  709. extern int json_object_get_string_len(const struct json_object *obj);
  710. /** Check if two json_object's are equal
  711. *
  712. * If the passed objects are equal 1 will be returned.
  713. * Equality is defined as follows:
  714. * - json_objects of different types are never equal
  715. * - json_objects of the same primitive type are equal if the
  716. * c-representation of their value is equal
  717. * - json-arrays are considered equal if all values at the same
  718. * indices are equal (same order)
  719. * - Complex json_objects are considered equal if all
  720. * contained objects referenced by their key are equal,
  721. * regardless their order.
  722. *
  723. * @param obj1 the first json_object instance
  724. * @param obj2 the second json_object instance
  725. * @returns whether both objects are equal or not
  726. */
  727. extern int json_object_equal(struct json_object *obj1,
  728. struct json_object *obj2);
  729. #ifdef __cplusplus
  730. }
  731. #endif
  732. #endif