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> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the MIT license. See COPYING for details. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #ifndef _json_object_h_ |
| 13 | #define _json_object_h_ |
| 14 | |
Eric Haszlakiewicz | b21b137 | 2012-02-15 20:44:54 -0600 | [diff] [blame] | 15 | #include "json_inttypes.h" |
| 16 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
Michael Clark | e6548a3 | 2009-01-10 13:58:06 +0000 | [diff] [blame] | 21 | #define JSON_OBJECT_DEF_HASH_ENTRIES 16 |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 22 | |
| 23 | #undef FALSE |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 24 | #define FALSE ((json_bool)0) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 25 | |
| 26 | #undef TRUE |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 27 | #define TRUE ((json_bool)1) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 28 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 29 | extern const char *json_number_chars; |
| 30 | extern const char *json_hex_chars; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 31 | |
Jehiah Czebotar | 43d2f41 | 2011-05-25 04:49:20 +0000 | [diff] [blame] | 32 | /* CAW: added for ANSI C iteration correctness */ |
| 33 | struct json_object_iter |
| 34 | { |
| 35 | char *key; |
| 36 | struct json_object *val; |
| 37 | struct lh_entry *entry; |
| 38 | }; |
| 39 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 40 | /* forward structure definitions */ |
| 41 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 42 | typedef int json_bool; |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 43 | typedef struct printbuf printbuf; |
| 44 | typedef struct lh_table lh_table; |
| 45 | typedef struct array_list array_list; |
| 46 | typedef struct json_object json_object; |
| 47 | typedef struct json_object_iter json_object_iter; |
| 48 | typedef struct json_tokener json_tokener; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 49 | |
| 50 | /* supported object types */ |
| 51 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 52 | typedef enum json_type { |
Eric Haszlakiewicz | 886c4fb | 2011-05-03 20:40:49 +0000 | [diff] [blame] | 53 | /* 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] | 54 | json_type_null, |
| 55 | json_type_boolean, |
| 56 | json_type_double, |
| 57 | json_type_int, |
| 58 | json_type_object, |
| 59 | json_type_array, |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 60 | json_type_string, |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 61 | } json_type; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 62 | |
| 63 | /* reference counting functions */ |
| 64 | |
| 65 | /** |
| 66 | * Increment the reference count of json_object |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 67 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 68 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 69 | extern struct json_object* json_object_get(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Decrement the reference count of json_object and free if it reaches zero |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 73 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 74 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 75 | extern void json_object_put(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /** |
| 79 | * Check if the json_object is of a given type |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 80 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 81 | * @param type one of: |
| 82 | json_type_boolean, |
| 83 | json_type_double, |
| 84 | json_type_int, |
| 85 | json_type_object, |
| 86 | json_type_array, |
| 87 | json_type_string, |
| 88 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 89 | 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] | 90 | |
| 91 | /** |
| 92 | * Get the type of the json_object |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 93 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 94 | * @returns type being one of: |
| 95 | json_type_boolean, |
| 96 | json_type_double, |
| 97 | json_type_int, |
| 98 | json_type_object, |
| 99 | json_type_array, |
| 100 | json_type_string, |
| 101 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 102 | extern enum json_type json_object_get_type(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | /** Stringify object to json format |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 106 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 107 | * @returns a string in JSON format |
| 108 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 109 | extern const char* json_object_to_json_string(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /* object type methods */ |
| 113 | |
| 114 | /** Create a new empty object |
| 115 | * @returns a json_object of type json_type_object |
| 116 | */ |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 117 | extern struct json_object* json_object_new_object(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 118 | |
| 119 | /** Get the hashtable of a json_object of type json_type_object |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 120 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 121 | * @returns a linkhash |
| 122 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 123 | extern struct lh_table* json_object_get_object(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 124 | |
| 125 | /** Add an object field to a json_object of type json_type_object |
| 126 | * |
| 127 | * The reference count will *not* be incremented. This is to make adding |
| 128 | * fields to objects in code more compact. If you want to retain a reference |
| 129 | * to an added object you must wrap the passed object with json_object_get |
| 130 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 131 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 132 | * @param key the object field name (a private copy will be duplicated) |
| 133 | * @param val a json_object or NULL member to associate with the given field |
| 134 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 135 | 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] | 136 | struct json_object *val); |
| 137 | |
| 138 | /** Get the json_object associate with a given object field |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 139 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 140 | * @param key the object field name |
| 141 | * @returns the json_object associated with the given field name |
| 142 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 143 | extern struct json_object* json_object_object_get(struct json_object* obj, |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 144 | const char *key); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 145 | |
| 146 | /** Delete the given json_object field |
| 147 | * |
| 148 | * The reference count will be decremented for the deleted object |
| 149 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 150 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 151 | * @param key the object field name |
| 152 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | /** Iterate through all keys and values of an object |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 156 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 157 | * @param key the local name for the char* key variable defined in the body |
| 158 | * @param val the local name for the json_object* object variable defined in the body |
| 159 | */ |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 160 | #if defined(__GNUC__) && !defined(__STRICT_ANSI__) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 161 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 162 | # define json_object_object_foreach(obj,key,val) \ |
| 163 | char *key; struct json_object *val; \ |
| 164 | for(struct lh_entry *entry = json_object_get_object(obj)->head; ({ if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } ; entry; }); entry = entry->next ) |
| 165 | |
| 166 | #else /* ANSI C or MSC */ |
| 167 | |
| 168 | # define json_object_object_foreach(obj,key,val) \ |
| 169 | char *key; struct json_object *val; struct lh_entry *entry; \ |
| 170 | for(entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, val = (struct json_object*)entry->v, entry) : 0); entry = entry->next) |
| 171 | |
| 172 | #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */ |
| 173 | |
| 174 | /** Iterate through all keys and values of an object (ANSI C Safe) |
| 175 | * @param obj the json_object instance |
| 176 | * @param iter the object iterator |
| 177 | */ |
| 178 | #define json_object_object_foreachC(obj,iter) \ |
| 179 | for(iter.entry = json_object_get_object(obj)->head; (iter.entry ? (iter.key = (char*)iter.entry->k, iter.val = (struct json_object*)iter.entry->v, iter.entry) : 0); iter.entry = iter.entry->next) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 180 | |
| 181 | /* Array type methods */ |
| 182 | |
| 183 | /** Create a new empty json_object of type json_type_array |
| 184 | * @returns a json_object of type json_type_array |
| 185 | */ |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 186 | extern struct json_object* json_object_new_array(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 187 | |
| 188 | /** Get the arraylist of a json_object of type json_type_array |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 189 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 190 | * @returns an arraylist |
| 191 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 192 | extern struct array_list* json_object_get_array(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 193 | |
| 194 | /** Get the length of a json_object of type json_type_array |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 195 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 196 | * @returns an int |
| 197 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 198 | extern int json_object_array_length(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 199 | |
Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 200 | /** Sorts the elements of jso of type json_type_array |
| 201 | * |
| 202 | * Pointers to the json_object pointers will be passed as the two arguments |
| 203 | * to @sort_fn |
| 204 | * |
| 205 | * @param obj the json_object instance |
| 206 | * @param sort_fn a sorting function |
| 207 | */ |
| 208 | extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)); |
| 209 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 210 | /** Add an element to the end of a json_object of type json_type_array |
| 211 | * |
| 212 | * The reference count will *not* be incremented. This is to make adding |
| 213 | * fields to objects in code more compact. If you want to retain a reference |
| 214 | * to an added object you must wrap the passed object with json_object_get |
| 215 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 216 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 217 | * @param val the json_object to be added |
| 218 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 219 | extern int json_object_array_add(struct json_object *obj, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 220 | struct json_object *val); |
| 221 | |
| 222 | /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array) |
| 223 | * |
| 224 | * The reference count will *not* be incremented. This is to make adding |
| 225 | * fields to objects in code more compact. If you want to retain a reference |
| 226 | * to an added object you must wrap the passed object with json_object_get |
| 227 | * |
| 228 | * The reference count of a replaced object will be decremented. |
| 229 | * |
| 230 | * The array size will be automatically be expanded to the size of the |
| 231 | * index if the index is larger than the current size. |
| 232 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 233 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 234 | * @param idx the index to insert the element at |
| 235 | * @param val the json_object to be added |
| 236 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 237 | 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] | 238 | struct json_object *val); |
| 239 | |
| 240 | /** 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] | 241 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 242 | * @param idx the index to get the element at |
| 243 | * @returns the json_object at the specified index (or NULL) |
| 244 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 245 | 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] | 246 | int idx); |
| 247 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 248 | /* json_bool type methods */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 249 | |
| 250 | /** Create a new empty json_object of type json_type_boolean |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 251 | * @param b a json_bool TRUE or FALSE (0 or 1) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 252 | * @returns a json_object of type json_type_boolean |
| 253 | */ |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 254 | extern struct json_object* json_object_new_boolean(json_bool b); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 255 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 256 | /** Get the json_bool value of a json_object |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 257 | * |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 258 | * 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] | 259 | * integer and double objects will return FALSE if there value is zero |
| 260 | * or TRUE otherwise. If the passed object is a string it will return |
| 261 | * TRUE if it has a non zero length. If any other object type is passed |
| 262 | * TRUE will be returned if the object is not NULL. |
| 263 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 264 | * @param obj the json_object instance |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 265 | * @returns a json_bool |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 266 | */ |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 267 | extern json_bool json_object_get_boolean(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | /* int type methods */ |
| 271 | |
| 272 | /** Create a new empty json_object of type json_type_int |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 273 | * Note that values are stored as 64-bit values internally. |
| 274 | * To ensure the full range is maintained, use json_object_new_int64 instead. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 275 | * @param i the integer |
| 276 | * @returns a json_object of type json_type_int |
| 277 | */ |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 278 | extern struct json_object* json_object_new_int(int32_t i); |
| 279 | |
| 280 | |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 281 | /** Create a new empty json_object of type json_type_int |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 282 | * @param i the integer |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 283 | * @returns a json_object of type json_type_int |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 284 | */ |
| 285 | extern struct json_object* json_object_new_int64(int64_t i); |
| 286 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 287 | |
| 288 | /** Get the int value of a json_object |
| 289 | * |
| 290 | * The type is coerced to a int if the passed object is not a int. |
| 291 | * double objects will return their integer conversion. Strings will be |
| 292 | * parsed as an integer. If no conversion exists then 0 is returned. |
| 293 | * |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 294 | * Note that integers are stored internally as 64-bit values. |
| 295 | * If the value of too big or too small to fit into 32-bit, INT32_MAX or |
| 296 | * INT32_MIN are returned, respectively. |
| 297 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 298 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 299 | * @returns an int |
| 300 | */ |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 301 | extern int32_t json_object_get_int(struct json_object *obj); |
| 302 | |
| 303 | /** Get the int value of a json_object |
| 304 | * |
| 305 | * The type is coerced to a int64 if the passed object is not a int64. |
| 306 | * double objects will return their int64 conversion. Strings will be |
| 307 | * parsed as an int64. If no conversion exists then 0 is returned. |
| 308 | * |
| 309 | * @param obj the json_object instance |
| 310 | * @returns an int64 |
| 311 | */ |
| 312 | extern int64_t json_object_get_int64(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | /* double type methods */ |
| 316 | |
| 317 | /** Create a new empty json_object of type json_type_double |
| 318 | * @param d the double |
| 319 | * @returns a json_object of type json_type_double |
| 320 | */ |
| 321 | extern struct json_object* json_object_new_double(double d); |
| 322 | |
| 323 | /** Get the double value of a json_object |
| 324 | * |
| 325 | * The type is coerced to a double if the passed object is not a double. |
| 326 | * integer objects will return their dboule conversion. Strings will be |
| 327 | * parsed as a double. If no conversion exists then 0.0 is returned. |
| 328 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 329 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 330 | * @returns an double |
| 331 | */ |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 332 | extern double json_object_get_double(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | /* string type methods */ |
| 336 | |
| 337 | /** Create a new empty json_object of type json_type_string |
| 338 | * |
| 339 | * A copy of the string is made and the memory is managed by the json_object |
| 340 | * |
| 341 | * @param s the string |
| 342 | * @returns a json_object of type json_type_string |
| 343 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 344 | extern struct json_object* json_object_new_string(const char *s); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 345 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 346 | 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] | 347 | |
| 348 | /** Get the string value of a json_object |
| 349 | * |
| 350 | * If the passed object is not of type json_type_string then the JSON |
| 351 | * representation of the object is returned. |
| 352 | * |
| 353 | * The returned string memory is managed by the json_object and will |
| 354 | * be freed when the reference count of the json_object drops to zero. |
| 355 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 356 | * @param obj the json_object instance |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 357 | * @returns a string |
| 358 | */ |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 359 | extern const char* json_object_get_string(struct json_object *obj); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 360 | |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 361 | /** Get the string length of a json_object |
| 362 | * |
| 363 | * If the passed object is not of type json_type_string then zero |
| 364 | * will be returned. |
| 365 | * |
| 366 | * @param obj the json_object instance |
| 367 | * @returns int |
| 368 | */ |
| 369 | extern int json_object_get_string_len(struct json_object *obj); |
| 370 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 371 | #ifdef __cplusplus |
| 372 | } |
| 373 | #endif |
| 374 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 375 | #endif |