root/src/json-lib/json_object.h

Revision efa9375336eec306ede14a9a1032985a71482810, 10.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

refs #294 resmon no support arbitrary json objects

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