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

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

No Description

Contributors (1)