Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 2 | * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 6 | * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 7 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 8 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
| 13 | #ifndef _json_object_h_ |
| 14 | #define _json_object_h_ |
| 15 | |
Eric Haszlakiewicz | b21b137 | 2012-02-15 20:44:54 -0600 | [diff] [blame] | 16 | #include "json_inttypes.h" |
| 17 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Michael Clark | e6548a3 | 2009-01-10 13:58:06 +0000 | [diff] [blame] | 22 | #define JSON_OBJECT_DEF_HASH_ENTRIES 16 |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 23 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 24 | /** |
| 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 46 | #undef FALSE |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 47 | #define FALSE ((json_bool)0) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 48 | |
| 49 | #undef TRUE |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 50 | #define TRUE ((json_bool)1) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 51 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 52 | extern const char *json_number_chars; |
| 53 | extern const char *json_hex_chars; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 54 | |
Jehiah Czebotar | 43d2f41 | 2011-05-25 04:49:20 +0000 | [diff] [blame] | 55 | /* CAW: added for ANSI C iteration correctness */ |
| 56 | struct json_object_iter |
| 57 | { |
| 58 | char *key; |
| 59 | struct json_object *val; |
| 60 | struct lh_entry *entry; |
| 61 | }; |
| 62 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 63 | /* forward structure definitions */ |
| 64 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 65 | typedef int json_bool; |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 66 | typedef struct printbuf printbuf; |
| 67 | typedef struct lh_table lh_table; |
| 68 | typedef struct array_list array_list; |
| 69 | typedef struct json_object json_object; |
| 70 | typedef struct json_object_iter json_object_iter; |
| 71 | typedef struct json_tokener json_tokener; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 72 | |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 73 | /** |
| 74 | * Type of custom user delete functions. See json_object_set_serializer. |
| 75 | */ |
| 76 | typedef 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 | */ |
| 81 | typedef int (json_object_to_json_string_fn)(struct json_object *jso, |
| 82 | struct printbuf *pb, |
| 83 | int level, |
| 84 | int flags); |
| 85 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 86 | /* supported object types */ |
| 87 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 88 | typedef enum json_type { |
Eric Haszlakiewicz | 886c4fb | 2011-05-03 20:40:49 +0000 | [diff] [blame] | 89 | /* If you change this, be sure to update json_type_to_name() too */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 90 | json_type_null, |
| 91 | json_type_boolean, |
| 92 | json_type_double, |
| 93 | json_type_int, |
| 94 | json_type_object, |
| 95 | json_type_array, |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 96 | json_type_string, |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 97 | } json_type; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 98 | |
| 99 | /* reference counting functions */ |
| 100 | |
| 101 | /** |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 102 | * Increment the reference count of json_object, thereby grabbing shared |
| 103 | * ownership of obj. |
| 104 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 105 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 106 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 107 | extern struct json_object* json_object_get(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 110 | * 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 114 | * @param obj the json_object instance |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 115 | * @returns 1 if the object was freed. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 116 | */ |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 117 | int json_object_put(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Check if the json_object is of a given type |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 121 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 122 | * @param type one of: |
Eric Haszlakiewicz | aef439a | 2012-03-31 13:47:28 -0500 | [diff] [blame] | 123 | json_type_null (i.e. obj == NULL), |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 124 | 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 131 | extern int json_object_is_type(struct json_object *obj, enum json_type type); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
Eric Haszlakiewicz | 6ff0817 | 2012-03-31 22:47:47 -0500 | [diff] [blame] | 134 | * 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 137 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 138 | * @returns type being one of: |
Eric Haszlakiewicz | aef439a | 2012-03-31 13:47:28 -0500 | [diff] [blame] | 139 | json_type_null (i.e. obj == NULL), |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 140 | 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 147 | extern enum json_type json_object_get_type(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 148 | |
| 149 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 150 | /** Stringify object to json format. |
| 151 | * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED) |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 152 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 153 | * @returns a string in JSON format |
| 154 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 155 | extern const char* json_object_to_json_string(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 156 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 157 | /** 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 | */ |
| 162 | extern const char* json_object_to_json_string_ext(struct json_object *obj, int |
| 163 | flags); |
| 164 | |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 165 | /** |
| 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 | */ |
| 191 | void 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 197 | |
| 198 | /* object type methods */ |
| 199 | |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 200 | /** 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 208 | * @returns a json_object of type json_type_object |
| 209 | */ |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 210 | extern struct json_object* json_object_new_object(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 211 | |
| 212 | /** Get the hashtable of a json_object of type json_type_object |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 213 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 214 | * @returns a linkhash |
| 215 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 216 | extern struct lh_table* json_object_get_object(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 217 | |
| 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 Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 222 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 229 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 230 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 231 | * @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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 234 | extern void json_object_object_add(struct json_object* obj, const char *key, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 235 | struct json_object *val); |
| 236 | |
| 237 | /** Get the json_object associate with a given object field |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 238 | * |
| 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 249 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 250 | * @param key the object field name |
| 251 | * @returns the json_object associated with the given field name |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 252 | * @deprecated Please use json_object_object_get_ex |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 253 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 254 | extern struct json_object* json_object_object_get(struct json_object* obj, |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 255 | const char *key); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 256 | |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 257 | /** 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 | */ |
| 275 | extern json_bool json_object_object_get_ex(struct json_object* obj, |
| 276 | const char *key, |
| 277 | struct json_object **value); |
| 278 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 279 | /** Delete the given json_object field |
| 280 | * |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 281 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 284 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 285 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 286 | * @param key the object field name |
| 287 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 288 | extern void json_object_object_del(struct json_object* obj, const char *key); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 289 | |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 290 | /** |
| 291 | * Iterate through all keys and values of an object. |
| 292 | * |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 293 | * Adding keys to the object while iterating is NOT allowed. |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 294 | * |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 295 | * Deleting an existing key, or replacing an existing key with a |
| 296 | * new value IS allowed. |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 297 | * |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 298 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 299 | * @param key the local name for the char* key variable defined in the body |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 300 | * @param val the local name for the json_object* object variable defined in |
| 301 | * the body |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 302 | */ |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 303 | #if defined(__GNUC__) && !defined(__STRICT_ANSI__) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 304 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 305 | # define json_object_object_foreach(obj,key,val) \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 306 | char *key; \ |
| 307 | struct json_object *val; \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 308 | for(struct lh_entry *entry = json_object_get_object(obj)->head, *entry_next = NULL; \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 309 | ({ if(entry) { \ |
| 310 | key = (char*)entry->k; \ |
| 311 | val = (struct json_object*)entry->v; \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 312 | entry_next = entry->next; \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 313 | } ; entry; }); \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 314 | entry = entry_next ) |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 315 | |
| 316 | #else /* ANSI C or MSC */ |
| 317 | |
| 318 | # define json_object_object_foreach(obj,key,val) \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 319 | char *key;\ |
| 320 | struct json_object *val; \ |
| 321 | struct lh_entry *entry; \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 322 | struct lh_entry *entry_next = NULL; \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 323 | for(entry = json_object_get_object(obj)->head; \ |
| 324 | (entry ? ( \ |
| 325 | key = (char*)entry->k, \ |
| 326 | val = (struct json_object*)entry->v, \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 327 | entry_next = entry->next, \ |
Eric Haszlakiewicz | 5abc0ea | 2012-10-20 20:10:15 -0500 | [diff] [blame] | 328 | entry) : 0); \ |
Eric Haszlakiewicz | f6b27cb | 2012-10-20 20:26:37 -0500 | [diff] [blame] | 329 | entry = entry_next) |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 330 | |
| 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 339 | |
| 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 Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 345 | extern struct json_object* json_object_new_array(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 346 | |
| 347 | /** Get the arraylist of a json_object of type json_type_array |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 348 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 349 | * @returns an arraylist |
| 350 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 351 | extern struct array_list* json_object_get_array(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 352 | |
| 353 | /** Get the length of a json_object of type json_type_array |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 354 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 355 | * @returns an int |
| 356 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 357 | extern int json_object_array_length(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 358 | |
Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 359 | /** 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 | */ |
| 367 | extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)); |
| 368 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 369 | /** 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 375 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 376 | * @param val the json_object to be added |
| 377 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 378 | extern int json_object_array_add(struct json_object *obj, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 379 | 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 392 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 393 | * @param idx the index to insert the element at |
| 394 | * @param val the json_object to be added |
| 395 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 396 | extern int json_object_array_put_idx(struct json_object *obj, int idx, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 397 | struct json_object *val); |
| 398 | |
| 399 | /** Get the element at specificed index of the array (a json_object of type json_type_array) |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 400 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 401 | * @param idx the index to get the element at |
| 402 | * @returns the json_object at the specified index (or NULL) |
| 403 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 404 | extern struct json_object* json_object_array_get_idx(struct json_object *obj, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 405 | int idx); |
| 406 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 407 | /* json_bool type methods */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 408 | |
| 409 | /** Create a new empty json_object of type json_type_boolean |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 410 | * @param b a json_bool TRUE or FALSE (0 or 1) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 411 | * @returns a json_object of type json_type_boolean |
| 412 | */ |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 413 | extern struct json_object* json_object_new_boolean(json_bool b); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 414 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 415 | /** Get the json_bool value of a json_object |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 416 | * |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 417 | * The type is coerced to a json_bool if the passed object is not a json_bool. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 418 | * 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 423 | * @param obj the json_object instance |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 424 | * @returns a json_bool |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 425 | */ |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 426 | extern json_bool json_object_get_boolean(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 427 | |
| 428 | |
| 429 | /* int type methods */ |
| 430 | |
| 431 | /** Create a new empty json_object of type json_type_int |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 432 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 434 | * @param i the integer |
| 435 | * @returns a json_object of type json_type_int |
| 436 | */ |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 437 | extern struct json_object* json_object_new_int(int32_t i); |
| 438 | |
| 439 | |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 440 | /** Create a new empty json_object of type json_type_int |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 441 | * @param i the integer |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 442 | * @returns a json_object of type json_type_int |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 443 | */ |
| 444 | extern struct json_object* json_object_new_int64(int64_t i); |
| 445 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 446 | |
| 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 Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 451 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 453 | * |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 454 | * 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 458 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 459 | * @returns an int |
| 460 | */ |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 461 | extern 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 Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 469 | * 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 Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 473 | * @param obj the json_object instance |
| 474 | * @returns an int64 |
| 475 | */ |
| 476 | extern int64_t json_object_get_int64(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 477 | |
| 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 | */ |
| 485 | extern struct json_object* json_object_new_double(double d); |
| 486 | |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 487 | /** Get the double floating point value of a json_object |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 488 | * |
| 489 | * The type is coerced to a double if the passed object is not a double. |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 490 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 506 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 507 | * @param obj the json_object instance |
Keith Derrick | ca519fb | 2012-04-05 19:33:09 -0700 | [diff] [blame] | 508 | * @returns a double floating point number |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 509 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 510 | extern double json_object_get_double(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 511 | |
| 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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 522 | extern struct json_object* json_object_new_string(const char *s); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 523 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 524 | extern struct json_object* json_object_new_string_len(const char *s, int len); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 525 | |
| 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 534 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 535 | * @returns a string |
| 536 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 537 | extern const char* json_object_get_string(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 538 | |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 539 | /** 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 | */ |
| 547 | extern int json_object_get_string_len(struct json_object *obj); |
| 548 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 549 | #ifdef __cplusplus |
| 550 | } |
| 551 | #endif |
| 552 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 553 | #endif |