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

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

No Description

Contributors (1)