2 * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
12 #ifndef _json_object_h_
13 #define _json_object_h_
15 #define JSON_OBJECT_DEF_HASH_ENTIRES 16
18 #define FALSE ((boolean)0)
21 #define TRUE ((boolean)1)
23 extern char *json_number_chars;
24 extern char *json_hex_chars;
26 /* forward structure definitions */
33 struct json_object_iter;
35 /* supported object types */
47 /* reference counting functions */
50 * Increment the reference count of json_object
51 * @param obj the json_object instance
53 extern struct json_object* json_object_get(struct json_object *obj);
56 * Decrement the reference count of json_object and free if it reaches zero
57 * @param obj the json_object instance
59 extern void json_object_put(struct json_object *obj);
63 * Check if the json_object is of a given type
64 * @param obj the json_object instance
73 extern int json_object_is_type(struct json_object *obj, enum json_type type);
76 * Get the type of the json_object
77 * @param obj the json_object instance
78 * @returns type being one of:
86 extern enum json_type json_object_get_type(struct json_object *obj);
89 /** Stringify object to json format
90 * @param obj the json_object instance
91 * @returns a string in JSON format
93 extern char* json_object_to_json_string(struct json_object *obj);
96 /* object type methods */
98 /** Create a new empty object
99 * @returns a json_object of type json_type_object
101 extern struct json_object* json_object_new_object();
103 /** Get the hashtable of a json_object of type json_type_object
104 * @param obj the json_object instance
105 * @returns a linkhash
107 extern struct lh_table* json_object_get_object(struct json_object *obj);
109 /** Add an object field to a json_object of type json_type_object
111 * The reference count will *not* be incremented. This is to make adding
112 * fields to objects in code more compact. If you want to retain a reference
113 * to an added object you must wrap the passed object with json_object_get
115 * @param obj the json_object instance
116 * @param key the object field name (a private copy will be duplicated)
117 * @param val a json_object or NULL member to associate with the given field
119 extern void json_object_object_add(struct json_object* obj, char *key,
120 struct json_object *val);
122 /** Get the json_object associate with a given object field
123 * @param obj the json_object instance
124 * @param key the object field name
125 * @returns the json_object associated with the given field name
127 extern struct json_object* json_object_object_get(struct json_object* obj,
130 /** Delete the given json_object field
132 * The reference count will be decremented for the deleted object
134 * @param obj the json_object instance
135 * @param key the object field name
137 extern void json_object_object_del(struct json_object* obj, char *key);
139 /** Iterate through all keys and values of an object
140 * @param obj the json_object instance
141 * @param key the local name for the char* key variable defined in the body
142 * @param val the local name for the json_object* object variable defined in the body
144 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
146 # define json_object_object_foreach(obj,key,val) \
147 char *key; struct json_object *val; \
148 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 )
150 #else /* ANSI C or MSC */
152 # define json_object_object_foreach(obj,key,val) \
153 char *key; struct json_object *val; struct lh_entry *entry; \
154 for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
156 #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
158 /** Iterate through all keys and values of an object (ANSI C Safe)
159 * @param obj the json_object instance
160 * @param iter the object iterator
162 #define json_object_object_foreachC(obj,iter) \
163 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)
165 /* Array type methods */
167 /** Create a new empty json_object of type json_type_array
168 * @returns a json_object of type json_type_array
170 extern struct json_object* json_object_new_array();
172 /** Get the arraylist of a json_object of type json_type_array
173 * @param obj the json_object instance
174 * @returns an arraylist
176 extern struct array_list* json_object_get_array(struct json_object *obj);
178 /** Get the length of a json_object of type json_type_array
179 * @param obj the json_object instance
182 extern int json_object_array_length(struct json_object *obj);
184 /** Add an element to the end of a json_object of type json_type_array
186 * The reference count will *not* be incremented. This is to make adding
187 * fields to objects in code more compact. If you want to retain a reference
188 * to an added object you must wrap the passed object with json_object_get
190 * @param obj the json_object instance
191 * @param val the json_object to be added
193 extern int json_object_array_add(struct json_object *obj,
194 struct json_object *val);
196 /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
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 you must wrap the passed object with json_object_get
202 * The reference count of a replaced object will be decremented.
204 * The array size will be automatically be expanded to the size of the
205 * index if the index is larger than the current size.
207 * @param obj the json_object instance
208 * @param idx the index to insert the element at
209 * @param val the json_object to be added
211 extern int json_object_array_put_idx(struct json_object *obj, int idx,
212 struct json_object *val);
214 /** Get the element at specificed index of the array (a json_object of type json_type_array)
215 * @param obj the json_object instance
216 * @param idx the index to get the element at
217 * @returns the json_object at the specified index (or NULL)
219 extern struct json_object* json_object_array_get_idx(struct json_object *obj,
222 /* boolean type methods */
224 /** Create a new empty json_object of type json_type_boolean
225 * @param b a boolean TRUE or FALSE (0 or 1)
226 * @returns a json_object of type json_type_boolean
228 extern struct json_object* json_object_new_boolean(boolean b);
230 /** Get the boolean value of a json_object
232 * The type is coerced to a boolean if the passed object is not a boolean.
233 * integer and double objects will return FALSE if there value is zero
234 * or TRUE otherwise. If the passed object is a string it will return
235 * TRUE if it has a non zero length. If any other object type is passed
236 * TRUE will be returned if the object is not NULL.
238 * @param obj the json_object instance
241 extern boolean json_object_get_boolean(struct json_object *obj);
244 /* int type methods */
246 /** Create a new empty json_object of type json_type_int
247 * @param i the integer
248 * @returns a json_object of type json_type_int
250 extern struct json_object* json_object_new_int(int i);
252 /** Get the int value of a json_object
254 * The type is coerced to a int if the passed object is not a int.
255 * double objects will return their integer conversion. Strings will be
256 * parsed as an integer. If no conversion exists then 0 is returned.
258 * @param obj the json_object instance
261 extern int json_object_get_int(struct json_object *obj);
264 /* double type methods */
266 /** Create a new empty json_object of type json_type_double
267 * @param d the double
268 * @returns a json_object of type json_type_double
270 extern struct json_object* json_object_new_double(double d);
272 /** Get the double value of a json_object
274 * The type is coerced to a double if the passed object is not a double.
275 * integer objects will return their dboule conversion. Strings will be
276 * parsed as a double. If no conversion exists then 0.0 is returned.
278 * @param obj the json_object instance
281 extern double json_object_get_double(struct json_object *obj);
284 /* string type methods */
286 /** Create a new empty json_object of type json_type_string
288 * A copy of the string is made and the memory is managed by the json_object
290 * @param s the string
291 * @returns a json_object of type json_type_string
293 extern struct json_object* json_object_new_string(char *s);
295 extern struct json_object* json_object_new_string_len(char *s, int len);
297 /** Get the string value of a json_object
299 * If the passed object is not of type json_type_string then the JSON
300 * representation of the object is returned.
302 * The returned string memory is managed by the json_object and will
303 * be freed when the reference count of the json_object drops to zero.
305 * @param obj the json_object instance
308 extern char* json_object_get_string(struct json_object *obj);