blob: 1ee9fb0be55a38ebbccd15d215c2029d015dbe8f [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
Eric Haszlakiewiczb21b1372012-02-15 20:44:54 -060015#include "json_inttypes.h"
16
Michael Clarkaaec1ef2009-02-25 02:31:32 +000017#ifdef __cplusplus
18extern "C" {
19#endif
20
Michael Clarke6548a32009-01-10 13:58:06 +000021#define JSON_OBJECT_DEF_HASH_ENTRIES 16
Michael Clarkf0d08882007-03-13 08:26:18 +000022
23#undef FALSE
Keith Derrick37e74672012-03-26 14:29:31 -070024#define FALSE ((json_bool)0)
Michael Clarkf0d08882007-03-13 08:26:18 +000025
26#undef TRUE
Keith Derrick37e74672012-03-26 14:29:31 -070027#define TRUE ((json_bool)1)
Michael Clarkf0d08882007-03-13 08:26:18 +000028
Michael Clark68cafad2009-01-06 22:56:57 +000029extern const char *json_number_chars;
30extern const char *json_hex_chars;
Michael Clarkf0d08882007-03-13 08:26:18 +000031
Jehiah Czebotar43d2f412011-05-25 04:49:20 +000032/* CAW: added for ANSI C iteration correctness */
33struct json_object_iter
34{
35 char *key;
36 struct json_object *val;
37 struct lh_entry *entry;
38};
39
Michael Clarkf0d08882007-03-13 08:26:18 +000040/* forward structure definitions */
41
Keith Derrick37e74672012-03-26 14:29:31 -070042typedef int json_bool;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000043typedef struct printbuf printbuf;
44typedef struct lh_table lh_table;
45typedef struct array_list array_list;
46typedef struct json_object json_object;
47typedef struct json_object_iter json_object_iter;
48typedef struct json_tokener json_tokener;
Michael Clarkf0d08882007-03-13 08:26:18 +000049
50/* supported object types */
51
Michael Clarkaaec1ef2009-02-25 02:31:32 +000052typedef enum json_type {
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +000053 /* If you change this, be sure to update json_type_to_name() too */
Michael Clarkf0d08882007-03-13 08:26:18 +000054 json_type_null,
55 json_type_boolean,
56 json_type_double,
57 json_type_int,
58 json_type_object,
59 json_type_array,
Michael Clarkc4dceae2010-10-06 16:39:20 +000060 json_type_string,
Michael Clarkaaec1ef2009-02-25 02:31:32 +000061} json_type;
Michael Clarkf0d08882007-03-13 08:26:18 +000062
63/* reference counting functions */
64
65/**
66 * Increment the reference count of json_object
Michael Clarkf6a6e482007-03-13 08:26:23 +000067 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000068 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000069extern struct json_object* json_object_get(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +000070
71/**
72 * Decrement the reference count of json_object and free if it reaches zero
Michael Clarkf6a6e482007-03-13 08:26:23 +000073 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000074 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000075extern void json_object_put(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +000076
77
78/**
79 * Check if the json_object is of a given type
Michael Clarkf6a6e482007-03-13 08:26:23 +000080 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000081 * @param type one of:
82 json_type_boolean,
83 json_type_double,
84 json_type_int,
85 json_type_object,
86 json_type_array,
87 json_type_string,
88 */
Michael Clarkf6a6e482007-03-13 08:26:23 +000089extern int json_object_is_type(struct json_object *obj, enum json_type type);
Michael Clarkf0d08882007-03-13 08:26:18 +000090
91/**
92 * Get the type of the json_object
Michael Clarkf6a6e482007-03-13 08:26:23 +000093 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +000094 * @returns type being one of:
95 json_type_boolean,
96 json_type_double,
97 json_type_int,
98 json_type_object,
99 json_type_array,
100 json_type_string,
101 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000102extern enum json_type json_object_get_type(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000103
104
105/** Stringify object to json format
Michael Clarkf6a6e482007-03-13 08:26:23 +0000106 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000107 * @returns a string in JSON format
108 */
Michael Clark68cafad2009-01-06 22:56:57 +0000109extern const char* json_object_to_json_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000110
111
112/* object type methods */
113
114/** Create a new empty object
115 * @returns a json_object of type json_type_object
116 */
Michael Clark14862b12007-12-07 02:50:42 +0000117extern struct json_object* json_object_new_object(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000118
119/** Get the hashtable of a json_object of type json_type_object
Michael Clarkf6a6e482007-03-13 08:26:23 +0000120 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000121 * @returns a linkhash
122 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000123extern struct lh_table* json_object_get_object(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000124
125/** Add an object field to a json_object of type json_type_object
126 *
127 * The reference count will *not* be incremented. This is to make adding
128 * fields to objects in code more compact. If you want to retain a reference
129 * to an added object you must wrap the passed object with json_object_get
130 *
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 (a private copy will be duplicated)
133 * @param val a json_object or NULL member to associate with the given field
134 */
Michael Clark68cafad2009-01-06 22:56:57 +0000135extern void json_object_object_add(struct json_object* obj, const char *key,
Michael Clarkf0d08882007-03-13 08:26:18 +0000136 struct json_object *val);
137
138/** Get the json_object associate with a given object field
Michael Clarkf6a6e482007-03-13 08:26:23 +0000139 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000140 * @param key the object field name
141 * @returns the json_object associated with the given field name
142 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000143extern struct json_object* json_object_object_get(struct json_object* obj,
Michael Clark68cafad2009-01-06 22:56:57 +0000144 const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000145
146/** Delete the given json_object field
147 *
148 * The reference count will be decremented for the deleted object
149 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000150 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000151 * @param key the object field name
152 */
Michael Clark68cafad2009-01-06 22:56:57 +0000153extern void json_object_object_del(struct json_object* obj, const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000154
155/** Iterate through all keys and values of an object
Michael Clark4504df72007-03-13 08:26:20 +0000156 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000157 * @param key the local name for the char* key variable defined in the body
158 * @param val the local name for the json_object* object variable defined in the body
159 */
Michael Clark4504df72007-03-13 08:26:20 +0000160#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
Michael Clarkf0d08882007-03-13 08:26:18 +0000161
Michael Clark4504df72007-03-13 08:26:20 +0000162# define json_object_object_foreach(obj,key,val) \
163 char *key; struct json_object *val; \
164 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 )
165
166#else /* ANSI C or MSC */
167
168# define json_object_object_foreach(obj,key,val) \
169 char *key; struct json_object *val; struct lh_entry *entry; \
170 for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
171
172#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
173
174/** Iterate through all keys and values of an object (ANSI C Safe)
175 * @param obj the json_object instance
176 * @param iter the object iterator
177 */
178#define json_object_object_foreachC(obj,iter) \
179 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 +0000180
181/* Array type methods */
182
183/** Create a new empty json_object of type json_type_array
184 * @returns a json_object of type json_type_array
185 */
Michael Clark14862b12007-12-07 02:50:42 +0000186extern struct json_object* json_object_new_array(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000187
188/** Get the arraylist of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000189 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000190 * @returns an arraylist
191 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000192extern struct array_list* json_object_get_array(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000193
194/** Get the length of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000195 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000196 * @returns an int
197 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000198extern int json_object_array_length(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000199
Frederik Deweerdtc43871c2011-10-07 21:07:18 +0200200/** Sorts the elements of jso of type json_type_array
201*
202* Pointers to the json_object pointers will be passed as the two arguments
203* to @sort_fn
204*
205* @param obj the json_object instance
206* @param sort_fn a sorting function
207*/
208extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
209
Michael Clarkf0d08882007-03-13 08:26:18 +0000210/** Add an element to the end of 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 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000216 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000217 * @param val the json_object to be added
218 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000219extern int json_object_array_add(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000220 struct json_object *val);
221
222/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
223 *
224 * The reference count will *not* be incremented. This is to make adding
225 * fields to objects in code more compact. If you want to retain a reference
226 * to an added object you must wrap the passed object with json_object_get
227 *
228 * The reference count of a replaced object will be decremented.
229 *
230 * The array size will be automatically be expanded to the size of the
231 * index if the index is larger than the current size.
232 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000233 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000234 * @param idx the index to insert the element at
235 * @param val the json_object to be added
236 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000237extern int json_object_array_put_idx(struct json_object *obj, int idx,
Michael Clarkf0d08882007-03-13 08:26:18 +0000238 struct json_object *val);
239
240/** Get the element at specificed index of the array (a json_object of type json_type_array)
Michael Clarkf6a6e482007-03-13 08:26:23 +0000241 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000242 * @param idx the index to get the element at
243 * @returns the json_object at the specified index (or NULL)
244 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000245extern struct json_object* json_object_array_get_idx(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000246 int idx);
247
Keith Derrick37e74672012-03-26 14:29:31 -0700248/* json_bool type methods */
Michael Clarkf0d08882007-03-13 08:26:18 +0000249
250/** Create a new empty json_object of type json_type_boolean
Keith Derrick37e74672012-03-26 14:29:31 -0700251 * @param b a json_bool TRUE or FALSE (0 or 1)
Michael Clarkf0d08882007-03-13 08:26:18 +0000252 * @returns a json_object of type json_type_boolean
253 */
Keith Derrick37e74672012-03-26 14:29:31 -0700254extern struct json_object* json_object_new_boolean(json_bool b);
Michael Clarkf0d08882007-03-13 08:26:18 +0000255
Keith Derrick37e74672012-03-26 14:29:31 -0700256/** Get the json_bool value of a json_object
Michael Clarkf0d08882007-03-13 08:26:18 +0000257 *
Keith Derrick37e74672012-03-26 14:29:31 -0700258 * The type is coerced to a json_bool if the passed object is not a json_bool.
Michael Clarkf0d08882007-03-13 08:26:18 +0000259 * integer and double objects will return FALSE if there value is zero
260 * or TRUE otherwise. If the passed object is a string it will return
261 * TRUE if it has a non zero length. If any other object type is passed
262 * TRUE will be returned if the object is not NULL.
263 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000264 * @param obj the json_object instance
Keith Derrick37e74672012-03-26 14:29:31 -0700265 * @returns a json_bool
Michael Clarkf0d08882007-03-13 08:26:18 +0000266 */
Keith Derrick37e74672012-03-26 14:29:31 -0700267extern json_bool json_object_get_boolean(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000268
269
270/* int type methods */
271
272/** Create a new empty json_object of type json_type_int
ehaszla252669c2010-12-07 18:15:35 +0000273 * Note that values are stored as 64-bit values internally.
274 * To ensure the full range is maintained, use json_object_new_int64 instead.
Michael Clarkf0d08882007-03-13 08:26:18 +0000275 * @param i the integer
276 * @returns a json_object of type json_type_int
277 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000278extern struct json_object* json_object_new_int(int32_t i);
279
280
ehaszla252669c2010-12-07 18:15:35 +0000281/** Create a new empty json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000282 * @param i the integer
ehaszla252669c2010-12-07 18:15:35 +0000283 * @returns a json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000284 */
285extern struct json_object* json_object_new_int64(int64_t i);
286
Michael Clarkf0d08882007-03-13 08:26:18 +0000287
288/** Get the int value of a json_object
289 *
290 * The type is coerced to a int if the passed object is not a int.
291 * double objects will return their integer conversion. Strings will be
292 * parsed as an integer. If no conversion exists then 0 is returned.
293 *
ehaszla252669c2010-12-07 18:15:35 +0000294 * Note that integers are stored internally as 64-bit values.
295 * If the value of too big or too small to fit into 32-bit, INT32_MAX or
296 * INT32_MIN are returned, respectively.
297 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000298 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000299 * @returns an int
300 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000301extern int32_t json_object_get_int(struct json_object *obj);
302
303/** Get the int value of a json_object
304 *
305 * The type is coerced to a int64 if the passed object is not a int64.
306 * double objects will return their int64 conversion. Strings will be
307 * parsed as an int64. If no conversion exists then 0 is returned.
308 *
309 * @param obj the json_object instance
310 * @returns an int64
311 */
312extern int64_t json_object_get_int64(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000313
314
315/* double type methods */
316
317/** Create a new empty json_object of type json_type_double
318 * @param d the double
319 * @returns a json_object of type json_type_double
320 */
321extern struct json_object* json_object_new_double(double d);
322
323/** Get the double value of a json_object
324 *
325 * The type is coerced to a double if the passed object is not a double.
326 * integer objects will return their dboule conversion. Strings will be
327 * parsed as a double. If no conversion exists then 0.0 is returned.
328 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000329 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000330 * @returns an double
331 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000332extern double json_object_get_double(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000333
334
335/* string type methods */
336
337/** Create a new empty json_object of type json_type_string
338 *
339 * A copy of the string is made and the memory is managed by the json_object
340 *
341 * @param s the string
342 * @returns a json_object of type json_type_string
343 */
Michael Clark68cafad2009-01-06 22:56:57 +0000344extern struct json_object* json_object_new_string(const char *s);
Michael Clarkf0d08882007-03-13 08:26:18 +0000345
Michael Clark68cafad2009-01-06 22:56:57 +0000346extern struct json_object* json_object_new_string_len(const char *s, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +0000347
348/** Get the string value of a json_object
349 *
350 * If the passed object is not of type json_type_string then the JSON
351 * representation of the object is returned.
352 *
353 * The returned string memory is managed by the json_object and will
354 * be freed when the reference count of the json_object drops to zero.
355 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000356 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000357 * @returns a string
358 */
Michael Clark68cafad2009-01-06 22:56:57 +0000359extern const char* json_object_get_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000360
Jehiah Czebotarac601b52011-01-14 17:23:06 +0000361/** Get the string length of a json_object
362 *
363 * If the passed object is not of type json_type_string then zero
364 * will be returned.
365 *
366 * @param obj the json_object instance
367 * @returns int
368 */
369extern int json_object_get_string_len(struct json_object *obj);
370
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000371#ifdef __cplusplus
372}
373#endif
374
Michael Clarkf0d08882007-03-13 08:26:18 +0000375#endif