blob: a94e15af1752bd09d81e49f7b722fa643631d8b9 [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>
Keith Derrick69175862012-04-12 11:44:13 -07006 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
Michael Clarkf0d08882007-03-13 08:26:18 +00007 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00008 * 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.
Michael Clarkf0d08882007-03-13 08:26:18 +000010 *
11 */
12
13#ifndef _json_object_h_
14#define _json_object_h_
15
Michael Clark64e36902014-04-09 13:48:21 +080016#ifdef __GNUC__
17#define THIS_FUNCTION_IS_DEPRECATED(func) func __attribute__ ((deprecated))
18#elif defined(_MSC_VER)
19#define THIS_FUNCTION_IS_DEPRECATED(func) __declspec(deprecated) func
20#else
21#define THIS_FUNCTION_IS_DEPRECATED(func) func
22#endif
23
Karsten Tausche88aaf762022-09-15 13:51:51 +020024#ifdef __GNUC__
25# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
26#else
27# define UNUSED(x) UNUSED_ ## x
28#endif
29
Eric Haszlakiewiczb21b1372012-02-15 20:44:54 -060030#include "json_inttypes.h"
31
Michael Clarkaaec1ef2009-02-25 02:31:32 +000032#ifdef __cplusplus
33extern "C" {
34#endif
35
Michael Clarke6548a32009-01-10 13:58:06 +000036#define JSON_OBJECT_DEF_HASH_ENTRIES 16
Michael Clarkf0d08882007-03-13 08:26:18 +000037
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050038/**
39 * A flag for the json_object_to_json_string_ext() and
40 * json_object_to_file_ext() functions which causes the output
41 * to have no extra whitespace or formatting applied.
42 */
43#define JSON_C_TO_STRING_PLAIN 0
44/**
45 * A flag for the json_object_to_json_string_ext() and
46 * json_object_to_file_ext() functions which causes the output to have
47 * minimal whitespace inserted to make things slightly more readable.
48 */
49#define JSON_C_TO_STRING_SPACED (1<<0)
50/**
51 * A flag for the json_object_to_json_string_ext() and
52 * json_object_to_file_ext() functions which causes
53 * the output to be formatted.
54 *
55 * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/
56 * for an example of the format.
57 */
58#define JSON_C_TO_STRING_PRETTY (1<<1)
Remi Collet32d149c2012-12-13 11:46:04 +010059/**
60 * A flag to drop trailing zero for float values
61 */
62#define JSON_C_TO_STRING_NOZERO (1<<2)
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050063
Michael Clarkf0d08882007-03-13 08:26:18 +000064#undef FALSE
Keith Derrick37e74672012-03-26 14:29:31 -070065#define FALSE ((json_bool)0)
Michael Clarkf0d08882007-03-13 08:26:18 +000066
67#undef TRUE
Keith Derrick37e74672012-03-26 14:29:31 -070068#define TRUE ((json_bool)1)
Michael Clarkf0d08882007-03-13 08:26:18 +000069
Michael Clark68cafad2009-01-06 22:56:57 +000070extern const char *json_number_chars;
71extern const char *json_hex_chars;
Michael Clarkf0d08882007-03-13 08:26:18 +000072
Jehiah Czebotar43d2f412011-05-25 04:49:20 +000073/* CAW: added for ANSI C iteration correctness */
74struct json_object_iter
75{
76 char *key;
77 struct json_object *val;
78 struct lh_entry *entry;
79};
80
Michael Clarkf0d08882007-03-13 08:26:18 +000081/* forward structure definitions */
82
Keith Derrick37e74672012-03-26 14:29:31 -070083typedef int json_bool;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000084typedef struct printbuf printbuf;
85typedef struct lh_table lh_table;
86typedef struct array_list array_list;
87typedef struct json_object json_object;
88typedef struct json_object_iter json_object_iter;
89typedef struct json_tokener json_tokener;
Michael Clarkf0d08882007-03-13 08:26:18 +000090
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -050091/**
92 * Type of custom user delete functions. See json_object_set_serializer.
93 */
94typedef void (json_object_delete_fn)(struct json_object *jso, void *userdata);
95
96/**
97 * Type of a custom serialization function. See json_object_set_serializer.
98 */
99typedef int (json_object_to_json_string_fn)(struct json_object *jso,
100 struct printbuf *pb,
101 int level,
102 int flags);
103
Michael Clarkf0d08882007-03-13 08:26:18 +0000104/* supported object types */
105
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000106typedef enum json_type {
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +0000107 /* If you change this, be sure to update json_type_to_name() too */
Michael Clarkf0d08882007-03-13 08:26:18 +0000108 json_type_null,
109 json_type_boolean,
110 json_type_double,
111 json_type_int,
112 json_type_object,
113 json_type_array,
Michael J. Chinn048dcf22014-08-10 00:46:25 -0400114 json_type_string
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000115} json_type;
Michael Clarkf0d08882007-03-13 08:26:18 +0000116
117/* reference counting functions */
118
119/**
Keith Derrickca519fb2012-04-05 19:33:09 -0700120 * Increment the reference count of json_object, thereby grabbing shared
121 * ownership of obj.
122 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000123 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000124 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000125extern struct json_object* json_object_get(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000126
127/**
Keith Derrickca519fb2012-04-05 19:33:09 -0700128 * Decrement the reference count of json_object and free if it reaches zero.
129 * You must have ownership of obj prior to doing this or you will cause an
130 * imbalance in the reference count.
131 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000132 * @param obj the json_object instance
Eric Haszlakiewicz5f4739e2012-10-18 17:10:09 -0500133 * @returns 1 if the object was freed.
Michael Clarkf0d08882007-03-13 08:26:18 +0000134 */
Eric Haszlakiewicz5f4739e2012-10-18 17:10:09 -0500135int json_object_put(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000136
137/**
138 * Check if the json_object is of a given type
Michael Clarkf6a6e482007-03-13 08:26:23 +0000139 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000140 * @param type one of:
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -0500141 json_type_null (i.e. obj == NULL),
Michael Clarkf0d08882007-03-13 08:26:18 +0000142 json_type_boolean,
143 json_type_double,
144 json_type_int,
145 json_type_object,
146 json_type_array,
Michael J. Chinn048dcf22014-08-10 00:46:25 -0400147 json_type_string
Michael Clarkf0d08882007-03-13 08:26:18 +0000148 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000149extern int json_object_is_type(struct json_object *obj, enum json_type type);
Michael Clarkf0d08882007-03-13 08:26:18 +0000150
151/**
Eric Haszlakiewicz6ff08172012-03-31 22:47:47 -0500152 * Get the type of the json_object. See also json_type_to_name() to turn this
153 * into a string suitable, for instance, for logging.
154 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000155 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000156 * @returns type being one of:
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -0500157 json_type_null (i.e. obj == NULL),
Michael Clarkf0d08882007-03-13 08:26:18 +0000158 json_type_boolean,
159 json_type_double,
160 json_type_int,
161 json_type_object,
162 json_type_array,
Michael J. Chinn048dcf22014-08-10 00:46:25 -0400163 json_type_string
Michael Clarkf0d08882007-03-13 08:26:18 +0000164 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000165extern enum json_type json_object_get_type(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000166
167
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500168/** Stringify object to json format.
169 * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
Alexander Dahl37f5d862014-08-18 10:28:38 +0200170 * The pointer you get is an internal of your json object. You don't
171 * have to free it, later use of json_object_put() should be sufficient.
172 * If you can not ensure there's no concurrent access to *obj use
173 * strdup().
Michael Clarkf6a6e482007-03-13 08:26:23 +0000174 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000175 * @returns a string in JSON format
176 */
Michael Clark68cafad2009-01-06 22:56:57 +0000177extern const char* json_object_to_json_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000178
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500179/** Stringify object to json format
Alexander Dahl37f5d862014-08-18 10:28:38 +0200180 * @see json_object_to_json_string() for details on how to free string.
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500181 * @param obj the json_object instance
182 * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
183 * @returns a string in JSON format
184 */
185extern const char* json_object_to_json_string_ext(struct json_object *obj, int
186flags);
187
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -0500188/**
189 * Set a custom serialization function to be used when this particular object
190 * is converted to a string by json_object_to_json_string.
191 *
192 * If a custom serializer is already set on this object, any existing
193 * user_delete function is called before the new one is set.
194 *
195 * If to_string_func is NULL, the other parameters are ignored
196 * and the default behaviour is reset.
197 *
198 * The userdata parameter is optional and may be passed as NULL. If provided,
199 * it is passed to to_string_func as-is. This parameter may be NULL even
200 * if user_delete is non-NULL.
201 *
202 * The user_delete parameter is optional and may be passed as NULL, even if
203 * the userdata parameter is non-NULL. It will be called just before the
204 * json_object is deleted, after it's reference count goes to zero
205 * (see json_object_put()).
206 * If this is not provided, it is up to the caller to free the userdata at
207 * an appropriate time. (i.e. after the json_object is deleted)
208 *
209 * @param jso the object to customize
210 * @param to_string_func the custom serialization function
211 * @param userdata an optional opaque cookie
212 * @param user_delete an optional function from freeing userdata
213 */
Even Rouault6c4bb382013-08-11 01:18:17 +0200214extern void json_object_set_serializer(json_object *jso,
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -0500215 json_object_to_json_string_fn to_string_func,
216 void *userdata,
217 json_object_delete_fn *user_delete);
218
Eric Haszlakiewicz51993c22013-09-11 20:27:39 -0500219/**
220 * Simply call free on the userdata pointer.
221 * Can be used with json_object_set_serializer().
222 *
223 * @param jso unused
224 * @param userdata the pointer that is passed to free().
225 */
226json_object_delete_fn json_object_free_userdata;
227
228/**
229 * Copy the jso->_userdata string over to pb as-is.
230 * Can be used with json_object_set_serializer().
231 *
232 * @param jso The object whose _userdata is used.
233 * @param pb The destination buffer.
234 * @param level Ignored.
235 * @param flags Ignored.
236 */
237json_object_to_json_string_fn json_object_userdata_to_json_string;
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -0500238
Michael Clarkf0d08882007-03-13 08:26:18 +0000239
240/* object type methods */
241
Keith Derrickca519fb2012-04-05 19:33:09 -0700242/** Create a new empty object with a reference count of 1. The caller of
243 * this object initially has sole ownership. Remember, when using
244 * json_object_object_add or json_object_array_put_idx, ownership will
245 * transfer to the object/array. Call json_object_get if you want to maintain
246 * shared ownership or also add this object as a child of multiple objects or
247 * arrays. Any ownerships you acquired but did not transfer must be released
248 * through json_object_put.
249 *
Michael Clarkf0d08882007-03-13 08:26:18 +0000250 * @returns a json_object of type json_type_object
251 */
Michael Clark14862b12007-12-07 02:50:42 +0000252extern struct json_object* json_object_new_object(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000253
254/** Get the hashtable of a json_object of type json_type_object
Michael Clarkf6a6e482007-03-13 08:26:23 +0000255 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000256 * @returns a linkhash
257 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000258extern struct lh_table* json_object_get_object(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000259
Eric Haszlakiewicz92d289f2013-02-09 16:18:05 -0600260/** Get the size of an object in terms of the number of fields it has.
261 * @param obj the json_object whose length to return
262 */
Greg Hazelcca74c62013-01-11 01:36:55 -0800263extern int json_object_object_length(struct json_object* obj);
264
Michael Clarkf0d08882007-03-13 08:26:18 +0000265/** Add an object field to a json_object of type json_type_object
266 *
267 * The reference count will *not* be incremented. This is to make adding
268 * fields to objects in code more compact. If you want to retain a reference
Keith Derrickca519fb2012-04-05 19:33:09 -0700269 * to an added object, independent of the lifetime of obj, you must wrap the
270 * passed object with json_object_get.
271 *
272 * Upon calling this, the ownership of val transfers to obj. Thus you must
273 * make sure that you do in fact have ownership over this object. For instance,
274 * json_object_new_object will give you ownership until you transfer it,
275 * whereas json_object_object_get does not.
Michael Clarkf0d08882007-03-13 08:26:18 +0000276 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000277 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000278 * @param key the object field name (a private copy will be duplicated)
279 * @param val a json_object or NULL member to associate with the given field
280 */
Michael Clark68cafad2009-01-06 22:56:57 +0000281extern void json_object_object_add(struct json_object* obj, const char *key,
Michael Clarkf0d08882007-03-13 08:26:18 +0000282 struct json_object *val);
283
284/** Get the json_object associate with a given object field
Keith Derrickca519fb2012-04-05 19:33:09 -0700285 *
286 * *No* reference counts will be changed. There is no need to manually adjust
287 * reference counts through the json_object_put/json_object_get methods unless
288 * you need to have the child (value) reference maintain a different lifetime
289 * than the owning parent (obj). Ownership of the returned value is retained
290 * by obj (do not do json_object_put unless you have done a json_object_get).
291 * If you delete the value from obj (json_object_object_del) and wish to access
292 * the returned reference afterwards, make sure you have first gotten shared
293 * ownership through json_object_get (& don't forget to do a json_object_put
294 * or transfer ownership to prevent a memory leak).
295 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000296 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000297 * @param key the object field name
298 * @returns the json_object associated with the given field name
Keith Derrick69175862012-04-12 11:44:13 -0700299 * @deprecated Please use json_object_object_get_ex
Michael Clarkf0d08882007-03-13 08:26:18 +0000300 */
Michael Clark64e36902014-04-09 13:48:21 +0800301THIS_FUNCTION_IS_DEPRECATED(extern struct json_object* json_object_object_get(struct json_object* obj,
302 const char *key));
Michael Clarkf0d08882007-03-13 08:26:18 +0000303
Keith Derrick69175862012-04-12 11:44:13 -0700304/** Get the json_object associated with a given object field.
305 *
306 * This returns true if the key is found, false in all other cases (including
307 * if obj isn't a json_type_object).
308 *
309 * *No* reference counts will be changed. There is no need to manually adjust
310 * reference counts through the json_object_put/json_object_get methods unless
311 * you need to have the child (value) reference maintain a different lifetime
312 * than the owning parent (obj). Ownership of value is retained by obj.
313 *
314 * @param obj the json_object instance
315 * @param key the object field name
316 * @param value a pointer where to store a reference to the json_object
317 * associated with the given field name.
318 *
319 * It is safe to pass a NULL value.
320 * @returns whether or not the key exists
321 */
322extern json_bool json_object_object_get_ex(struct json_object* obj,
323 const char *key,
324 struct json_object **value);
325
Michael Clarkf0d08882007-03-13 08:26:18 +0000326/** Delete the given json_object field
327 *
Keith Derrickca519fb2012-04-05 19:33:09 -0700328 * The reference count will be decremented for the deleted object. If there
329 * are no more owners of the value represented by this key, then the value is
330 * freed. Otherwise, the reference to the value will remain in memory.
Michael Clarkf0d08882007-03-13 08:26:18 +0000331 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000332 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000333 * @param key the object field name
334 */
Michael Clark68cafad2009-01-06 22:56:57 +0000335extern void json_object_object_del(struct json_object* obj, const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000336
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500337/**
338 * Iterate through all keys and values of an object.
339 *
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500340 * Adding keys to the object while iterating is NOT allowed.
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500341 *
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500342 * Deleting an existing key, or replacing an existing key with a
343 * new value IS allowed.
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500344 *
Michael Clark4504df72007-03-13 08:26:20 +0000345 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000346 * @param key the local name for the char* key variable defined in the body
Keith Derrickca519fb2012-04-05 19:33:09 -0700347 * @param val the local name for the json_object* object variable defined in
348 * the body
Michael Clarkf0d08882007-03-13 08:26:18 +0000349 */
Greg Hazel88bf1c92013-03-19 16:26:12 -0700350#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L
Michael Clarkf0d08882007-03-13 08:26:18 +0000351
Michael Clark4504df72007-03-13 08:26:20 +0000352# define json_object_object_foreach(obj,key,val) \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500353 char *key; \
Eric Haszlakiewicz9b64c052013-02-21 12:32:29 -0600354 struct json_object *val __attribute__((__unused__)); \
Alexander Klauerbeb12d42012-12-19 10:31:39 +0100355 for(struct lh_entry *entry ## key = json_object_get_object(obj)->head, *entry_next ## key = NULL; \
356 ({ if(entry ## key) { \
357 key = (char*)entry ## key->k; \
358 val = (struct json_object*)entry ## key->v; \
359 entry_next ## key = entry ## key->next; \
360 } ; entry ## key; }); \
361 entry ## key = entry_next ## key )
Michael Clark4504df72007-03-13 08:26:20 +0000362
363#else /* ANSI C or MSC */
364
365# define json_object_object_foreach(obj,key,val) \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500366 char *key;\
367 struct json_object *val; \
Alexander Klauerbeb12d42012-12-19 10:31:39 +0100368 struct lh_entry *entry ## key; \
369 struct lh_entry *entry_next ## key = NULL; \
370 for(entry ## key = json_object_get_object(obj)->head; \
371 (entry ## key ? ( \
372 key = (char*)entry ## key->k, \
373 val = (struct json_object*)entry ## key->v, \
374 entry_next ## key = entry ## key->next, \
375 entry ## key) : 0); \
376 entry ## key = entry_next ## key)
Michael Clark4504df72007-03-13 08:26:20 +0000377
Greg Hazel88bf1c92013-03-19 16:26:12 -0700378#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) && __STDC_VERSION__ >= 199901L */
Michael Clark4504df72007-03-13 08:26:20 +0000379
380/** Iterate through all keys and values of an object (ANSI C Safe)
381 * @param obj the json_object instance
382 * @param iter the object iterator
383 */
384#define json_object_object_foreachC(obj,iter) \
385 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 +0000386
387/* Array type methods */
388
389/** Create a new empty json_object of type json_type_array
390 * @returns a json_object of type json_type_array
391 */
Michael Clark14862b12007-12-07 02:50:42 +0000392extern struct json_object* json_object_new_array(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000393
394/** Get the arraylist of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000395 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000396 * @returns an arraylist
397 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000398extern struct array_list* json_object_get_array(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000399
400/** Get the length of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000401 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000402 * @returns an int
403 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000404extern int json_object_array_length(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000405
Frederik Deweerdtc43871c2011-10-07 21:07:18 +0200406/** Sorts the elements of jso of type json_type_array
407*
408* Pointers to the json_object pointers will be passed as the two arguments
409* to @sort_fn
410*
411* @param obj the json_object instance
412* @param sort_fn a sorting function
413*/
414extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
415
Michael Clarkf0d08882007-03-13 08:26:18 +0000416/** Add an element to the end of a json_object of type json_type_array
417 *
418 * The reference count will *not* be incremented. This is to make adding
419 * fields to objects in code more compact. If you want to retain a reference
420 * to an added object you must wrap the passed object with json_object_get
421 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000422 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000423 * @param val the json_object to be added
424 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000425extern int json_object_array_add(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000426 struct json_object *val);
427
428/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
429 *
430 * The reference count will *not* be incremented. This is to make adding
431 * fields to objects in code more compact. If you want to retain a reference
432 * to an added object you must wrap the passed object with json_object_get
433 *
434 * The reference count of a replaced object will be decremented.
435 *
436 * The array size will be automatically be expanded to the size of the
437 * index if the index is larger than the current size.
438 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000439 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000440 * @param idx the index to insert the element at
441 * @param val the json_object to be added
442 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000443extern int json_object_array_put_idx(struct json_object *obj, int idx,
Michael Clarkf0d08882007-03-13 08:26:18 +0000444 struct json_object *val);
445
446/** Get the element at specificed index of the array (a json_object of type json_type_array)
Michael Clarkf6a6e482007-03-13 08:26:23 +0000447 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000448 * @param idx the index to get the element at
449 * @returns the json_object at the specified index (or NULL)
450 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000451extern struct json_object* json_object_array_get_idx(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000452 int idx);
453
Keith Derrick37e74672012-03-26 14:29:31 -0700454/* json_bool type methods */
Michael Clarkf0d08882007-03-13 08:26:18 +0000455
456/** Create a new empty json_object of type json_type_boolean
Keith Derrick37e74672012-03-26 14:29:31 -0700457 * @param b a json_bool TRUE or FALSE (0 or 1)
Michael Clarkf0d08882007-03-13 08:26:18 +0000458 * @returns a json_object of type json_type_boolean
459 */
Keith Derrick37e74672012-03-26 14:29:31 -0700460extern struct json_object* json_object_new_boolean(json_bool b);
Michael Clarkf0d08882007-03-13 08:26:18 +0000461
Keith Derrick37e74672012-03-26 14:29:31 -0700462/** Get the json_bool value of a json_object
Michael Clarkf0d08882007-03-13 08:26:18 +0000463 *
Keith Derrick37e74672012-03-26 14:29:31 -0700464 * The type is coerced to a json_bool if the passed object is not a json_bool.
Michael Clarkf0d08882007-03-13 08:26:18 +0000465 * integer and double objects will return FALSE if there value is zero
466 * or TRUE otherwise. If the passed object is a string it will return
467 * TRUE if it has a non zero length. If any other object type is passed
468 * TRUE will be returned if the object is not NULL.
469 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000470 * @param obj the json_object instance
Keith Derrick37e74672012-03-26 14:29:31 -0700471 * @returns a json_bool
Michael Clarkf0d08882007-03-13 08:26:18 +0000472 */
Keith Derrick37e74672012-03-26 14:29:31 -0700473extern json_bool json_object_get_boolean(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000474
475
476/* int type methods */
477
478/** Create a new empty json_object of type json_type_int
ehaszla252669c2010-12-07 18:15:35 +0000479 * Note that values are stored as 64-bit values internally.
480 * To ensure the full range is maintained, use json_object_new_int64 instead.
Michael Clarkf0d08882007-03-13 08:26:18 +0000481 * @param i the integer
482 * @returns a json_object of type json_type_int
483 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000484extern struct json_object* json_object_new_int(int32_t i);
485
486
ehaszla252669c2010-12-07 18:15:35 +0000487/** Create a new empty json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000488 * @param i the integer
ehaszla252669c2010-12-07 18:15:35 +0000489 * @returns a json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000490 */
491extern struct json_object* json_object_new_int64(int64_t i);
492
Michael Clarkf0d08882007-03-13 08:26:18 +0000493
494/** Get the int value of a json_object
495 *
496 * The type is coerced to a int if the passed object is not a int.
497 * double objects will return their integer conversion. Strings will be
Keith Derrickca519fb2012-04-05 19:33:09 -0700498 * parsed as an integer. If no conversion exists then 0 is returned
499 * and errno is set to EINVAL. null is equivalent to 0 (no error values set)
Michael Clarkf0d08882007-03-13 08:26:18 +0000500 *
ehaszla252669c2010-12-07 18:15:35 +0000501 * Note that integers are stored internally as 64-bit values.
502 * If the value of too big or too small to fit into 32-bit, INT32_MAX or
503 * INT32_MIN are returned, respectively.
504 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000505 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000506 * @returns an int
507 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000508extern int32_t json_object_get_int(struct json_object *obj);
509
510/** Get the int value of a json_object
511 *
512 * The type is coerced to a int64 if the passed object is not a int64.
513 * double objects will return their int64 conversion. Strings will be
514 * parsed as an int64. If no conversion exists then 0 is returned.
515 *
Keith Derrickca519fb2012-04-05 19:33:09 -0700516 * NOTE: Set errno to 0 directly before a call to this function to determine
517 * whether or not conversion was successful (it does not clear the value for
518 * you).
519 *
Michael Clarkc4dceae2010-10-06 16:39:20 +0000520 * @param obj the json_object instance
521 * @returns an int64
522 */
523extern int64_t json_object_get_int64(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000524
525
526/* double type methods */
527
528/** Create a new empty json_object of type json_type_double
529 * @param d the double
530 * @returns a json_object of type json_type_double
531 */
532extern struct json_object* json_object_new_double(double d);
533
Eric Haszlakiewicz51993c22013-09-11 20:27:39 -0500534/**
535 * Create a new json_object of type json_type_double, using
536 * the exact serialized representation of the value.
537 *
538 * This allows for numbers that would otherwise get displayed
539 * inefficiently (e.g. 12.3 => "12.300000000000001") to be
540 * serialized with the more convenient form.
541 *
542 * Note: this is used by json_tokener_parse_ex() to allow for
543 * an exact re-serialization of a parsed object.
544 *
545 * An equivalent sequence of calls is:
546 * @code
547 * jso = json_object_new_double(d);
548 * json_object_set_serializer(d, json_object_userdata_to_json_string,
549 * strdup(ds), json_object_free_userdata)
550 * @endcode
551 *
552 * @param d the numeric value of the double.
553 * @param ds the string representation of the double. This will be copied.
554 */
555extern struct json_object* json_object_new_double_s(double d, const char *ds);
556
Keith Derrickca519fb2012-04-05 19:33:09 -0700557/** Get the double floating point value of a json_object
Michael Clarkf0d08882007-03-13 08:26:18 +0000558 *
559 * The type is coerced to a double if the passed object is not a double.
Keith Derrickca519fb2012-04-05 19:33:09 -0700560 * integer objects will return their double conversion. Strings will be
561 * parsed as a double. If no conversion exists then 0.0 is returned and
562 * errno is set to EINVAL. null is equivalent to 0 (no error values set)
563 *
564 * If the value is too big to fit in a double, then the value is set to
565 * the closest infinity with errno set to ERANGE. If strings cannot be
566 * converted to their double value, then EINVAL is set & NaN is returned.
567 *
568 * Arrays of length 0 are interpreted as 0 (with no error flags set).
569 * Arrays of length 1 are effectively cast to the equivalent object and
570 * converted using the above rules. All other arrays set the error to
571 * EINVAL & return NaN.
572 *
573 * NOTE: Set errno to 0 directly before a call to this function to
574 * determine whether or not conversion was successful (it does not clear
575 * the value for you).
Michael Clarkf0d08882007-03-13 08:26:18 +0000576 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000577 * @param obj the json_object instance
Keith Derrickca519fb2012-04-05 19:33:09 -0700578 * @returns a double floating point number
Michael Clarkf0d08882007-03-13 08:26:18 +0000579 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000580extern double json_object_get_double(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000581
582
583/* string type methods */
584
585/** Create a new empty json_object of type json_type_string
586 *
587 * A copy of the string is made and the memory is managed by the json_object
588 *
589 * @param s the string
590 * @returns a json_object of type json_type_string
591 */
Michael Clark68cafad2009-01-06 22:56:57 +0000592extern struct json_object* json_object_new_string(const char *s);
Michael Clarkf0d08882007-03-13 08:26:18 +0000593
Michael Clark68cafad2009-01-06 22:56:57 +0000594extern struct json_object* json_object_new_string_len(const char *s, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +0000595
596/** Get the string value of a json_object
597 *
598 * If the passed object is not of type json_type_string then the JSON
599 * representation of the object is returned.
600 *
601 * The returned string memory is managed by the json_object and will
602 * be freed when the reference count of the json_object drops to zero.
603 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000604 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000605 * @returns a string
606 */
Michael Clark68cafad2009-01-06 22:56:57 +0000607extern const char* json_object_get_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000608
Jehiah Czebotarac601b52011-01-14 17:23:06 +0000609/** Get the string length of a json_object
610 *
611 * If the passed object is not of type json_type_string then zero
612 * will be returned.
613 *
614 * @param obj the json_object instance
615 * @returns int
616 */
617extern int json_object_get_string_len(struct json_object *obj);
618
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000619#ifdef __cplusplus
620}
621#endif
622
Michael Clarkf0d08882007-03-13 08:26:18 +0000623#endif