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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #ifndef _json_object_h_
  12. #define _json_object_h_
  13. #define JSON_OBJECT_DEF_HASH_ENTIRES 16
  14. #undef FALSE
  15. #define FALSE ((boolean)0)
  16. #undef TRUE
  17. #define TRUE ((boolean)1)
  18. extern const char *json_number_chars;
  19. extern const char *json_hex_chars;
  20. /* forward structure definitions */
  21. typedef int boolean;
  22. struct printbuf;
  23. struct lh_table;
  24. struct array_list;
  25. struct json_object;
  26. struct json_object_iter;
  27. /* supported object types */
  28. enum json_type {
  29. json_type_null,
  30. json_type_boolean,
  31. json_type_double,
  32. json_type_int,
  33. json_type_object,
  34. json_type_array,
  35. json_type_string
  36. };
  37. /* reference counting functions */
  38. /**
  39. * Increment the reference count of json_object
  40. * @param obj the json_object instance
  41. */
  42. extern struct json_object* json_object_get(struct json_object *obj);
  43. /**
  44. * Decrement the reference count of json_object and free if it reaches zero
  45. * @param obj the json_object instance
  46. */
  47. extern void json_object_put(struct json_object *obj);
  48. /**
  49. * Check if the json_object is of a given type
  50. * @param obj the json_object instance
  51. * @param type one of:
  52. json_type_boolean,
  53. json_type_double,
  54. json_type_int,
  55. json_type_object,
  56. json_type_array,
  57. json_type_string,
  58. */
  59. extern int json_object_is_type(struct json_object *obj, enum json_type type);
  60. /**
  61. * Get the type of the json_object
  62. * @param obj the json_object instance
  63. * @returns type being one of:
  64. json_type_boolean,
  65. json_type_double,
  66. json_type_int,
  67. json_type_object,
  68. json_type_array,
  69. json_type_string,
  70. */
  71. extern enum json_type json_object_get_type(struct json_object *obj);
  72. /** Stringify object to json format
  73. * @param obj the json_object instance
  74. * @returns a string in JSON format
  75. */
  76. extern const char* json_object_to_json_string(struct json_object *obj);
  77. /* object type methods */
  78. /** Create a new empty object
  79. * @returns a json_object of type json_type_object
  80. */
  81. extern struct json_object* json_object_new_object(void);
  82. /** Get the hashtable of a json_object of type json_type_object
  83. * @param obj the json_object instance
  84. * @returns a linkhash
  85. */
  86. extern struct lh_table* json_object_get_object(struct json_object *obj);
  87. /** Add an object field to a json_object of type json_type_object
  88. *
  89. * The reference count will *not* be incremented. This is to make adding
  90. * fields to objects in code more compact. If you want to retain a reference
  91. * to an added object you must wrap the passed object with json_object_get
  92. *
  93. * @param obj the json_object instance
  94. * @param key the object field name (a private copy will be duplicated)
  95. * @param val a json_object or NULL member to associate with the given field
  96. */
  97. extern void json_object_object_add(struct json_object* obj, const char *key,
  98. struct json_object *val);
  99. /** Get the json_object associate with a given object field
  100. * @param obj the json_object instance
  101. * @param key the object field name
  102. * @returns the json_object associated with the given field name
  103. */
  104. extern struct json_object* json_object_object_get(struct json_object* obj,
  105. const char *key);
  106. /** Delete the given json_object field
  107. *
  108. * The reference count will be decremented for the deleted object
  109. *
  110. * @param obj the json_object instance
  111. * @param key the object field name
  112. */
  113. extern void json_object_object_del(struct json_object* obj, const char *key);
  114. /** Iterate through all keys and values of an object
  115. * @param obj the json_object instance
  116. * @param key the local name for the char* key variable defined in the body
  117. * @param val the local name for the json_object* object variable defined in the body
  118. */
  119. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  120. # define json_object_object_foreach(obj,key,val) \
  121. char *key; struct json_object *val; \
  122. for(struct lh_entry *entry = json_object_get_object(obj)->head; ({ if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } ; entry; }); entry = entry->next )
  123. #else /* ANSI C or MSC */
  124. # define json_object_object_foreach(obj,key,val) \
  125. char *key; struct json_object *val; struct lh_entry *entry; \
  126. for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
  127. #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
  128. /** Iterate through all keys and values of an object (ANSI C Safe)
  129. * @param obj the json_object instance
  130. * @param iter the object iterator
  131. */
  132. #define json_object_object_foreachC(obj,iter) \
  133. 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)
  134. /* Array type methods */
  135. /** Create a new empty json_object of type json_type_array
  136. * @returns a json_object of type json_type_array
  137. */
  138. extern struct json_object* json_object_new_array(void);
  139. /** Get the arraylist of a json_object of type json_type_array
  140. * @param obj the json_object instance
  141. * @returns an arraylist
  142. */
  143. extern struct array_list* json_object_get_array(struct json_object *obj);
  144. /** Get the length of a json_object of type json_type_array
  145. * @param obj the json_object instance
  146. * @returns an int
  147. */
  148. extern int json_object_array_length(struct json_object *obj);
  149. /** Add an element to the end of a json_object of type json_type_array
  150. *
  151. * The reference count will *not* be incremented. This is to make adding
  152. * fields to objects in code more compact. If you want to retain a reference
  153. * to an added object you must wrap the passed object with json_object_get
  154. *
  155. * @param obj the json_object instance
  156. * @param val the json_object to be added
  157. */
  158. extern int json_object_array_add(struct json_object *obj,
  159. struct json_object *val);
  160. /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
  161. *
  162. * The reference count will *not* be incremented. This is to make adding
  163. * fields to objects in code more compact. If you want to retain a reference
  164. * to an added object you must wrap the passed object with json_object_get
  165. *
  166. * The reference count of a replaced object will be decremented.
  167. *
  168. * The array size will be automatically be expanded to the size of the
  169. * index if the index is larger than the current size.
  170. *
  171. * @param obj the json_object instance
  172. * @param idx the index to insert the element at
  173. * @param val the json_object to be added
  174. */
  175. extern int json_object_array_put_idx(struct json_object *obj, int idx,
  176. struct json_object *val);
  177. /** Get the element at specificed index of the array (a json_object of type json_type_array)
  178. * @param obj the json_object instance
  179. * @param idx the index to get the element at
  180. * @returns the json_object at the specified index (or NULL)
  181. */
  182. extern struct json_object* json_object_array_get_idx(struct json_object *obj,
  183. int idx);
  184. /* boolean type methods */
  185. /** Create a new empty json_object of type json_type_boolean
  186. * @param b a boolean TRUE or FALSE (0 or 1)
  187. * @returns a json_object of type json_type_boolean
  188. */
  189. extern struct json_object* json_object_new_boolean(boolean b);
  190. /** Get the boolean value of a json_object
  191. *
  192. * The type is coerced to a boolean if the passed object is not a boolean.
  193. * integer and double objects will return FALSE if there value is zero
  194. * or TRUE otherwise. If the passed object is a string it will return
  195. * TRUE if it has a non zero length. If any other object type is passed
  196. * TRUE will be returned if the object is not NULL.
  197. *
  198. * @param obj the json_object instance
  199. * @returns a boolean
  200. */
  201. extern boolean json_object_get_boolean(struct json_object *obj);
  202. /* int type methods */
  203. /** Create a new empty json_object of type json_type_int
  204. * @param i the integer
  205. * @returns a json_object of type json_type_int
  206. */
  207. extern struct json_object* json_object_new_int(int i);
  208. /** Get the int value of a json_object
  209. *
  210. * The type is coerced to a int if the passed object is not a int.
  211. * double objects will return their integer conversion. Strings will be
  212. * parsed as an integer. If no conversion exists then 0 is returned.
  213. *
  214. * @param obj the json_object instance
  215. * @returns an int
  216. */
  217. extern int json_object_get_int(struct json_object *obj);
  218. /* double type methods */
  219. /** Create a new empty json_object of type json_type_double
  220. * @param d the double
  221. * @returns a json_object of type json_type_double
  222. */
  223. extern struct json_object* json_object_new_double(double d);
  224. /** Get the double value of a json_object
  225. *
  226. * The type is coerced to a double if the passed object is not a double.
  227. * integer objects will return their dboule conversion. Strings will be
  228. * parsed as a double. If no conversion exists then 0.0 is returned.
  229. *
  230. * @param obj the json_object instance
  231. * @returns an double
  232. */
  233. extern double json_object_get_double(struct json_object *obj);
  234. /* string type methods */
  235. /** Create a new empty json_object of type json_type_string
  236. *
  237. * A copy of the string is made and the memory is managed by the json_object
  238. *
  239. * @param s the string
  240. * @returns a json_object of type json_type_string
  241. */
  242. extern struct json_object* json_object_new_string(const char *s);
  243. extern struct json_object* json_object_new_string_len(const char *s, int len);
  244. /** Get the string value of a json_object
  245. *
  246. * If the passed object is not of type json_type_string then the JSON
  247. * representation of the object is returned.
  248. *
  249. * The returned string memory is managed by the json_object and will
  250. * be freed when the reference count of the json_object drops to zero.
  251. *
  252. * @param obj the json_object instance
  253. * @returns a string
  254. */
  255. extern const char* json_object_get_string(struct json_object *obj);
  256. #endif

No Description

Contributors (1)