Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 363 lines (301 sloc) 11.599 kb
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
1 /*
837240f7 » michaeljclark
2007-03-13 * Fix bug in escaping of control characters
2 * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
3 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
5 * Michael Clark <michael@metaparadigm.com>
6 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
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.
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
9 *
10 */
11
12 #ifndef _json_object_h_
13 #define _json_object_h_
14
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
e6548a3d » michaeljclark
2009-01-10 fix typo in json_object.h
19 #define JSON_OBJECT_DEF_HASH_ENTRIES 16
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
20
21 #undef FALSE
22 #define FALSE ((boolean)0)
23
24 #undef TRUE
25 #define TRUE ((boolean)1)
26
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
27 extern const char *json_number_chars;
28 extern const char *json_hex_chars;
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
29
43d2f417 »
2011-05-25 move definition of json_object_iter to public header to enable extern…
30 /* CAW: added for ANSI C iteration correctness */
31 struct json_object_iter
32 {
33 char *key;
34 struct json_object *val;
35 struct lh_entry *entry;
36 };
37
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
38 /* forward structure definitions */
39
40 typedef int boolean;
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
41 typedef struct printbuf printbuf;
42 typedef struct lh_table lh_table;
43 typedef struct array_list array_list;
44 typedef struct json_object json_object;
45 typedef struct json_object_iter json_object_iter;
46 typedef struct json_tokener json_tokener;
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
47
48 /* supported object types */
49
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
50 typedef enum json_type {
886c4fbe » Eric Haszlakiewicz
2011-05-03 Add a json_type_to_name() function which returns a string that descri…
51 /* If you change this, be sure to update json_type_to_name() too */
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
52 json_type_null,
53 json_type_boolean,
54 json_type_double,
55 json_type_int,
56 json_type_object,
57 json_type_array,
c4dceae1 » michaeljclark
2010-10-06 * Add int64 support. Two new functions json_object_net_int64 and
58 json_type_string,
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
59 } json_type;
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
60
61 /* reference counting functions */
62
63 /**
64 * Increment the reference count of json_object
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
65 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
66 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
67 extern struct json_object* json_object_get(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
68
69 /**
70 * Decrement the reference count of json_object and free if it reaches zero
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
71 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
72 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
73 extern void json_object_put(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
74
75
76 /**
77 * Check if the json_object is of a given type
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
78 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
79 * @param type one of:
80 json_type_boolean,
81 json_type_double,
82 json_type_int,
83 json_type_object,
84 json_type_array,
85 json_type_string,
86 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
87 extern int json_object_is_type(struct json_object *obj, enum json_type type);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
88
89 /**
90 * Get the type of the json_object
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
91 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
92 * @returns type being one of:
93 json_type_boolean,
94 json_type_double,
95 json_type_int,
96 json_type_object,
97 json_type_array,
98 json_type_string,
99 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
100 extern enum json_type json_object_get_type(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
101
102
103 /** Stringify object to json format
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
104 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
105 * @returns a string in JSON format
106 */
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
107 extern const char* json_object_to_json_string(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
108
109
110 /* object type methods */
111
112 /** Create a new empty object
113 * @returns a json_object of type json_type_object
114 */
14862b1e » michaeljclark
2007-12-07 Patch allows for json-c compile with -Werror and not fail due to
115 extern struct json_object* json_object_new_object(void);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
116
117 /** Get the hashtable of a json_object of type json_type_object
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
118 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
119 * @returns a linkhash
120 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
121 extern struct lh_table* json_object_get_object(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
122
123 /** Add an object field to a json_object of type json_type_object
124 *
125 * The reference count will *not* be incremented. This is to make adding
126 * fields to objects in code more compact. If you want to retain a reference
127 * to an added object you must wrap the passed object with json_object_get
128 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
129 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
130 * @param key the object field name (a private copy will be duplicated)
131 * @param val a json_object or NULL member to associate with the given field
132 */
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
133 extern void json_object_object_add(struct json_object* obj, const char *key,
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
134 struct json_object *val);
135
136 /** Get the json_object associate with a given object field
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
137 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
138 * @param key the object field name
139 * @returns the json_object associated with the given field name
140 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
141 extern struct json_object* json_object_object_get(struct json_object* obj,
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
142 const char *key);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
143
144 /** Delete the given json_object field
145 *
146 * The reference count will be decremented for the deleted object
147 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
148 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
149 * @param key the object field name
150 */
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
151 extern void json_object_object_del(struct json_object* obj, const char *key);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
152
153 /** Iterate through all keys and values of an object
4504df71 » michaeljclark
2007-03-13 * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
154 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
155 * @param key the local name for the char* key variable defined in the body
156 * @param val the local name for the json_object* object variable defined in the body
157 */
4504df71 » michaeljclark
2007-03-13 * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
158 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
159
160 # define json_object_object_foreach(obj,key,val) \
161 char *key; struct json_object *val; \
162 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 )
163
164 #else /* ANSI C or MSC */
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
165
4504df71 » michaeljclark
2007-03-13 * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
166 # define json_object_object_foreach(obj,key,val) \
167 char *key; struct json_object *val; struct lh_entry *entry; \
168 for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
169
170 #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
171
172 /** Iterate through all keys and values of an object (ANSI C Safe)
173 * @param obj the json_object instance
174 * @param iter the object iterator
175 */
176 #define json_object_object_foreachC(obj,iter) \
177 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)
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
178
179 /* Array type methods */
180
181 /** Create a new empty json_object of type json_type_array
182 * @returns a json_object of type json_type_array
183 */
14862b1e » michaeljclark
2007-12-07 Patch allows for json-c compile with -Werror and not fail due to
184 extern struct json_object* json_object_new_array(void);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
185
186 /** Get the arraylist of a json_object of type json_type_array
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
187 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
188 * @returns an arraylist
189 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
190 extern struct array_list* json_object_get_array(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
191
192 /** Get the length of a json_object of type json_type_array
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
193 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
194 * @returns an int
195 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
196 extern int json_object_array_length(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
197
198 /** Add an element to the end of a json_object of type json_type_array
199 *
200 * The reference count will *not* be incremented. This is to make adding
201 * fields to objects in code more compact. If you want to retain a reference
202 * to an added object you must wrap the passed object with json_object_get
203 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
204 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
205 * @param val the json_object to be added
206 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
207 extern int json_object_array_add(struct json_object *obj,
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
208 struct json_object *val);
209
210 /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
211 *
212 * The reference count will *not* be incremented. This is to make adding
213 * fields to objects in code more compact. If you want to retain a reference
214 * to an added object you must wrap the passed object with json_object_get
215 *
216 * The reference count of a replaced object will be decremented.
217 *
218 * The array size will be automatically be expanded to the size of the
219 * index if the index is larger than the current size.
220 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
221 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
222 * @param idx the index to insert the element at
223 * @param val the json_object to be added
224 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
225 extern int json_object_array_put_idx(struct json_object *obj, int idx,
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
226 struct json_object *val);
227
228 /** Get the element at specificed index of the array (a json_object of type json_type_array)
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
229 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
230 * @param idx the index to get the element at
231 * @returns the json_object at the specified index (or NULL)
232 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
233 extern struct json_object* json_object_array_get_idx(struct json_object *obj,
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
234 int idx);
235
236 /* boolean type methods */
237
238 /** Create a new empty json_object of type json_type_boolean
239 * @param b a boolean TRUE or FALSE (0 or 1)
240 * @returns a json_object of type json_type_boolean
241 */
242 extern struct json_object* json_object_new_boolean(boolean b);
243
244 /** Get the boolean value of a json_object
245 *
246 * The type is coerced to a boolean if the passed object is not a boolean.
247 * integer and double objects will return FALSE if there value is zero
248 * or TRUE otherwise. If the passed object is a string it will return
249 * TRUE if it has a non zero length. If any other object type is passed
250 * TRUE will be returned if the object is not NULL.
251 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
252 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
253 * @returns a boolean
254 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
255 extern boolean json_object_get_boolean(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
256
257
258 /* int type methods */
259
260 /** Create a new empty json_object of type json_type_int
252669ce » ehaszla
2010-12-07 Simplify things by storing integer values only as int64_t's internall…
261 * Note that values are stored as 64-bit values internally.
262 * To ensure the full range is maintained, use json_object_new_int64 instead.
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
263 * @param i the integer
264 * @returns a json_object of type json_type_int
265 */
c4dceae1 » michaeljclark
2010-10-06 * Add int64 support. Two new functions json_object_net_int64 and
266 extern struct json_object* json_object_new_int(int32_t i);
267
268
252669ce » ehaszla
2010-12-07 Simplify things by storing integer values only as int64_t's internall…
269 /** Create a new empty json_object of type json_type_int
c4dceae1 » michaeljclark
2010-10-06 * Add int64 support. Two new functions json_object_net_int64 and
270 * @param i the integer
252669ce » ehaszla
2010-12-07 Simplify things by storing integer values only as int64_t's internall…
271 * @returns a json_object of type json_type_int
c4dceae1 » michaeljclark
2010-10-06 * Add int64 support. Two new functions json_object_net_int64 and
272 */
273 extern struct json_object* json_object_new_int64(int64_t i);
274
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
275
276 /** Get the int value of a json_object
277 *
278 * The type is coerced to a int if the passed object is not a int.
279 * double objects will return their integer conversion. Strings will be
280 * parsed as an integer. If no conversion exists then 0 is returned.
281 *
252669ce » ehaszla
2010-12-07 Simplify things by storing integer values only as int64_t's internall…
282 * Note that integers are stored internally as 64-bit values.
283 * If the value of too big or too small to fit into 32-bit, INT32_MAX or
284 * INT32_MIN are returned, respectively.
285 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
286 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
287 * @returns an int
288 */
c4dceae1 » michaeljclark
2010-10-06 * Add int64 support. Two new functions json_object_net_int64 and
289 extern int32_t json_object_get_int(struct json_object *obj);
290
291 /** Get the int value of a json_object
292 *
293 * The type is coerced to a int64 if the passed object is not a int64.
294 * double objects will return their int64 conversion. Strings will be
295 * parsed as an int64. If no conversion exists then 0 is returned.
296 *
297 * @param obj the json_object instance
298 * @returns an int64
299 */
300 extern int64_t json_object_get_int64(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
301
302
303 /* double type methods */
304
305 /** Create a new empty json_object of type json_type_double
306 * @param d the double
307 * @returns a json_object of type json_type_double
308 */
309 extern struct json_object* json_object_new_double(double d);
310
311 /** Get the double value of a json_object
312 *
313 * The type is coerced to a double if the passed object is not a double.
314 * integer objects will return their dboule conversion. Strings will be
315 * parsed as a double. If no conversion exists then 0.0 is returned.
316 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
317 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
318 * @returns an double
319 */
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
320 extern double json_object_get_double(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
321
322
323 /* string type methods */
324
325 /** Create a new empty json_object of type json_type_string
326 *
327 * A copy of the string is made and the memory is managed by the json_object
328 *
329 * @param s the string
330 * @returns a json_object of type json_type_string
331 */
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
332 extern struct json_object* json_object_new_string(const char *s);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
333
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
334 extern struct json_object* json_object_new_string_len(const char *s, int len);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
335
336 /** Get the string value of a json_object
337 *
338 * If the passed object is not of type json_type_string then the JSON
339 * representation of the object is returned.
340 *
341 * The returned string memory is managed by the json_object and will
342 * be freed when the reference count of the json_object drops to zero.
343 *
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
344 * @param obj the json_object instance
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
345 * @returns a string
346 */
68cafad0 » michaeljclark
2009-01-06 Add const correctness to public interfaces
347 extern const char* json_object_get_string(struct json_object *obj);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
348
ac601b5b »
2011-01-14 update json_object_new_string_len, json_escape_str (internal). Writer…
349 /** Get the string length of a json_object
350 *
351 * If the passed object is not of type json_type_string then zero
352 * will be returned.
353 *
354 * @param obj the json_object instance
355 * @returns int
356 */
357 extern int json_object_get_string_len(struct json_object *obj);
358
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
359 #ifdef __cplusplus
360 }
361 #endif
362
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
363 #endif
Something went wrong with that request. Please try again.