blob: 262111262e6d33cd0ad5fa0100e0c5b931f7c436 [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
Eric Haszlakiewiczb21b1372012-02-15 20:44:54 -060016#include "json_inttypes.h"
17
Michael Clarkaaec1ef2009-02-25 02:31:32 +000018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michael Clarke6548a32009-01-10 13:58:06 +000022#define JSON_OBJECT_DEF_HASH_ENTRIES 16
Michael Clarkf0d08882007-03-13 08:26:18 +000023
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -050024/**
25 * A flag for the json_object_to_json_string_ext() and
26 * json_object_to_file_ext() functions which causes the output
27 * to have no extra whitespace or formatting applied.
28 */
29#define JSON_C_TO_STRING_PLAIN 0
30/**
31 * A flag for the json_object_to_json_string_ext() and
32 * json_object_to_file_ext() functions which causes the output to have
33 * minimal whitespace inserted to make things slightly more readable.
34 */
35#define JSON_C_TO_STRING_SPACED (1<<0)
36/**
37 * A flag for the json_object_to_json_string_ext() and
38 * json_object_to_file_ext() functions which causes
39 * the output to be formatted.
40 *
41 * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/
42 * for an example of the format.
43 */
44#define JSON_C_TO_STRING_PRETTY (1<<1)
45
Michael Clarkf0d08882007-03-13 08:26:18 +000046#undef FALSE
Keith Derrick37e74672012-03-26 14:29:31 -070047#define FALSE ((json_bool)0)
Michael Clarkf0d08882007-03-13 08:26:18 +000048
49#undef TRUE
Keith Derrick37e74672012-03-26 14:29:31 -070050#define TRUE ((json_bool)1)
Michael Clarkf0d08882007-03-13 08:26:18 +000051
Michael Clark68cafad2009-01-06 22:56:57 +000052extern const char *json_number_chars;
53extern const char *json_hex_chars;
Michael Clarkf0d08882007-03-13 08:26:18 +000054
Jehiah Czebotar43d2f412011-05-25 04:49:20 +000055/* CAW: added for ANSI C iteration correctness */
56struct json_object_iter
57{
58 char *key;
59 struct json_object *val;
60 struct lh_entry *entry;
61};
62
Michael Clarkf0d08882007-03-13 08:26:18 +000063/* forward structure definitions */
64
Keith Derrick37e74672012-03-26 14:29:31 -070065typedef int json_bool;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000066typedef struct printbuf printbuf;
67typedef struct lh_table lh_table;
68typedef struct array_list array_list;
69typedef struct json_object json_object;
70typedef struct json_object_iter json_object_iter;
71typedef struct json_tokener json_tokener;
Michael Clarkf0d08882007-03-13 08:26:18 +000072
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -050073/**
74 * Type of custom user delete functions. See json_object_set_serializer.
75 */
76typedef void (json_object_delete_fn)(struct json_object *jso, void *userdata);
77
78/**
79 * Type of a custom serialization function. See json_object_set_serializer.
80 */
81typedef int (json_object_to_json_string_fn)(struct json_object *jso,
82 struct printbuf *pb,
83 int level,
84 int flags);
85
Michael Clarkf0d08882007-03-13 08:26:18 +000086/* supported object types */
87
Michael Clarkaaec1ef2009-02-25 02:31:32 +000088typedef enum json_type {
Eric Haszlakiewicz886c4fb2011-05-03 20:40:49 +000089 /* If you change this, be sure to update json_type_to_name() too */
Michael Clarkf0d08882007-03-13 08:26:18 +000090 json_type_null,
91 json_type_boolean,
92 json_type_double,
93 json_type_int,
94 json_type_object,
95 json_type_array,
Michael Clarkc4dceae2010-10-06 16:39:20 +000096 json_type_string,
Michael Clarkaaec1ef2009-02-25 02:31:32 +000097} json_type;
Michael Clarkf0d08882007-03-13 08:26:18 +000098
99/* reference counting functions */
100
101/**
Keith Derrickca519fb2012-04-05 19:33:09 -0700102 * Increment the reference count of json_object, thereby grabbing shared
103 * ownership of obj.
104 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000105 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000106 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000107extern struct json_object* json_object_get(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000108
109/**
Keith Derrickca519fb2012-04-05 19:33:09 -0700110 * Decrement the reference count of json_object and free if it reaches zero.
111 * You must have ownership of obj prior to doing this or you will cause an
112 * imbalance in the reference count.
113 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000114 * @param obj the json_object instance
Eric Haszlakiewicz5f4739e2012-10-18 17:10:09 -0500115 * @returns 1 if the object was freed.
Michael Clarkf0d08882007-03-13 08:26:18 +0000116 */
Eric Haszlakiewicz5f4739e2012-10-18 17:10:09 -0500117int json_object_put(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000118
119/**
120 * Check if the json_object is of a given type
Michael Clarkf6a6e482007-03-13 08:26:23 +0000121 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000122 * @param type one of:
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -0500123 json_type_null (i.e. obj == NULL),
Michael Clarkf0d08882007-03-13 08:26:18 +0000124 json_type_boolean,
125 json_type_double,
126 json_type_int,
127 json_type_object,
128 json_type_array,
129 json_type_string,
130 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000131extern int json_object_is_type(struct json_object *obj, enum json_type type);
Michael Clarkf0d08882007-03-13 08:26:18 +0000132
133/**
Eric Haszlakiewicz6ff08172012-03-31 22:47:47 -0500134 * Get the type of the json_object. See also json_type_to_name() to turn this
135 * into a string suitable, for instance, for logging.
136 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000137 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000138 * @returns type being one of:
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -0500139 json_type_null (i.e. obj == NULL),
Michael Clarkf0d08882007-03-13 08:26:18 +0000140 json_type_boolean,
141 json_type_double,
142 json_type_int,
143 json_type_object,
144 json_type_array,
145 json_type_string,
146 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000147extern enum json_type json_object_get_type(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000148
149
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500150/** Stringify object to json format.
151 * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
Michael Clarkf6a6e482007-03-13 08:26:23 +0000152 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000153 * @returns a string in JSON format
154 */
Michael Clark68cafad2009-01-06 22:56:57 +0000155extern const char* json_object_to_json_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000156
Eric Haszlakiewicz3fcffe12012-04-28 13:26:09 -0500157/** Stringify object to json format
158 * @param obj the json_object instance
159 * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants
160 * @returns a string in JSON format
161 */
162extern const char* json_object_to_json_string_ext(struct json_object *obj, int
163flags);
164
Eric Haszlakiewicz38f421a2012-09-02 15:21:56 -0500165/**
166 * Set a custom serialization function to be used when this particular object
167 * is converted to a string by json_object_to_json_string.
168 *
169 * If a custom serializer is already set on this object, any existing
170 * user_delete function is called before the new one is set.
171 *
172 * If to_string_func is NULL, the other parameters are ignored
173 * and the default behaviour is reset.
174 *
175 * The userdata parameter is optional and may be passed as NULL. If provided,
176 * it is passed to to_string_func as-is. This parameter may be NULL even
177 * if user_delete is non-NULL.
178 *
179 * The user_delete parameter is optional and may be passed as NULL, even if
180 * the userdata parameter is non-NULL. It will be called just before the
181 * json_object is deleted, after it's reference count goes to zero
182 * (see json_object_put()).
183 * If this is not provided, it is up to the caller to free the userdata at
184 * an appropriate time. (i.e. after the json_object is deleted)
185 *
186 * @param jso the object to customize
187 * @param to_string_func the custom serialization function
188 * @param userdata an optional opaque cookie
189 * @param user_delete an optional function from freeing userdata
190 */
191void json_object_set_serializer(json_object *jso,
192 json_object_to_json_string_fn to_string_func,
193 void *userdata,
194 json_object_delete_fn *user_delete);
195
196
Michael Clarkf0d08882007-03-13 08:26:18 +0000197
198/* object type methods */
199
Keith Derrickca519fb2012-04-05 19:33:09 -0700200/** Create a new empty object with a reference count of 1. The caller of
201 * this object initially has sole ownership. Remember, when using
202 * json_object_object_add or json_object_array_put_idx, ownership will
203 * transfer to the object/array. Call json_object_get if you want to maintain
204 * shared ownership or also add this object as a child of multiple objects or
205 * arrays. Any ownerships you acquired but did not transfer must be released
206 * through json_object_put.
207 *
Michael Clarkf0d08882007-03-13 08:26:18 +0000208 * @returns a json_object of type json_type_object
209 */
Michael Clark14862b12007-12-07 02:50:42 +0000210extern struct json_object* json_object_new_object(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000211
212/** Get the hashtable of a json_object of type json_type_object
Michael Clarkf6a6e482007-03-13 08:26:23 +0000213 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000214 * @returns a linkhash
215 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000216extern struct lh_table* json_object_get_object(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000217
218/** Add an object field to a json_object of type json_type_object
219 *
220 * The reference count will *not* be incremented. This is to make adding
221 * fields to objects in code more compact. If you want to retain a reference
Keith Derrickca519fb2012-04-05 19:33:09 -0700222 * to an added object, independent of the lifetime of obj, you must wrap the
223 * passed object with json_object_get.
224 *
225 * Upon calling this, the ownership of val transfers to obj. Thus you must
226 * make sure that you do in fact have ownership over this object. For instance,
227 * json_object_new_object will give you ownership until you transfer it,
228 * whereas json_object_object_get does not.
Michael Clarkf0d08882007-03-13 08:26:18 +0000229 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000230 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000231 * @param key the object field name (a private copy will be duplicated)
232 * @param val a json_object or NULL member to associate with the given field
233 */
Michael Clark68cafad2009-01-06 22:56:57 +0000234extern void json_object_object_add(struct json_object* obj, const char *key,
Michael Clarkf0d08882007-03-13 08:26:18 +0000235 struct json_object *val);
236
237/** Get the json_object associate with a given object field
Keith Derrickca519fb2012-04-05 19:33:09 -0700238 *
239 * *No* reference counts will be changed. There is no need to manually adjust
240 * reference counts through the json_object_put/json_object_get methods unless
241 * you need to have the child (value) reference maintain a different lifetime
242 * than the owning parent (obj). Ownership of the returned value is retained
243 * by obj (do not do json_object_put unless you have done a json_object_get).
244 * If you delete the value from obj (json_object_object_del) and wish to access
245 * the returned reference afterwards, make sure you have first gotten shared
246 * ownership through json_object_get (& don't forget to do a json_object_put
247 * or transfer ownership to prevent a memory leak).
248 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000249 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000250 * @param key the object field name
251 * @returns the json_object associated with the given field name
Keith Derrick69175862012-04-12 11:44:13 -0700252 * @deprecated Please use json_object_object_get_ex
Michael Clarkf0d08882007-03-13 08:26:18 +0000253 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000254extern struct json_object* json_object_object_get(struct json_object* obj,
Michael Clark68cafad2009-01-06 22:56:57 +0000255 const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000256
Keith Derrick69175862012-04-12 11:44:13 -0700257/** Get the json_object associated with a given object field.
258 *
259 * This returns true if the key is found, false in all other cases (including
260 * if obj isn't a json_type_object).
261 *
262 * *No* reference counts will be changed. There is no need to manually adjust
263 * reference counts through the json_object_put/json_object_get methods unless
264 * you need to have the child (value) reference maintain a different lifetime
265 * than the owning parent (obj). Ownership of value is retained by obj.
266 *
267 * @param obj the json_object instance
268 * @param key the object field name
269 * @param value a pointer where to store a reference to the json_object
270 * associated with the given field name.
271 *
272 * It is safe to pass a NULL value.
273 * @returns whether or not the key exists
274 */
275extern json_bool json_object_object_get_ex(struct json_object* obj,
276 const char *key,
277 struct json_object **value);
278
Michael Clarkf0d08882007-03-13 08:26:18 +0000279/** Delete the given json_object field
280 *
Keith Derrickca519fb2012-04-05 19:33:09 -0700281 * The reference count will be decremented for the deleted object. If there
282 * are no more owners of the value represented by this key, then the value is
283 * freed. Otherwise, the reference to the value will remain in memory.
Michael Clarkf0d08882007-03-13 08:26:18 +0000284 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000285 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000286 * @param key the object field name
287 */
Michael Clark68cafad2009-01-06 22:56:57 +0000288extern void json_object_object_del(struct json_object* obj, const char *key);
Michael Clarkf0d08882007-03-13 08:26:18 +0000289
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500290/**
291 * Iterate through all keys and values of an object.
292 *
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500293 * Adding keys to the object while iterating is NOT allowed.
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500294 *
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500295 * Deleting an existing key, or replacing an existing key with a
296 * new value IS allowed.
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500297 *
Michael Clark4504df72007-03-13 08:26:20 +0000298 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000299 * @param key the local name for the char* key variable defined in the body
Keith Derrickca519fb2012-04-05 19:33:09 -0700300 * @param val the local name for the json_object* object variable defined in
301 * the body
Michael Clarkf0d08882007-03-13 08:26:18 +0000302 */
Michael Clark4504df72007-03-13 08:26:20 +0000303#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
Michael Clarkf0d08882007-03-13 08:26:18 +0000304
Michael Clark4504df72007-03-13 08:26:20 +0000305# define json_object_object_foreach(obj,key,val) \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500306 char *key; \
307 struct json_object *val; \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500308 for(struct lh_entry *entry = json_object_get_object(obj)->head, *entry_next = NULL; \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500309 ({ if(entry) { \
310 key = (char*)entry->k; \
311 val = (struct json_object*)entry->v; \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500312 entry_next = entry->next; \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500313 } ; entry; }); \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500314 entry = entry_next )
Michael Clark4504df72007-03-13 08:26:20 +0000315
316#else /* ANSI C or MSC */
317
318# define json_object_object_foreach(obj,key,val) \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500319 char *key;\
320 struct json_object *val; \
321 struct lh_entry *entry; \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500322 struct lh_entry *entry_next = NULL; \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500323 for(entry = json_object_get_object(obj)->head; \
324 (entry ? ( \
325 key = (char*)entry->k, \
326 val = (struct json_object*)entry->v, \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500327 entry_next = entry->next, \
Eric Haszlakiewicz5abc0ea2012-10-20 20:10:15 -0500328 entry) : 0); \
Eric Haszlakiewiczf6b27cb2012-10-20 20:26:37 -0500329 entry = entry_next)
Michael Clark4504df72007-03-13 08:26:20 +0000330
331#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
332
333/** Iterate through all keys and values of an object (ANSI C Safe)
334 * @param obj the json_object instance
335 * @param iter the object iterator
336 */
337#define json_object_object_foreachC(obj,iter) \
338 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 +0000339
340/* Array type methods */
341
342/** Create a new empty json_object of type json_type_array
343 * @returns a json_object of type json_type_array
344 */
Michael Clark14862b12007-12-07 02:50:42 +0000345extern struct json_object* json_object_new_array(void);
Michael Clarkf0d08882007-03-13 08:26:18 +0000346
347/** Get the arraylist of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000348 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000349 * @returns an arraylist
350 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000351extern struct array_list* json_object_get_array(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000352
353/** Get the length of a json_object of type json_type_array
Michael Clarkf6a6e482007-03-13 08:26:23 +0000354 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000355 * @returns an int
356 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000357extern int json_object_array_length(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000358
Frederik Deweerdtc43871c2011-10-07 21:07:18 +0200359/** Sorts the elements of jso of type json_type_array
360*
361* Pointers to the json_object pointers will be passed as the two arguments
362* to @sort_fn
363*
364* @param obj the json_object instance
365* @param sort_fn a sorting function
366*/
367extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
368
Michael Clarkf0d08882007-03-13 08:26:18 +0000369/** Add an element to the end of a json_object of type json_type_array
370 *
371 * The reference count will *not* be incremented. This is to make adding
372 * fields to objects in code more compact. If you want to retain a reference
373 * to an added object you must wrap the passed object with json_object_get
374 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000375 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000376 * @param val the json_object to be added
377 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000378extern int json_object_array_add(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000379 struct json_object *val);
380
381/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
382 *
383 * The reference count will *not* be incremented. This is to make adding
384 * fields to objects in code more compact. If you want to retain a reference
385 * to an added object you must wrap the passed object with json_object_get
386 *
387 * The reference count of a replaced object will be decremented.
388 *
389 * The array size will be automatically be expanded to the size of the
390 * index if the index is larger than the current size.
391 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000392 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000393 * @param idx the index to insert the element at
394 * @param val the json_object to be added
395 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000396extern int json_object_array_put_idx(struct json_object *obj, int idx,
Michael Clarkf0d08882007-03-13 08:26:18 +0000397 struct json_object *val);
398
399/** Get the element at specificed index of the array (a json_object of type json_type_array)
Michael Clarkf6a6e482007-03-13 08:26:23 +0000400 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000401 * @param idx the index to get the element at
402 * @returns the json_object at the specified index (or NULL)
403 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000404extern struct json_object* json_object_array_get_idx(struct json_object *obj,
Michael Clarkf0d08882007-03-13 08:26:18 +0000405 int idx);
406
Keith Derrick37e74672012-03-26 14:29:31 -0700407/* json_bool type methods */
Michael Clarkf0d08882007-03-13 08:26:18 +0000408
409/** Create a new empty json_object of type json_type_boolean
Keith Derrick37e74672012-03-26 14:29:31 -0700410 * @param b a json_bool TRUE or FALSE (0 or 1)
Michael Clarkf0d08882007-03-13 08:26:18 +0000411 * @returns a json_object of type json_type_boolean
412 */
Keith Derrick37e74672012-03-26 14:29:31 -0700413extern struct json_object* json_object_new_boolean(json_bool b);
Michael Clarkf0d08882007-03-13 08:26:18 +0000414
Keith Derrick37e74672012-03-26 14:29:31 -0700415/** Get the json_bool value of a json_object
Michael Clarkf0d08882007-03-13 08:26:18 +0000416 *
Keith Derrick37e74672012-03-26 14:29:31 -0700417 * The type is coerced to a json_bool if the passed object is not a json_bool.
Michael Clarkf0d08882007-03-13 08:26:18 +0000418 * integer and double objects will return FALSE if there value is zero
419 * or TRUE otherwise. If the passed object is a string it will return
420 * TRUE if it has a non zero length. If any other object type is passed
421 * TRUE will be returned if the object is not NULL.
422 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000423 * @param obj the json_object instance
Keith Derrick37e74672012-03-26 14:29:31 -0700424 * @returns a json_bool
Michael Clarkf0d08882007-03-13 08:26:18 +0000425 */
Keith Derrick37e74672012-03-26 14:29:31 -0700426extern json_bool json_object_get_boolean(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000427
428
429/* int type methods */
430
431/** Create a new empty json_object of type json_type_int
ehaszla252669c2010-12-07 18:15:35 +0000432 * Note that values are stored as 64-bit values internally.
433 * To ensure the full range is maintained, use json_object_new_int64 instead.
Michael Clarkf0d08882007-03-13 08:26:18 +0000434 * @param i the integer
435 * @returns a json_object of type json_type_int
436 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000437extern struct json_object* json_object_new_int(int32_t i);
438
439
ehaszla252669c2010-12-07 18:15:35 +0000440/** Create a new empty json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000441 * @param i the integer
ehaszla252669c2010-12-07 18:15:35 +0000442 * @returns a json_object of type json_type_int
Michael Clarkc4dceae2010-10-06 16:39:20 +0000443 */
444extern struct json_object* json_object_new_int64(int64_t i);
445
Michael Clarkf0d08882007-03-13 08:26:18 +0000446
447/** Get the int value of a json_object
448 *
449 * The type is coerced to a int if the passed object is not a int.
450 * double objects will return their integer conversion. Strings will be
Keith Derrickca519fb2012-04-05 19:33:09 -0700451 * parsed as an integer. If no conversion exists then 0 is returned
452 * and errno is set to EINVAL. null is equivalent to 0 (no error values set)
Michael Clarkf0d08882007-03-13 08:26:18 +0000453 *
ehaszla252669c2010-12-07 18:15:35 +0000454 * Note that integers are stored internally as 64-bit values.
455 * If the value of too big or too small to fit into 32-bit, INT32_MAX or
456 * INT32_MIN are returned, respectively.
457 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000458 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000459 * @returns an int
460 */
Michael Clarkc4dceae2010-10-06 16:39:20 +0000461extern int32_t json_object_get_int(struct json_object *obj);
462
463/** Get the int value of a json_object
464 *
465 * The type is coerced to a int64 if the passed object is not a int64.
466 * double objects will return their int64 conversion. Strings will be
467 * parsed as an int64. If no conversion exists then 0 is returned.
468 *
Keith Derrickca519fb2012-04-05 19:33:09 -0700469 * NOTE: Set errno to 0 directly before a call to this function to determine
470 * whether or not conversion was successful (it does not clear the value for
471 * you).
472 *
Michael Clarkc4dceae2010-10-06 16:39:20 +0000473 * @param obj the json_object instance
474 * @returns an int64
475 */
476extern int64_t json_object_get_int64(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000477
478
479/* double type methods */
480
481/** Create a new empty json_object of type json_type_double
482 * @param d the double
483 * @returns a json_object of type json_type_double
484 */
485extern struct json_object* json_object_new_double(double d);
486
Keith Derrickca519fb2012-04-05 19:33:09 -0700487/** Get the double floating point value of a json_object
Michael Clarkf0d08882007-03-13 08:26:18 +0000488 *
489 * The type is coerced to a double if the passed object is not a double.
Keith Derrickca519fb2012-04-05 19:33:09 -0700490 * integer objects will return their double conversion. Strings will be
491 * parsed as a double. If no conversion exists then 0.0 is returned and
492 * errno is set to EINVAL. null is equivalent to 0 (no error values set)
493 *
494 * If the value is too big to fit in a double, then the value is set to
495 * the closest infinity with errno set to ERANGE. If strings cannot be
496 * converted to their double value, then EINVAL is set & NaN is returned.
497 *
498 * Arrays of length 0 are interpreted as 0 (with no error flags set).
499 * Arrays of length 1 are effectively cast to the equivalent object and
500 * converted using the above rules. All other arrays set the error to
501 * EINVAL & return NaN.
502 *
503 * NOTE: Set errno to 0 directly before a call to this function to
504 * determine whether or not conversion was successful (it does not clear
505 * the value for you).
Michael Clarkf0d08882007-03-13 08:26:18 +0000506 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000507 * @param obj the json_object instance
Keith Derrickca519fb2012-04-05 19:33:09 -0700508 * @returns a double floating point number
Michael Clarkf0d08882007-03-13 08:26:18 +0000509 */
Michael Clarkf6a6e482007-03-13 08:26:23 +0000510extern double json_object_get_double(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000511
512
513/* string type methods */
514
515/** Create a new empty json_object of type json_type_string
516 *
517 * A copy of the string is made and the memory is managed by the json_object
518 *
519 * @param s the string
520 * @returns a json_object of type json_type_string
521 */
Michael Clark68cafad2009-01-06 22:56:57 +0000522extern struct json_object* json_object_new_string(const char *s);
Michael Clarkf0d08882007-03-13 08:26:18 +0000523
Michael Clark68cafad2009-01-06 22:56:57 +0000524extern struct json_object* json_object_new_string_len(const char *s, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +0000525
526/** Get the string value of a json_object
527 *
528 * If the passed object is not of type json_type_string then the JSON
529 * representation of the object is returned.
530 *
531 * The returned string memory is managed by the json_object and will
532 * be freed when the reference count of the json_object drops to zero.
533 *
Michael Clarkf6a6e482007-03-13 08:26:23 +0000534 * @param obj the json_object instance
Michael Clarkf0d08882007-03-13 08:26:18 +0000535 * @returns a string
536 */
Michael Clark68cafad2009-01-06 22:56:57 +0000537extern const char* json_object_get_string(struct json_object *obj);
Michael Clarkf0d08882007-03-13 08:26:18 +0000538
Jehiah Czebotarac601b52011-01-14 17:23:06 +0000539/** Get the string length of a json_object
540 *
541 * If the passed object is not of type json_type_string then zero
542 * will be returned.
543 *
544 * @param obj the json_object instance
545 * @returns int
546 */
547extern int json_object_get_string_len(struct json_object *obj);
548
Michael Clarkaaec1ef2009-02-25 02:31:32 +0000549#ifdef __cplusplus
550}
551#endif
552
Michael Clarkf0d08882007-03-13 08:26:18 +0000553#endif