blob: cea4c81064dba3f0e0907aa201ee17ddd5a05536 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clark837240f2007-03-13 08:26:25 +00002 * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
10 */
11
12#ifndef _json_object_h_
13#define _json_object_h_
14
Michael Clarkaaec1ef2009-02-25 02:31:32 +000015#ifdef __cplusplus
16extern "C" {
17#endif
18
Michael Clarke6548a32009-01-10 13:58:06 +000019#define JSON_OBJECT_DEF_HASH_ENTRIES 16
Michael Clarkf0d08882007-03-13 08:26:18 +000020
21#undef FALSE
22#define FALSE ((boolean)0)
23
24#undef TRUE
25#define TRUE ((boolean)1)
26
Michael Clark68cafad2009-01-06 22:56:57 +000027extern const char *json_number_chars;
28extern const char *json_hex_chars;
Michael Clarkf0d08882007-03-13 08:26:18 +000029
30/* forward structure definitions */
31
32typedef int boolean;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000033typedef struct printbuf printbuf;
34typedef struct lh_table lh_table;
35typedef struct array_list array_list;
36typedef struct json_object json_object;
37typedef struct json_object_iter json_object_iter;
38typedef struct json_tokener json_tokener;
Michael Clarkf0d08882007-03-13 08:26:18 +000039
40/* supported object types */
41
Michael Clarkaaec1ef2009-02-25 02:31:32 +000042typedef enum json_type {
Michael Clarkf0d08882007-03-13 08:26:18 +000043 json_type_null,
44 json_type_boolean,
45 json_type_double,
46 json_type_int,
47 json_type_object,
48 json_type_array,
Michael Clarkc4dceae2010-10-06 16:39:20 +000049 json_type_string,
50 json_type_int64
Michael Clarkaaec1ef2009-02-25 02:31:32 +000051} json_type;
Michael Clarkf0d08882007-03-13 08:26:18 +000052
53/* reference counting functions */
54
55/**
56 * Increment the reference count of json_object
Michael Clarkf6a6e482007-03-13 08:26:23 +000057 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000058 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000059extern struct json_object* json_object_get(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +000060
61/**
62 * Decrement the reference count of json_object and free if it reaches zero
Michael Clarkf6a6e482007-03-13 08:26:23 +000063 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000064 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000065extern void json_object_put(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +000066
67
68/**
69 * Check if the json_object is of a given type
Michael Clarkf6a6e482007-03-13 08:26:23 +000070 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000071 * @param type 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,
Michael Clarkc4dceae2010-10-06 16:39:20 +000078 json_type_int64,
Michael Clarkf0d08882007-03-13 08:26:18 +000079 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000080extern int json_object_is_type(struct json_object *obj, enum json_type type);
Michael Clarkf0d08882007-03-13 08:26:18 +000081
82/**
83 * Get the type of the json_object
Michael Clarkf6a6e482007-03-13 08:26:23 +000084 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000085 * @returns type being one of:
86 json_type_boolean,
87 json_type_double,
88 json_type_int,
89 json_type_object,
90 json_type_array,
91 json_type_string,
Michael Clarkc4dceae2010-10-06 16:39:20 +000092 json_type_int64,
Michael Clarkf0d08882007-03-13 08:26:18 +000093 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000094extern enum json_type json_object_get_type(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +000095
96
97/** Stringify object to json format
Michael Clarkf6a6e482007-03-13 08:26:23 +000098 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000099 * @returns a string in JSON format
100 */
Michael Clark68cafad2009-01-06 22:56:57 +0000101extern const char* json_object_to_json_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000102
103
104/* object type methods */
105
106/** Create a new empty object
107 * @returns a json_object of type json_type_object
108 */
Michael Clark14862b12007-12-07 02:50:42 +0000109extern struct json_object* json_object_new_object(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000110
111/** Get the hashtable of a json_object of type json_type_object
Michael Clarkf6a6e482007-03-13 08:26:23 +0000112 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000113 * @returns a linkhash
114 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000115extern struct lh_table* json_object_get_object(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000116
117/** Add an object field to a json_object of type json_type_object
118 *
119 * The reference count will *not* be incremented. This is to make adding
120 * fields to objects in code more compact. If you want to retain a reference
121 * to an added object you must wrap the passed object with json_object_get
122 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000123 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000124 * @param key the object field name (a private copy will be duplicated)
125 * @param val a json_object or NULL member to associate with the given field
126 */
Michael Clark68cafad2009-01-06 22:56:57 +0000127extern void json_object_object_add(struct json_object* obj, const char *key,
Michael Clarkf0d08882007-03-13 08:26:18 +0000128 struct json_object *val);
129
130/** Get the json_object associate with a given object field
Michael Clarkf6a6e482007-03-13 08:26:23 +0000131 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000132 * @param key the object field name
133 * @returns the json_object associated with the given field name
134 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000135extern struct json_object* json_object_object_get(struct json_object* obj,
Michael Clark68cafad2009-01-06 22:56:57 +0000136 const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000137
138/** Delete the given json_object field
139 *
140 * The reference count will be decremented for the deleted object
141 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000142 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000143 * @param key the object field name
144 */
Michael Clark68cafad2009-01-06 22:56:57 +0000145extern void json_object_object_del(struct json_object* obj, const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000146
147/** Iterate through all keys and values of an object
Michael Clark4504df72007-03-13 08:26:20 +0000148 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000149 * @param key the local name for the char* key variable defined in the body
150 * @param val the local name for the json_object* object variable defined in the body
151 */
Michael Clark4504df72007-03-13 08:26:20 +0000152#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
Michael Clarkf0d08882007-03-13 08:26:18 +0000153
Michael Clark4504df72007-03-13 08:26:20 +0000154# define json_object_object_foreach(obj,key,val) \
155 char *key; struct json_object *val; \
156 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 )
157
158#else /* ANSI C or MSC */
159
160# define json_object_object_foreach(obj,key,val) \
161 char *key; struct json_object *val; struct lh_entry *entry; \
162 for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
163
164#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
165
166/** Iterate through all keys and values of an object (ANSI C Safe)
167 * @param obj the json_object instance
168 * @param iter the object iterator
169 */
170#define json_object_object_foreachC(obj,iter) \
171 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)
Michael Clarkf0d08882007-03-13 08:26:18 +0000172
173/* Array type methods */
174
175/** Create a new empty json_object of type json_type_array
176 * @returns a json_object of type json_type_array
177 */
Michael Clark14862b12007-12-07 02:50:42 +0000178extern struct json_object* json_object_new_array(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000179
180/** Get the arraylist of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000181 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000182 * @returns an arraylist
183 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000184extern struct array_list* json_object_get_array(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000185
186/** Get the length of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000187 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000188 * @returns an int
189 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000190extern int json_object_array_length(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000191
192/** Add an element to the end of a json_object of type json_type_array
193 *
194 * The reference count will *not* be incremented. This is to make adding
195 * fields to objects in code more compact. If you want to retain a reference
196 * to an added object you must wrap the passed object with json_object_get
197 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000198 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000199 * @param val the json_object to be added
200 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000201extern int json_object_array_add(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000202 struct json_object *val);
203
204/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
205 *
206 * The reference count will *not* be incremented. This is to make adding
207 * fields to objects in code more compact. If you want to retain a reference
208 * to an added object you must wrap the passed object with json_object_get
209 *
210 * The reference count of a replaced object will be decremented.
211 *
212 * The array size will be automatically be expanded to the size of the
213 * index if the index is larger than the current size.
214 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000215 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000216 * @param idx the index to insert the element at
217 * @param val the json_object to be added
218 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000219extern int json_object_array_put_idx(struct json_object *obj, int idx,
Michael Clarkf0d08882007-03-13 08:26:18 +0000220 struct json_object *val);
221
222/** Get the element at specificed index of the array (a json_object of type json_type_array)
Michael Clarkf6a6e482007-03-13 08:26:23 +0000223 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000224 * @param idx the index to get the element at
225 * @returns the json_object at the specified index (or NULL)
226 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000227extern struct json_object* json_object_array_get_idx(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000228 int idx);
229
230/* boolean type methods */
231
232/** Create a new empty json_object of type json_type_boolean
233 * @param b a boolean TRUE or FALSE (0 or 1)
234 * @returns a json_object of type json_type_boolean
235 */
236extern struct json_object* json_object_new_boolean(boolean b);
237
238/** Get the boolean value of a json_object
239 *
240 * The type is coerced to a boolean if the passed object is not a boolean.
241 * integer and double objects will return FALSE if there value is zero
242 * or TRUE otherwise. If the passed object is a string it will return
243 * TRUE if it has a non zero length. If any other object type is passed
244 * TRUE will be returned if the object is not NULL.
245 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000246 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000247 * @returns a boolean
248 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000249extern boolean json_object_get_boolean(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000250
251
252/* int type methods */
253
254/** Create a new empty json_object of type json_type_int
255 * @param i the integer
256 * @returns a json_object of type json_type_int
257 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000258extern struct json_object* json_object_new_int(int32_t i);
259
260
261/** Create a new empty json_object of type json_type_int64
262 * @param i the integer
263 * @returns a json_object of type json_type_int64
264 */
265extern struct json_object* json_object_new_int64(int64_t i);
266
Michael Clarkf0d08882007-03-13 08:26:18 +0000267
268/** Get the int value of a json_object
269 *
270 * The type is coerced to a int if the passed object is not a int.
271 * double objects will return their integer conversion. Strings will be
272 * parsed as an integer. If no conversion exists then 0 is returned.
273 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000274 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000275 * @returns an int
276 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000277extern int32_t json_object_get_int(struct json_object *obj);
278
279/** Get the int value of a json_object
280 *
281 * The type is coerced to a int64 if the passed object is not a int64.
282 * double objects will return their int64 conversion. Strings will be
283 * parsed as an int64. If no conversion exists then 0 is returned.
284 *
285 * @param obj the json_object instance
286 * @returns an int64
287 */
288extern int64_t json_object_get_int64(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000289
290
291/* double type methods */
292
293/** Create a new empty json_object of type json_type_double
294 * @param d the double
295 * @returns a json_object of type json_type_double
296 */
297extern struct json_object* json_object_new_double(double d);
298
299/** Get the double value of a json_object
300 *
301 * The type is coerced to a double if the passed object is not a double.
302 * integer objects will return their dboule conversion. Strings will be
303 * parsed as a double. If no conversion exists then 0.0 is returned.
304 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000305 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000306 * @returns an double
307 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000308extern double json_object_get_double(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000309
310
311/* string type methods */
312
313/** Create a new empty json_object of type json_type_string
314 *
315 * A copy of the string is made and the memory is managed by the json_object
316 *
317 * @param s the string
318 * @returns a json_object of type json_type_string
319 */
Michael Clark68cafad2009-01-06 22:56:57 +0000320extern struct json_object* json_object_new_string(const char *s);
Michael Clarkf0d08882007-03-13 08:26:18 +0000321
Michael Clark68cafad2009-01-06 22:56:57 +0000322extern struct json_object* json_object_new_string_len(const char *s, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +0000323
324/** Get the string value of a json_object
325 *
326 * If the passed object is not of type json_type_string then the JSON
327 * representation of the object is returned.
328 *
329 * The returned string memory is managed by the json_object and will
330 * be freed when the reference count of the json_object drops to zero.
331 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000332 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000333 * @returns a string
334 */
Michael Clark68cafad2009-01-06 22:56:57 +0000335extern const char* json_object_get_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000336
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000337#ifdef __cplusplus
338}
339#endif
340
Michael Clarkf0d08882007-03-13 08:26:18 +0000341#endif