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>
6 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
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.
13 #ifndef _json_object_h_
14 #define _json_object_h_
16 #include "json_inttypes.h"
22 #define JSON_OBJECT_DEF_HASH_ENTRIES 16
25 * A flag for the json_object_to_json_string_ext() and
26 * json_object_to_file_ext() functions which causes the output
27 * to have no extra whitespace or formatting applied.
29 #define JSON_C_TO_STRING_PLAIN 0
31 * A flag for the json_object_to_json_string_ext() and
32 * json_object_to_file_ext() functions which causes the output to have
33 * minimal whitespace inserted to make things slightly more readable.
35 #define JSON_C_TO_STRING_SPACED (1<<0)
37 * A flag for the json_object_to_json_string_ext() and
38 * json_object_to_file_ext() functions which causes
39 * the output to be formatted.
41 * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/
42 * for an example of the format.
44 #define JSON_C_TO_STRING_PRETTY (1<<1)
47 #define FALSE ((json_bool)0)
50 #define TRUE ((json_bool)1)
52 extern const char *json_number_chars;
53 extern const char *json_hex_chars;
55 /* CAW: added for ANSI C iteration correctness */
56 struct json_object_iter
59 struct json_object *val;
60 struct lh_entry *entry;
63 /* forward structure definitions */
65 typedef int json_bool;
66 typedef struct printbuf printbuf;
67 typedef struct lh_table lh_table;
68 typedef struct array_list array_list;
69 typedef struct json_object json_object;
70 typedef struct json_object_iter json_object_iter;
71 typedef struct json_tokener json_tokener;
73 /* supported object types */
75 typedef enum json_type {
76 /* If you change this, be sure to update json_type_to_name() too */
86 /* reference counting functions */
89 * Increment the reference count of json_object, thereby grabbing shared
92 * @param obj the json_object instance
94 extern struct json_object* json_object_get(struct json_object *obj);
97 * Decrement the reference count of json_object and free if it reaches zero.
98 * You must have ownership of obj prior to doing this or you will cause an
99 * imbalance in the reference count.
101 * @param obj the json_object instance
103 extern void json_object_put(struct json_object *obj);
107 * Check if the json_object is of a given type
108 * @param obj the json_object instance
109 * @param type one of:
110 json_type_null (i.e. obj == NULL),
118 extern int json_object_is_type(struct json_object *obj, enum json_type type);
121 * Get the type of the json_object. See also json_type_to_name() to turn this
122 * into a string suitable, for instance, for logging.
124 * @param obj the json_object instance
125 * @returns type being one of:
126 json_type_null (i.e. obj == NULL),
134 extern enum json_type json_object_get_type(struct json_object *obj);
137 /** Stringify object to json format.
138 * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
139 * @param obj the json_object instance
140 * @returns a string in JSON format
142 extern const char* json_object_to_json_string(struct json_object *obj);
144 /** Stringify object to json format
145 * @param obj the json_object instance
146 * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
147 * @returns a string in JSON format
149 extern const char* json_object_to_json_string_ext(struct json_object *obj, int
153 /* object type methods */
155 /** Create a new empty object with a reference count of 1. The caller of
156 * this object initially has sole ownership. Remember, when using
157 * json_object_object_add or json_object_array_put_idx, ownership will
158 * transfer to the object/array. Call json_object_get if you want to maintain
159 * shared ownership or also add this object as a child of multiple objects or
160 * arrays. Any ownerships you acquired but did not transfer must be released
161 * through json_object_put.
163 * @returns a json_object of type json_type_object
165 extern struct json_object* json_object_new_object(void);
167 /** Get the hashtable of a json_object of type json_type_object
168 * @param obj the json_object instance
169 * @returns a linkhash
171 extern struct lh_table* json_object_get_object(struct json_object *obj);
173 /** Add an object field to a json_object of type json_type_object
175 * The reference count will *not* be incremented. This is to make adding
176 * fields to objects in code more compact. If you want to retain a reference
177 * to an added object, independent of the lifetime of obj, you must wrap the
178 * passed object with json_object_get.
180 * Upon calling this, the ownership of val transfers to obj. Thus you must
181 * make sure that you do in fact have ownership over this object. For instance,
182 * json_object_new_object will give you ownership until you transfer it,
183 * whereas json_object_object_get does not.
185 * @param obj the json_object instance
186 * @param key the object field name (a private copy will be duplicated)
187 * @param val a json_object or NULL member to associate with the given field
189 extern void json_object_object_add(struct json_object* obj, const char *key,
190 struct json_object *val);
192 /** Get the json_object associate with a given object field
194 * *No* reference counts will be changed. There is no need to manually adjust
195 * reference counts through the json_object_put/json_object_get methods unless
196 * you need to have the child (value) reference maintain a different lifetime
197 * than the owning parent (obj). Ownership of the returned value is retained
198 * by obj (do not do json_object_put unless you have done a json_object_get).
199 * If you delete the value from obj (json_object_object_del) and wish to access
200 * the returned reference afterwards, make sure you have first gotten shared
201 * ownership through json_object_get (& don't forget to do a json_object_put
202 * or transfer ownership to prevent a memory leak).
204 * @param obj the json_object instance
205 * @param key the object field name
206 * @returns the json_object associated with the given field name
207 * @deprecated Please use json_object_object_get_ex
209 extern struct json_object* json_object_object_get(struct json_object* obj,
212 /** Get the json_object associated with a given object field.
214 * This returns true if the key is found, false in all other cases (including
215 * if obj isn't a json_type_object).
217 * *No* reference counts will be changed. There is no need to manually adjust
218 * reference counts through the json_object_put/json_object_get methods unless
219 * you need to have the child (value) reference maintain a different lifetime
220 * than the owning parent (obj). Ownership of value is retained by obj.
222 * @param obj the json_object instance
223 * @param key the object field name
224 * @param value a pointer where to store a reference to the json_object
225 * associated with the given field name.
227 * It is safe to pass a NULL value.
228 * @returns whether or not the key exists
230 extern json_bool json_object_object_get_ex(struct json_object* obj,
232 struct json_object **value);
234 /** Delete the given json_object field
236 * The reference count will be decremented for the deleted object. If there
237 * are no more owners of the value represented by this key, then the value is
238 * freed. Otherwise, the reference to the value will remain in memory.
240 * @param obj the json_object instance
241 * @param key the object field name
243 extern void json_object_object_del(struct json_object* obj, const char *key);
245 /** Iterate through all keys and values of an object
246 * @param obj the json_object instance
247 * @param key the local name for the char* key variable defined in the body
248 * @param val the local name for the json_object* object variable defined in
251 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
253 # define json_object_object_foreach(obj,key,val) \
254 char *key; struct json_object *val; \
255 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 )
257 #else /* ANSI C or MSC */
259 # define json_object_object_foreach(obj,key,val) \
260 char *key; struct json_object *val; struct lh_entry *entry; \
261 for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
263 #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
265 /** Iterate through all keys and values of an object (ANSI C Safe)
266 * @param obj the json_object instance
267 * @param iter the object iterator
269 #define json_object_object_foreachC(obj,iter) \
270 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)
272 /* Array type methods */
274 /** Create a new empty json_object of type json_type_array
275 * @returns a json_object of type json_type_array
277 extern struct json_object* json_object_new_array(void);
279 /** Get the arraylist of a json_object of type json_type_array
280 * @param obj the json_object instance
281 * @returns an arraylist
283 extern struct array_list* json_object_get_array(struct json_object *obj);
285 /** Get the length of a json_object of type json_type_array
286 * @param obj the json_object instance
289 extern int json_object_array_length(struct json_object *obj);
291 /** Sorts the elements of jso of type json_type_array
293 * Pointers to the json_object pointers will be passed as the two arguments
296 * @param obj the json_object instance
297 * @param sort_fn a sorting function
299 extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
301 /** Add an element to the end of a json_object of type json_type_array
303 * The reference count will *not* be incremented. This is to make adding
304 * fields to objects in code more compact. If you want to retain a reference
305 * to an added object you must wrap the passed object with json_object_get
307 * @param obj the json_object instance
308 * @param val the json_object to be added
310 extern int json_object_array_add(struct json_object *obj,
311 struct json_object *val);
313 /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
315 * The reference count will *not* be incremented. This is to make adding
316 * fields to objects in code more compact. If you want to retain a reference
317 * to an added object you must wrap the passed object with json_object_get
319 * The reference count of a replaced object will be decremented.
321 * The array size will be automatically be expanded to the size of the
322 * index if the index is larger than the current size.
324 * @param obj the json_object instance
325 * @param idx the index to insert the element at
326 * @param val the json_object to be added
328 extern int json_object_array_put_idx(struct json_object *obj, int idx,
329 struct json_object *val);
331 /** Get the element at specificed index of the array (a json_object of type json_type_array)
332 * @param obj the json_object instance
333 * @param idx the index to get the element at
334 * @returns the json_object at the specified index (or NULL)
336 extern struct json_object* json_object_array_get_idx(struct json_object *obj,
339 /* json_bool type methods */
341 /** Create a new empty json_object of type json_type_boolean
342 * @param b a json_bool TRUE or FALSE (0 or 1)
343 * @returns a json_object of type json_type_boolean
345 extern struct json_object* json_object_new_boolean(json_bool b);
347 /** Get the json_bool value of a json_object
349 * The type is coerced to a json_bool if the passed object is not a json_bool.
350 * integer and double objects will return FALSE if there value is zero
351 * or TRUE otherwise. If the passed object is a string it will return
352 * TRUE if it has a non zero length. If any other object type is passed
353 * TRUE will be returned if the object is not NULL.
355 * @param obj the json_object instance
356 * @returns a json_bool
358 extern json_bool json_object_get_boolean(struct json_object *obj);
361 /* int type methods */
363 /** Create a new empty json_object of type json_type_int
364 * Note that values are stored as 64-bit values internally.
365 * To ensure the full range is maintained, use json_object_new_int64 instead.
366 * @param i the integer
367 * @returns a json_object of type json_type_int
369 extern struct json_object* json_object_new_int(int32_t i);
372 /** Create a new empty json_object of type json_type_int
373 * @param i the integer
374 * @returns a json_object of type json_type_int
376 extern struct json_object* json_object_new_int64(int64_t i);
379 /** Get the int value of a json_object
381 * The type is coerced to a int if the passed object is not a int.
382 * double objects will return their integer conversion. Strings will be
383 * parsed as an integer. If no conversion exists then 0 is returned
384 * and errno is set to EINVAL. null is equivalent to 0 (no error values set)
386 * Note that integers are stored internally as 64-bit values.
387 * If the value of too big or too small to fit into 32-bit, INT32_MAX or
388 * INT32_MIN are returned, respectively.
390 * @param obj the json_object instance
393 extern int32_t json_object_get_int(struct json_object *obj);
395 /** Get the int value of a json_object
397 * The type is coerced to a int64 if the passed object is not a int64.
398 * double objects will return their int64 conversion. Strings will be
399 * parsed as an int64. If no conversion exists then 0 is returned.
401 * NOTE: Set errno to 0 directly before a call to this function to determine
402 * whether or not conversion was successful (it does not clear the value for
405 * @param obj the json_object instance
408 extern int64_t json_object_get_int64(struct json_object *obj);
411 /* double type methods */
413 /** Create a new empty json_object of type json_type_double
414 * @param d the double
415 * @returns a json_object of type json_type_double
417 extern struct json_object* json_object_new_double(double d);
419 /** Get the double floating point value of a json_object
421 * The type is coerced to a double if the passed object is not a double.
422 * integer objects will return their double conversion. Strings will be
423 * parsed as a double. If no conversion exists then 0.0 is returned and
424 * errno is set to EINVAL. null is equivalent to 0 (no error values set)
426 * If the value is too big to fit in a double, then the value is set to
427 * the closest infinity with errno set to ERANGE. If strings cannot be
428 * converted to their double value, then EINVAL is set & NaN is returned.
430 * Arrays of length 0 are interpreted as 0 (with no error flags set).
431 * Arrays of length 1 are effectively cast to the equivalent object and
432 * converted using the above rules. All other arrays set the error to
433 * EINVAL & return NaN.
435 * NOTE: Set errno to 0 directly before a call to this function to
436 * determine whether or not conversion was successful (it does not clear
437 * the value for you).
439 * @param obj the json_object instance
440 * @returns a double floating point number
442 extern double json_object_get_double(struct json_object *obj);
445 /* string type methods */
447 /** Create a new empty json_object of type json_type_string
449 * A copy of the string is made and the memory is managed by the json_object
451 * @param s the string
452 * @returns a json_object of type json_type_string
454 extern struct json_object* json_object_new_string(const char *s);
456 extern struct json_object* json_object_new_string_len(const char *s, int len);
458 /** Get the string value of a json_object
460 * If the passed object is not of type json_type_string then the JSON
461 * representation of the object is returned.
463 * The returned string memory is managed by the json_object and will
464 * be freed when the reference count of the json_object drops to zero.
466 * @param obj the json_object instance
469 extern const char* json_object_get_string(struct json_object *obj);
471 /** Get the string length of a json_object
473 * If the passed object is not of type json_type_string then zero
476 * @param obj the json_object instance
479 extern int json_object_get_string_len(struct json_object *obj);