w.deng | e87b500 | 2025-08-20 10:43:03 +0800 | [diff] [blame] | 1 | /* |
| 2 | * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
| 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
| 7 | * |
| 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. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * @file |
| 15 | * @brief Core json-c API. Start here, or with json_tokener.h |
| 16 | */ |
| 17 | #ifndef _json_object_h_ |
| 18 | #define _json_object_h_ |
| 19 | |
| 20 | #ifdef __GNUC__ |
| 21 | #define JSON_C_CONST_FUNCTION(func) func __attribute__((const)) |
| 22 | #else |
| 23 | #define JSON_C_CONST_FUNCTION(func) func |
| 24 | #endif |
| 25 | |
| 26 | #include "json_inttypes.h" |
| 27 | #include "json_types.h" |
| 28 | #include "printbuf.h" |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | #define JSON_OBJECT_DEF_HASH_ENTRIES 16 |
| 37 | |
| 38 | /** |
| 39 | * A flag for the json_object_to_json_string_ext() and |
| 40 | * json_object_to_file_ext() functions which causes the output |
| 41 | * to have no extra whitespace or formatting applied. |
| 42 | */ |
| 43 | #define JSON_C_TO_STRING_PLAIN 0 |
| 44 | /** |
| 45 | * A flag for the json_object_to_json_string_ext() and |
| 46 | * json_object_to_file_ext() functions which causes the output to have |
| 47 | * minimal whitespace inserted to make things slightly more readable. |
| 48 | */ |
| 49 | #define JSON_C_TO_STRING_SPACED (1 << 0) |
| 50 | /** |
| 51 | * A flag for the json_object_to_json_string_ext() and |
| 52 | * json_object_to_file_ext() functions which causes |
| 53 | * the output to be formatted. |
| 54 | * |
| 55 | * See the "Two Space Tab" option at https://jsonformatter.curiousconcept.com/ |
| 56 | * for an example of the format. |
| 57 | */ |
| 58 | #define JSON_C_TO_STRING_PRETTY (1 << 1) |
| 59 | /** |
| 60 | * A flag for the json_object_to_json_string_ext() and |
| 61 | * json_object_to_file_ext() functions which causes |
| 62 | * the output to be formatted. |
| 63 | * |
| 64 | * Instead of a "Two Space Tab" this gives a single tab character. |
| 65 | */ |
| 66 | #define JSON_C_TO_STRING_PRETTY_TAB (1 << 3) |
| 67 | /** |
| 68 | * A flag to drop trailing zero for float values |
| 69 | */ |
| 70 | #define JSON_C_TO_STRING_NOZERO (1 << 2) |
| 71 | |
| 72 | /** |
| 73 | * Don't escape forward slashes. |
| 74 | */ |
| 75 | #define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4) |
| 76 | |
| 77 | /** |
| 78 | * A flag for the json_object_object_add_ex function which |
| 79 | * causes the value to be added without a check if it already exists. |
| 80 | * Note: it is the responsibility of the caller to ensure that no |
| 81 | * key is added multiple times. If this is done, results are |
| 82 | * unpredictable. While this option is somewhat dangerous, it |
| 83 | * permits potentially large performance savings in code that |
| 84 | * knows for sure the key values are unique (e.g. because the |
| 85 | * code adds a well-known set of constant key values). |
| 86 | */ |
| 87 | #define JSON_C_OBJECT_ADD_KEY_IS_NEW (1 << 1) |
| 88 | /** |
| 89 | * A flag for the json_object_object_add_ex function which |
| 90 | * flags the key as being constant memory. This means that |
| 91 | * the key will NOT be copied via strdup(), resulting in a |
| 92 | * potentially huge performance win (malloc, strdup and |
| 93 | * free are usually performance hogs). It is acceptable to |
| 94 | * use this flag for keys in non-constant memory blocks if |
| 95 | * the caller ensure that the memory holding the key lives |
| 96 | * longer than the corresponding json object. However, this |
| 97 | * is somewhat dangerous and should only be done if really |
| 98 | * justified. |
| 99 | * The general use-case for this flag is cases where the |
| 100 | * key is given as a real constant value in the function |
| 101 | * call, e.g. as in |
| 102 | * json_object_object_add_ex(obj, "ip", json, |
| 103 | * JSON_C_OBJECT_ADD_CONSTANT_KEY); |
| 104 | */ |
| 105 | #define JSON_C_OBJECT_ADD_CONSTANT_KEY (1 << 2) |
| 106 | /** |
| 107 | * This flag is an alias to JSON_C_OBJECT_ADD_CONSTANT_KEY. |
| 108 | * Historically, this flag was used first and the new name |
| 109 | * JSON_C_OBJECT_ADD_CONSTANT_KEY was introduced for version |
| 110 | * 0.16.00 in order to have regular naming. |
| 111 | * Use of this flag is now legacy. |
| 112 | */ |
| 113 | #define JSON_C_OBJECT_KEY_IS_CONSTANT JSON_C_OBJECT_ADD_CONSTANT_KEY |
| 114 | |
| 115 | /** |
| 116 | * Set the global value of an option, which will apply to all |
| 117 | * current and future threads that have not set a thread-local value. |
| 118 | * |
| 119 | * @see json_c_set_serialization_double_format |
| 120 | */ |
| 121 | #define JSON_C_OPTION_GLOBAL (0) |
| 122 | /** |
| 123 | * Set a thread-local value of an option, overriding the global value. |
| 124 | * This will fail if json-c is not compiled with threading enabled, and |
| 125 | * with the __thread specifier (or equivalent) available. |
| 126 | * |
| 127 | * @see json_c_set_serialization_double_format |
| 128 | */ |
| 129 | #define JSON_C_OPTION_THREAD (1) |
| 130 | |
| 131 | /* reference counting functions */ |
| 132 | |
| 133 | /** |
| 134 | * Increment the reference count of json_object, thereby taking ownership of it. |
| 135 | * |
| 136 | * Cases where you might need to increase the refcount include: |
| 137 | * - Using an object field or array index (retrieved through |
| 138 | * `json_object_object_get()` or `json_object_array_get_idx()`) |
| 139 | * beyond the lifetime of the parent object. |
| 140 | * - Detaching an object field or array index from its parent object |
| 141 | * (using `json_object_object_del()` or `json_object_array_del_idx()`) |
| 142 | * - Sharing a json_object with multiple (not necessarily parallel) threads |
| 143 | * of execution that all expect to free it (with `json_object_put()`) when |
| 144 | * they're done. |
| 145 | * |
| 146 | * @param obj the json_object instance |
| 147 | * @see json_object_put() |
| 148 | * @see json_object_object_get() |
| 149 | * @see json_object_array_get_idx() |
| 150 | */ |
| 151 | JSON_EXPORT struct json_object *json_object_get(struct json_object *obj); |
| 152 | |
| 153 | /** |
| 154 | * Decrement the reference count of json_object and free if it reaches zero. |
| 155 | * |
| 156 | * You must have ownership of obj prior to doing this or you will cause an |
| 157 | * imbalance in the reference count, leading to a classic use-after-free bug. |
| 158 | * In particular, you normally do not need to call `json_object_put()` on the |
| 159 | * json_object returned by `json_object_object_get()` or `json_object_array_get_idx()`. |
| 160 | * |
| 161 | * Just like after calling `free()` on a block of memory, you must not use |
| 162 | * `obj` after calling `json_object_put()` on it or any object that it |
| 163 | * is a member of (unless you know you've called `json_object_get(obj)` to |
| 164 | * explicitly increment the refcount). |
| 165 | * |
| 166 | * NULL may be passed, which which case this is a no-op. |
| 167 | * |
| 168 | * @param obj the json_object instance |
| 169 | * @returns 1 if the object was freed. |
| 170 | * @see json_object_get() |
| 171 | */ |
| 172 | JSON_EXPORT int json_object_put(struct json_object *obj); |
| 173 | |
| 174 | /** |
| 175 | * Check if the json_object is of a given type |
| 176 | * @param obj the json_object instance |
| 177 | * @param type one of: |
| 178 | json_type_null (i.e. obj == NULL), |
| 179 | json_type_boolean, |
| 180 | json_type_double, |
| 181 | json_type_int, |
| 182 | json_type_object, |
| 183 | json_type_array, |
| 184 | json_type_string |
| 185 | */ |
| 186 | JSON_EXPORT int json_object_is_type(const struct json_object *obj, enum json_type type); |
| 187 | |
| 188 | /** |
| 189 | * Get the type of the json_object. See also json_type_to_name() to turn this |
| 190 | * into a string suitable, for instance, for logging. |
| 191 | * |
| 192 | * @param obj the json_object instance |
| 193 | * @returns type being one of: |
| 194 | json_type_null (i.e. obj == NULL), |
| 195 | json_type_boolean, |
| 196 | json_type_double, |
| 197 | json_type_int, |
| 198 | json_type_object, |
| 199 | json_type_array, |
| 200 | json_type_string |
| 201 | */ |
| 202 | JSON_EXPORT enum json_type json_object_get_type(const struct json_object *obj); |
| 203 | |
| 204 | /** Stringify object to json format. |
| 205 | * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED) |
| 206 | * The pointer you get is an internal of your json object. You don't |
| 207 | * have to free it, later use of json_object_put() should be sufficient. |
| 208 | * If you can not ensure there's no concurrent access to *obj use |
| 209 | * strdup(). |
| 210 | * @param obj the json_object instance |
| 211 | * @returns a string in JSON format |
| 212 | */ |
| 213 | JSON_EXPORT const char *json_object_to_json_string(struct json_object *obj); |
| 214 | |
| 215 | /** Stringify object to json format |
| 216 | * @see json_object_to_json_string() for details on how to free string. |
| 217 | * @param obj the json_object instance |
| 218 | * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants |
| 219 | * @returns a string in JSON format |
| 220 | */ |
| 221 | JSON_EXPORT const char *json_object_to_json_string_ext(struct json_object *obj, int flags); |
| 222 | |
| 223 | /** Stringify object to json format |
| 224 | * @see json_object_to_json_string() for details on how to free string. |
| 225 | * @param obj the json_object instance |
| 226 | * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants |
| 227 | * @param length a pointer where, if not NULL, the length (without null) is stored |
| 228 | * @returns a string in JSON format and the length if not NULL |
| 229 | */ |
| 230 | JSON_EXPORT const char *json_object_to_json_string_length(struct json_object *obj, int flags, |
| 231 | size_t *length); |
| 232 | |
| 233 | /** |
| 234 | * Returns the userdata set by json_object_set_userdata() or |
| 235 | * json_object_set_serializer() |
| 236 | * |
| 237 | * @param jso the object to return the userdata for |
| 238 | */ |
| 239 | JSON_EXPORT void *json_object_get_userdata(json_object *jso); |
| 240 | |
| 241 | /** |
| 242 | * Set an opaque userdata value for an object |
| 243 | * |
| 244 | * The userdata can be retrieved using json_object_get_userdata(). |
| 245 | * |
| 246 | * If custom userdata is already set on this object, any existing user_delete |
| 247 | * function is called before the new one is set. |
| 248 | * |
| 249 | * The user_delete parameter is optional and may be passed as NULL, even if |
| 250 | * the userdata parameter is non-NULL. It will be called just before the |
| 251 | * json_object is deleted, after it's reference count goes to zero |
| 252 | * (see json_object_put()). |
| 253 | * If this is not provided, it is up to the caller to free the userdata at |
| 254 | * an appropriate time. (i.e. after the json_object is deleted) |
| 255 | * |
| 256 | * Note: Objects created by parsing strings may have custom serializers set |
| 257 | * which expect the userdata to contain specific data (due to use of |
| 258 | * json_object_new_double_s()). In this case, json_object_set_serialiser() with |
| 259 | * NULL as to_string_func should be used instead to set the userdata and reset |
| 260 | * the serializer to its default value. |
| 261 | * |
| 262 | * @param jso the object to set the userdata for |
| 263 | * @param userdata an optional opaque cookie |
| 264 | * @param user_delete an optional function from freeing userdata |
| 265 | */ |
| 266 | JSON_EXPORT void json_object_set_userdata(json_object *jso, void *userdata, |
| 267 | json_object_delete_fn *user_delete); |
| 268 | |
| 269 | /** |
| 270 | * Set a custom serialization function to be used when this particular object |
| 271 | * is converted to a string by json_object_to_json_string. |
| 272 | * |
| 273 | * If custom userdata is already set on this object, any existing user_delete |
| 274 | * function is called before the new one is set. |
| 275 | * |
| 276 | * If to_string_func is NULL the default behaviour is reset (but the userdata |
| 277 | * and user_delete fields are still set). |
| 278 | * |
| 279 | * The userdata parameter is optional and may be passed as NULL. It can be used |
| 280 | * to provide additional data for to_string_func to use. This parameter may |
| 281 | * be NULL even if user_delete is non-NULL. |
| 282 | * |
| 283 | * The user_delete parameter is optional and may be passed as NULL, even if |
| 284 | * the userdata parameter is non-NULL. It will be called just before the |
| 285 | * json_object is deleted, after it's reference count goes to zero |
| 286 | * (see json_object_put()). |
| 287 | * If this is not provided, it is up to the caller to free the userdata at |
| 288 | * an appropriate time. (i.e. after the json_object is deleted) |
| 289 | * |
| 290 | * Note that the userdata is the same as set by json_object_set_userdata(), so |
| 291 | * care must be taken not to overwrite the value when both a custom serializer |
| 292 | * and json_object_set_userdata() are used. |
| 293 | * |
| 294 | * @param jso the object to customize |
| 295 | * @param to_string_func the custom serialization function |
| 296 | * @param userdata an optional opaque cookie |
| 297 | * @param user_delete an optional function from freeing userdata |
| 298 | */ |
| 299 | JSON_EXPORT void json_object_set_serializer(json_object *jso, |
| 300 | json_object_to_json_string_fn *to_string_func, |
| 301 | void *userdata, json_object_delete_fn *user_delete); |
| 302 | |
| 303 | #ifdef __clang__ |
| 304 | /* |
| 305 | * Clang doesn't pay attention to the parameters defined in the |
| 306 | * function typedefs used here, so turn off spurious doc warnings. |
| 307 | * { |
| 308 | */ |
| 309 | #pragma clang diagnostic push |
| 310 | #pragma clang diagnostic ignored "-Wdocumentation" |
| 311 | #endif |
| 312 | |
| 313 | /** |
| 314 | * Simply call free on the userdata pointer. |
| 315 | * Can be used with json_object_set_serializer(). |
| 316 | * |
| 317 | * @param jso unused |
| 318 | * @param userdata the pointer that is passed to free(). |
| 319 | */ |
| 320 | JSON_EXPORT json_object_delete_fn json_object_free_userdata; |
| 321 | |
| 322 | /** |
| 323 | * Copy the jso->_userdata string over to pb as-is. |
| 324 | * Can be used with json_object_set_serializer(). |
| 325 | * |
| 326 | * @param jso The object whose _userdata is used. |
| 327 | * @param pb The destination buffer. |
| 328 | * @param level Ignored. |
| 329 | * @param flags Ignored. |
| 330 | */ |
| 331 | JSON_EXPORT json_object_to_json_string_fn json_object_userdata_to_json_string; |
| 332 | |
| 333 | #ifdef __clang__ |
| 334 | /* } */ |
| 335 | #pragma clang diagnostic pop |
| 336 | #endif |
| 337 | |
| 338 | /* object type methods */ |
| 339 | |
| 340 | /** Create a new empty object with a reference count of 1. The caller of |
| 341 | * this object initially has sole ownership. Remember, when using |
| 342 | * json_object_object_add or json_object_array_put_idx, ownership will |
| 343 | * transfer to the object/array. Call json_object_get if you want to maintain |
| 344 | * shared ownership or also add this object as a child of multiple objects or |
| 345 | * arrays. Any ownerships you acquired but did not transfer must be released |
| 346 | * through json_object_put. |
| 347 | * |
| 348 | * @returns a json_object of type json_type_object |
| 349 | */ |
| 350 | JSON_EXPORT struct json_object *json_object_new_object(void); |
| 351 | |
| 352 | /** Get the hashtable of a json_object of type json_type_object |
| 353 | * @param obj the json_object instance |
| 354 | * @returns a linkhash |
| 355 | */ |
| 356 | JSON_EXPORT struct lh_table *json_object_get_object(const struct json_object *obj); |
| 357 | |
| 358 | /** Get the size of an object in terms of the number of fields it has. |
| 359 | * @param obj the json_object whose length to return |
| 360 | */ |
| 361 | JSON_EXPORT int json_object_object_length(const struct json_object *obj); |
| 362 | |
| 363 | /** Get the sizeof (struct json_object). |
| 364 | * @returns a size_t with the sizeof (struct json_object) |
| 365 | */ |
| 366 | JSON_C_CONST_FUNCTION(JSON_EXPORT size_t json_c_object_sizeof(void)); |
| 367 | |
| 368 | /** Add an object field to a json_object of type json_type_object |
| 369 | * |
| 370 | * The reference count of `val` will *not* be incremented, in effect |
| 371 | * transferring ownership that object to `obj`, and thus `val` will be |
| 372 | * freed when `obj` is. (i.e. through `json_object_put(obj)`) |
| 373 | * |
| 374 | * If you want to retain a reference to the added object, independent |
| 375 | * of the lifetime of obj, you must increment the refcount with |
| 376 | * `json_object_get(val)` (and later release it with json_object_put()). |
| 377 | * |
| 378 | * Since ownership transfers to `obj`, you must make sure |
| 379 | * that you do in fact have ownership over `val`. For instance, |
| 380 | * json_object_new_object() will give you ownership until you transfer it, |
| 381 | * whereas json_object_object_get() does not. |
| 382 | * |
| 383 | * Any previous object stored under `key` in `obj` will have its refcount |
| 384 | * decremented, and be freed normally if that drops to zero. |
| 385 | * |
| 386 | * @param obj the json_object instance |
| 387 | * @param key the object field name (a private copy will be duplicated) |
| 388 | * @param val a json_object or NULL member to associate with the given field |
| 389 | * |
| 390 | * @return On success, <code>0</code> is returned. |
| 391 | * On error, a negative value is returned. |
| 392 | */ |
| 393 | JSON_EXPORT int json_object_object_add(struct json_object *obj, const char *key, |
| 394 | struct json_object *val); |
| 395 | |
| 396 | /** Add an object field to a json_object of type json_type_object |
| 397 | * |
| 398 | * The semantics are identical to json_object_object_add, except that an |
| 399 | * additional flag fields gives you more control over some detail aspects |
| 400 | * of processing. See the description of JSON_C_OBJECT_ADD_* flags for more |
| 401 | * details. |
| 402 | * |
| 403 | * @param obj the json_object instance |
| 404 | * @param key the object field name (a private copy will be duplicated) |
| 405 | * @param val a json_object or NULL member to associate with the given field |
| 406 | * @param opts process-modifying options. To specify multiple options, use |
| 407 | * (OPT1|OPT2) |
| 408 | */ |
| 409 | JSON_EXPORT int json_object_object_add_ex(struct json_object *obj, const char *const key, |
| 410 | struct json_object *const val, const unsigned opts); |
| 411 | |
| 412 | /** Get the json_object associate with a given object field. |
| 413 | * Deprecated/discouraged: used json_object_object_get_ex instead. |
| 414 | * |
| 415 | * This returns NULL if the field is found but its value is null, or if |
| 416 | * the field is not found, or if obj is not a json_type_object. If you |
| 417 | * need to distinguish between these cases, use json_object_object_get_ex(). |
| 418 | * |
| 419 | * *No* reference counts will be changed. There is no need to manually adjust |
| 420 | * reference counts through the json_object_put/json_object_get methods unless |
| 421 | * you need to have the child (value) reference maintain a different lifetime |
| 422 | * than the owning parent (obj). Ownership of the returned value is retained |
| 423 | * by obj (do not do json_object_put unless you have done a json_object_get). |
| 424 | * If you delete the value from obj (json_object_object_del) and wish to access |
| 425 | * the returned reference afterwards, make sure you have first gotten shared |
| 426 | * ownership through json_object_get (& don't forget to do a json_object_put |
| 427 | * or transfer ownership to prevent a memory leak). |
| 428 | * |
| 429 | * @param obj the json_object instance |
| 430 | * @param key the object field name |
| 431 | * @returns the json_object associated with the given field name |
| 432 | */ |
| 433 | JSON_EXPORT struct json_object *json_object_object_get(const struct json_object *obj, |
| 434 | const char *key); |
| 435 | |
| 436 | /** Get the json_object associated with a given object field. |
| 437 | * |
| 438 | * This returns true if the key is found, false in all other cases (including |
| 439 | * if obj isn't a json_type_object). |
| 440 | * |
| 441 | * *No* reference counts will be changed. There is no need to manually adjust |
| 442 | * reference counts through the json_object_put/json_object_get methods unless |
| 443 | * you need to have the child (value) reference maintain a different lifetime |
| 444 | * than the owning parent (obj). Ownership of value is retained by obj. |
| 445 | * |
| 446 | * @param obj the json_object instance |
| 447 | * @param key the object field name |
| 448 | * @param value a pointer where to store a reference to the json_object |
| 449 | * associated with the given field name. |
| 450 | * |
| 451 | * It is safe to pass a NULL value. |
| 452 | * @returns whether or not the key exists |
| 453 | */ |
| 454 | JSON_EXPORT json_bool json_object_object_get_ex(const struct json_object *obj, const char *key, |
| 455 | struct json_object **value); |
| 456 | |
| 457 | /** Delete the given json_object field |
| 458 | * |
| 459 | * The reference count will be decremented for the deleted object. If there |
| 460 | * are no more owners of the value represented by this key, then the value is |
| 461 | * freed. Otherwise, the reference to the value will remain in memory. |
| 462 | * |
| 463 | * @param obj the json_object instance |
| 464 | * @param key the object field name |
| 465 | */ |
| 466 | JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key); |
| 467 | |
| 468 | /** |
| 469 | * Iterate through all keys and values of an object. |
| 470 | * |
| 471 | * Adding keys to the object while iterating is NOT allowed. |
| 472 | * |
| 473 | * Deleting an existing key, or replacing an existing key with a |
| 474 | * new value IS allowed. |
| 475 | * |
| 476 | * @param obj the json_object instance |
| 477 | * @param key the local name for the char* key variable defined in the body |
| 478 | * @param val the local name for the json_object* object variable defined in |
| 479 | * the body |
| 480 | */ |
| 481 | #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) |
| 482 | |
| 483 | #define json_object_object_foreach(obj, key, val) \ |
| 484 | char *key = NULL; \ |
| 485 | struct json_object *val __attribute__((__unused__)) = NULL; \ |
| 486 | for (struct lh_entry *entry##key = lh_table_head(json_object_get_object(obj)), \ |
| 487 | *entry_next##key = NULL; \ |
| 488 | ({ \ |
| 489 | if (entry##key) \ |
| 490 | { \ |
| 491 | key = (char *)lh_entry_k(entry##key); \ |
| 492 | val = (struct json_object *)lh_entry_v(entry##key); \ |
| 493 | entry_next##key = lh_entry_next(entry##key); \ |
| 494 | }; \ |
| 495 | entry##key; \ |
| 496 | }); \ |
| 497 | entry##key = entry_next##key) |
| 498 | |
| 499 | #else /* ANSI C or MSC */ |
| 500 | |
| 501 | #define json_object_object_foreach(obj, key, val) \ |
| 502 | char *key = NULL; \ |
| 503 | struct json_object *val = NULL; \ |
| 504 | struct lh_entry *entry##key; \ |
| 505 | struct lh_entry *entry_next##key = NULL; \ |
| 506 | for (entry##key = lh_table_head(json_object_get_object(obj)); \ |
| 507 | (entry##key ? (key = (char *)lh_entry_k(entry##key), \ |
| 508 | val = (struct json_object *)lh_entry_v(entry##key), \ |
| 509 | entry_next##key = lh_entry_next(entry##key), entry##key) \ |
| 510 | : 0); \ |
| 511 | entry##key = entry_next##key) |
| 512 | |
| 513 | #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) && (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) */ |
| 514 | |
| 515 | /** Iterate through all keys and values of an object (ANSI C Safe) |
| 516 | * @param obj the json_object instance |
| 517 | * @param iter the object iterator, use type json_object_iter |
| 518 | */ |
| 519 | #define json_object_object_foreachC(obj, iter) \ |
| 520 | for (iter.entry = lh_table_head(json_object_get_object(obj)); \ |
| 521 | (iter.entry ? (iter.key = (char *)lh_entry_k(iter.entry), \ |
| 522 | iter.val = (struct json_object *)lh_entry_v(iter.entry), iter.entry) \ |
| 523 | : 0); \ |
| 524 | iter.entry = lh_entry_next(iter.entry)) |
| 525 | |
| 526 | /* Array type methods */ |
| 527 | |
| 528 | /** Create a new empty json_object of type json_type_array |
| 529 | * with 32 slots allocated. |
| 530 | * If you know the array size you'll need ahead of time, use |
| 531 | * json_object_new_array_ext() instead. |
| 532 | * @see json_object_new_array_ext() |
| 533 | * @see json_object_array_shrink() |
| 534 | * @returns a json_object of type json_type_array |
| 535 | */ |
| 536 | JSON_EXPORT struct json_object *json_object_new_array(void); |
| 537 | |
| 538 | /** Create a new empty json_object of type json_type_array |
| 539 | * with the desired number of slots allocated. |
| 540 | * @see json_object_array_shrink() |
| 541 | * @param initial_size the number of slots to allocate |
| 542 | * @returns a json_object of type json_type_array |
| 543 | */ |
| 544 | JSON_EXPORT struct json_object *json_object_new_array_ext(int initial_size); |
| 545 | |
| 546 | /** Get the arraylist of a json_object of type json_type_array |
| 547 | * @param obj the json_object instance |
| 548 | * @returns an arraylist |
| 549 | */ |
| 550 | JSON_EXPORT struct array_list *json_object_get_array(const struct json_object *obj); |
| 551 | |
| 552 | /** Get the length of a json_object of type json_type_array |
| 553 | * @param obj the json_object instance |
| 554 | * @returns an int |
| 555 | */ |
| 556 | JSON_EXPORT size_t json_object_array_length(const struct json_object *obj); |
| 557 | |
| 558 | /** Sorts the elements of jso of type json_type_array |
| 559 | * |
| 560 | * Pointers to the json_object pointers will be passed as the two arguments |
| 561 | * to sort_fn |
| 562 | * |
| 563 | * @param jso the json_object instance |
| 564 | * @param sort_fn a sorting function |
| 565 | */ |
| 566 | JSON_EXPORT void json_object_array_sort(struct json_object *jso, |
| 567 | int (*sort_fn)(const void *, const void *)); |
| 568 | |
| 569 | /** Binary search a sorted array for a specified key object. |
| 570 | * |
| 571 | * It depends on your compare function what's sufficient as a key. |
| 572 | * Usually you create some dummy object with the parameter compared in |
| 573 | * it, to identify the right item you're actually looking for. |
| 574 | * |
| 575 | * @see json_object_array_sort() for hints on the compare function. |
| 576 | * |
| 577 | * @param key a dummy json_object with the right key |
| 578 | * @param jso the array object we're searching |
| 579 | * @param sort_fn the sort/compare function |
| 580 | * |
| 581 | * @return the wanted json_object instance |
| 582 | */ |
| 583 | JSON_EXPORT struct json_object * |
| 584 | json_object_array_bsearch(const struct json_object *key, const struct json_object *jso, |
| 585 | int (*sort_fn)(const void *, const void *)); |
| 586 | |
| 587 | /** Add an element to the end of a json_object of type json_type_array |
| 588 | * |
| 589 | * The reference count will *not* be incremented. This is to make adding |
| 590 | * fields to objects in code more compact. If you want to retain a reference |
| 591 | * to an added object you must wrap the passed object with json_object_get |
| 592 | * |
| 593 | * @param obj the json_object instance |
| 594 | * @param val the json_object to be added |
| 595 | */ |
| 596 | JSON_EXPORT int json_object_array_add(struct json_object *obj, struct json_object *val); |
| 597 | |
| 598 | /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array) |
| 599 | * |
| 600 | * The reference count will *not* be incremented. This is to make adding |
| 601 | * fields to objects in code more compact. If you want to retain a reference |
| 602 | * to an added object you must wrap the passed object with json_object_get |
| 603 | * |
| 604 | * The reference count of a replaced object will be decremented. |
| 605 | * |
| 606 | * The array size will be automatically be expanded to the size of the |
| 607 | * index if the index is larger than the current size. |
| 608 | * |
| 609 | * @param obj the json_object instance |
| 610 | * @param idx the index to insert the element at |
| 611 | * @param val the json_object to be added |
| 612 | */ |
| 613 | JSON_EXPORT int json_object_array_put_idx(struct json_object *obj, size_t idx, |
| 614 | struct json_object *val); |
| 615 | |
| 616 | /** Get the element at specified index of array `obj` (which must be a json_object of type json_type_array) |
| 617 | * |
| 618 | * *No* reference counts will be changed, and ownership of the returned |
| 619 | * object remains with `obj`. See json_object_object_get() for additional |
| 620 | * implications of this behavior. |
| 621 | * |
| 622 | * Calling this with anything other than a json_type_array will trigger |
| 623 | * an assert. |
| 624 | * |
| 625 | * @param obj the json_object instance |
| 626 | * @param idx the index to get the element at |
| 627 | * @returns the json_object at the specified index (or NULL) |
| 628 | */ |
| 629 | JSON_EXPORT struct json_object *json_object_array_get_idx(const struct json_object *obj, |
| 630 | size_t idx); |
| 631 | |
| 632 | /** Delete an elements from a specified index in an array (a json_object of type json_type_array) |
| 633 | * |
| 634 | * The reference count will be decremented for each of the deleted objects. If there |
| 635 | * are no more owners of an element that is being deleted, then the value is |
| 636 | * freed. Otherwise, the reference to the value will remain in memory. |
| 637 | * |
| 638 | * @param obj the json_object instance |
| 639 | * @param idx the index to start deleting elements at |
| 640 | * @param count the number of elements to delete |
| 641 | * @returns 0 if the elements were successfully deleted |
| 642 | */ |
| 643 | JSON_EXPORT int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count); |
| 644 | |
| 645 | /** |
| 646 | * Shrink the internal memory allocation of the array to just |
| 647 | * enough to fit the number of elements in it, plus empty_slots. |
| 648 | * |
| 649 | * @param jso the json_object instance, must be json_type_array |
| 650 | * @param empty_slots the number of empty slots to leave allocated |
| 651 | */ |
| 652 | JSON_EXPORT int json_object_array_shrink(struct json_object *jso, int empty_slots); |
| 653 | |
| 654 | /* json_bool type methods */ |
| 655 | |
| 656 | /** Create a new empty json_object of type json_type_boolean |
| 657 | * @param b a json_bool 1 or 0 |
| 658 | * @returns a json_object of type json_type_boolean |
| 659 | */ |
| 660 | JSON_EXPORT struct json_object *json_object_new_boolean(json_bool b); |
| 661 | |
| 662 | /** Get the json_bool value of a json_object |
| 663 | * |
| 664 | * The type is coerced to a json_bool if the passed object is not a json_bool. |
| 665 | * integer and double objects will return 0 if there value is zero |
| 666 | * or 1 otherwise. If the passed object is a string it will return |
| 667 | * 1 if it has a non zero length. |
| 668 | * If any other object type is passed 0 will be returned, even non-empty |
| 669 | * json_type_array and json_type_object objects. |
| 670 | * |
| 671 | * @param obj the json_object instance |
| 672 | * @returns a json_bool |
| 673 | */ |
| 674 | JSON_EXPORT json_bool json_object_get_boolean(const struct json_object *obj); |
| 675 | |
| 676 | /** Set the json_bool value of a json_object |
| 677 | * |
| 678 | * The type of obj is checked to be a json_type_boolean and 0 is returned |
| 679 | * if it is not without any further actions. If type of obj is json_type_boolean |
| 680 | * the object value is changed to new_value |
| 681 | * |
| 682 | * @param obj the json_object instance |
| 683 | * @param new_value the value to be set |
| 684 | * @returns 1 if value is set correctly, 0 otherwise |
| 685 | */ |
| 686 | JSON_EXPORT int json_object_set_boolean(struct json_object *obj, json_bool new_value); |
| 687 | |
| 688 | /* int type methods */ |
| 689 | |
| 690 | /** Create a new empty json_object of type json_type_int |
| 691 | * Note that values are stored as 64-bit values internally. |
| 692 | * To ensure the full range is maintained, use json_object_new_int64 instead. |
| 693 | * @param i the integer |
| 694 | * @returns a json_object of type json_type_int |
| 695 | */ |
| 696 | JSON_EXPORT struct json_object *json_object_new_int(int32_t i); |
| 697 | |
| 698 | /** Create a new empty json_object of type json_type_int |
| 699 | * @param i the integer |
| 700 | * @returns a json_object of type json_type_int |
| 701 | */ |
| 702 | JSON_EXPORT struct json_object *json_object_new_int64(int64_t i); |
| 703 | |
| 704 | /** Create a new empty json_object of type json_type_uint |
| 705 | * @param i the integer |
| 706 | * @returns a json_object of type json_type_uint |
| 707 | */ |
| 708 | JSON_EXPORT struct json_object *json_object_new_uint64(uint64_t i); |
| 709 | |
| 710 | /** Get the int value of a json_object |
| 711 | * |
| 712 | * The type is coerced to a int if the passed object is not a int. |
| 713 | * double objects will return their integer conversion. Strings will be |
| 714 | * parsed as an integer. If no conversion exists then 0 is returned |
| 715 | * and errno is set to EINVAL. null is equivalent to 0 (no error values set) |
| 716 | * |
| 717 | * Note that integers are stored internally as 64-bit values. |
| 718 | * If the value of too big or too small to fit into 32-bit, INT32_MAX or |
| 719 | * INT32_MIN are returned, respectively. |
| 720 | * |
| 721 | * @param obj the json_object instance |
| 722 | * @returns an int |
| 723 | */ |
| 724 | JSON_EXPORT int32_t json_object_get_int(const struct json_object *obj); |
| 725 | |
| 726 | /** Set the int value of a json_object |
| 727 | * |
| 728 | * The type of obj is checked to be a json_type_int and 0 is returned |
| 729 | * if it is not without any further actions. If type of obj is json_type_int |
| 730 | * the object value is changed to new_value |
| 731 | * |
| 732 | * @param obj the json_object instance |
| 733 | * @param new_value the value to be set |
| 734 | * @returns 1 if value is set correctly, 0 otherwise |
| 735 | */ |
| 736 | JSON_EXPORT int json_object_set_int(struct json_object *obj, int new_value); |
| 737 | |
| 738 | /** Increment a json_type_int object by the given amount, which may be negative. |
| 739 | * |
| 740 | * If the type of obj is not json_type_int then 0 is returned with no further |
| 741 | * action taken. |
| 742 | * If the addition would result in a overflow, the object value |
| 743 | * is set to INT64_MAX. |
| 744 | * If the addition would result in a underflow, the object value |
| 745 | * is set to INT64_MIN. |
| 746 | * Neither overflow nor underflow affect the return value. |
| 747 | * |
| 748 | * @param obj the json_object instance |
| 749 | * @param val the value to add |
| 750 | * @returns 1 if the increment succeeded, 0 otherwise |
| 751 | */ |
| 752 | JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val); |
| 753 | |
| 754 | /** Get the int value of a json_object |
| 755 | * |
| 756 | * The type is coerced to a int64 if the passed object is not a int64. |
| 757 | * double objects will return their int64 conversion. Strings will be |
| 758 | * parsed as an int64. If no conversion exists then 0 is returned. |
| 759 | * |
| 760 | * NOTE: Set errno to 0 directly before a call to this function to determine |
| 761 | * whether or not conversion was successful (it does not clear the value for |
| 762 | * you). |
| 763 | * |
| 764 | * @param obj the json_object instance |
| 765 | * @returns an int64 |
| 766 | */ |
| 767 | JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj); |
| 768 | |
| 769 | /** Get the uint value of a json_object |
| 770 | * |
| 771 | * The type is coerced to a uint64 if the passed object is not a uint64. |
| 772 | * double objects will return their uint64 conversion. Strings will be |
| 773 | * parsed as an uint64. If no conversion exists then 0 is returned. |
| 774 | * |
| 775 | * NOTE: Set errno to 0 directly before a call to this function to determine |
| 776 | * whether or not conversion was successful (it does not clear the value for |
| 777 | * you). |
| 778 | * |
| 779 | * @param obj the json_object instance |
| 780 | * @returns an uint64 |
| 781 | */ |
| 782 | JSON_EXPORT uint64_t json_object_get_uint64(const struct json_object *obj); |
| 783 | |
| 784 | /** Set the int64_t value of a json_object |
| 785 | * |
| 786 | * The type of obj is checked to be a json_type_int and 0 is returned |
| 787 | * if it is not without any further actions. If type of obj is json_type_int |
| 788 | * the object value is changed to new_value |
| 789 | * |
| 790 | * @param obj the json_object instance |
| 791 | * @param new_value the value to be set |
| 792 | * @returns 1 if value is set correctly, 0 otherwise |
| 793 | */ |
| 794 | JSON_EXPORT int json_object_set_int64(struct json_object *obj, int64_t new_value); |
| 795 | |
| 796 | /** Set the uint64_t value of a json_object |
| 797 | * |
| 798 | * The type of obj is checked to be a json_type_uint and 0 is returned |
| 799 | * if it is not without any further actions. If type of obj is json_type_uint |
| 800 | * the object value is changed to new_value |
| 801 | * |
| 802 | * @param obj the json_object instance |
| 803 | * @param new_value the value to be set |
| 804 | * @returns 1 if value is set correctly, 0 otherwise |
| 805 | */ |
| 806 | JSON_EXPORT int json_object_set_uint64(struct json_object *obj, uint64_t new_value); |
| 807 | |
| 808 | /* double type methods */ |
| 809 | |
| 810 | /** Create a new empty json_object of type json_type_double |
| 811 | * |
| 812 | * @see json_object_double_to_json_string() for how to set a custom format string. |
| 813 | * |
| 814 | * @param d the double |
| 815 | * @returns a json_object of type json_type_double |
| 816 | */ |
| 817 | JSON_EXPORT struct json_object *json_object_new_double(double d); |
| 818 | |
| 819 | /** |
| 820 | * Create a new json_object of type json_type_double, using |
| 821 | * the exact serialized representation of the value. |
| 822 | * |
| 823 | * This allows for numbers that would otherwise get displayed |
| 824 | * inefficiently (e.g. 12.3 => "12.300000000000001") to be |
| 825 | * serialized with the more convenient form. |
| 826 | * |
| 827 | * Notes: |
| 828 | * |
| 829 | * This is used by json_tokener_parse_ex() to allow for |
| 830 | * an exact re-serialization of a parsed object. |
| 831 | * |
| 832 | * The userdata field is used to store the string representation, so it |
| 833 | * can't be used for other data if this function is used. |
| 834 | * |
| 835 | * A roughly equivalent sequence of calls, with the difference being that |
| 836 | * the serialization function won't be reset by json_object_set_double(), is: |
| 837 | * @code |
| 838 | * jso = json_object_new_double(d); |
| 839 | * json_object_set_serializer(jso, json_object_userdata_to_json_string, |
| 840 | * strdup(ds), json_object_free_userdata); |
| 841 | * @endcode |
| 842 | * |
| 843 | * @param d the numeric value of the double. |
| 844 | * @param ds the string representation of the double. This will be copied. |
| 845 | */ |
| 846 | JSON_EXPORT struct json_object *json_object_new_double_s(double d, const char *ds); |
| 847 | |
| 848 | /** |
| 849 | * Set a global or thread-local json-c option, depending on whether |
| 850 | * JSON_C_OPTION_GLOBAL or JSON_C_OPTION_THREAD is passed. |
| 851 | * Thread-local options default to undefined, and inherit from the global |
| 852 | * value, even if the global value is changed after the thread is created. |
| 853 | * Attempting to set thread-local options when threading is not compiled in |
| 854 | * will result in an error. Be sure to check the return value. |
| 855 | * |
| 856 | * double_format is a "%g" printf format, such as "%.20g" |
| 857 | * |
| 858 | * @return -1 on errors, 0 on success. |
| 859 | */ |
| 860 | JSON_EXPORT int json_c_set_serialization_double_format(const char *double_format, |
| 861 | int global_or_thread); |
| 862 | |
| 863 | /** Serialize a json_object of type json_type_double to a string. |
| 864 | * |
| 865 | * This function isn't meant to be called directly. Instead, you can set a |
| 866 | * custom format string for the serialization of this double using the |
| 867 | * following call (where "%.17g" actually is the default): |
| 868 | * |
| 869 | * @code |
| 870 | * jso = json_object_new_double(d); |
| 871 | * json_object_set_serializer(jso, json_object_double_to_json_string, |
| 872 | * "%.17g", NULL); |
| 873 | * @endcode |
| 874 | * |
| 875 | * @see printf(3) man page for format strings |
| 876 | * |
| 877 | * @param jso The json_type_double object that is serialized. |
| 878 | * @param pb The destination buffer. |
| 879 | * @param level Ignored. |
| 880 | * @param flags Ignored. |
| 881 | */ |
| 882 | JSON_EXPORT int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, |
| 883 | int level, int flags); |
| 884 | |
| 885 | /** Get the double floating point value of a json_object |
| 886 | * |
| 887 | * The type is coerced to a double if the passed object is not a double. |
| 888 | * integer objects will return their double conversion. Strings will be |
| 889 | * parsed as a double. If no conversion exists then 0.0 is returned and |
| 890 | * errno is set to EINVAL. null is equivalent to 0 (no error values set) |
| 891 | * |
| 892 | * If the value is too big to fit in a double, then the value is set to |
| 893 | * the closest infinity with errno set to ERANGE. If strings cannot be |
| 894 | * converted to their double value, then EINVAL is set & NaN is returned. |
| 895 | * |
| 896 | * Arrays of length 0 are interpreted as 0 (with no error flags set). |
| 897 | * Arrays of length 1 are effectively cast to the equivalent object and |
| 898 | * converted using the above rules. All other arrays set the error to |
| 899 | * EINVAL & return NaN. |
| 900 | * |
| 901 | * NOTE: Set errno to 0 directly before a call to this function to |
| 902 | * determine whether or not conversion was successful (it does not clear |
| 903 | * the value for you). |
| 904 | * |
| 905 | * @param obj the json_object instance |
| 906 | * @returns a double floating point number |
| 907 | */ |
| 908 | JSON_EXPORT double json_object_get_double(const struct json_object *obj); |
| 909 | |
| 910 | /** Set the double value of a json_object |
| 911 | * |
| 912 | * The type of obj is checked to be a json_type_double and 0 is returned |
| 913 | * if it is not without any further actions. If type of obj is json_type_double |
| 914 | * the object value is changed to new_value |
| 915 | * |
| 916 | * If the object was created with json_object_new_double_s(), the serialization |
| 917 | * function is reset to the default and the cached serialized value is cleared. |
| 918 | * |
| 919 | * @param obj the json_object instance |
| 920 | * @param new_value the value to be set |
| 921 | * @returns 1 if value is set correctly, 0 otherwise |
| 922 | */ |
| 923 | JSON_EXPORT int json_object_set_double(struct json_object *obj, double new_value); |
| 924 | |
| 925 | /* string type methods */ |
| 926 | |
| 927 | /** Create a new empty json_object of type json_type_string |
| 928 | * |
| 929 | * A copy of the string is made and the memory is managed by the json_object |
| 930 | * |
| 931 | * @param s the string |
| 932 | * @returns a json_object of type json_type_string |
| 933 | * @see json_object_new_string_len() |
| 934 | */ |
| 935 | JSON_EXPORT struct json_object *json_object_new_string(const char *s); |
| 936 | |
| 937 | /** Create a new empty json_object of type json_type_string and allocate |
| 938 | * len characters for the new string. |
| 939 | * |
| 940 | * A copy of the string is made and the memory is managed by the json_object |
| 941 | * |
| 942 | * @param s the string |
| 943 | * @param len max length of the new string |
| 944 | * @returns a json_object of type json_type_string |
| 945 | * @see json_object_new_string() |
| 946 | */ |
| 947 | JSON_EXPORT struct json_object *json_object_new_string_len(const char *s, const int len); |
| 948 | |
| 949 | /** Get the string value of a json_object |
| 950 | * |
| 951 | * If the passed object is of type json_type_null (i.e. obj == NULL), |
| 952 | * NULL is returned. |
| 953 | * |
| 954 | * If the passed object of type json_type_string, the string contents |
| 955 | * are returned. |
| 956 | * |
| 957 | * Otherwise the JSON representation of the object is returned. |
| 958 | * |
| 959 | * The returned string memory is managed by the json_object and will |
| 960 | * be freed when the reference count of the json_object drops to zero. |
| 961 | * |
| 962 | * @param obj the json_object instance |
| 963 | * @returns a string or NULL |
| 964 | */ |
| 965 | JSON_EXPORT const char *json_object_get_string(struct json_object *obj); |
| 966 | |
| 967 | /** Get the string length of a json_object |
| 968 | * |
| 969 | * If the passed object is not of type json_type_string then zero |
| 970 | * will be returned. |
| 971 | * |
| 972 | * @param obj the json_object instance |
| 973 | * @returns int |
| 974 | */ |
| 975 | JSON_EXPORT int json_object_get_string_len(const struct json_object *obj); |
| 976 | |
| 977 | /** Set the string value of a json_object with zero terminated strings |
| 978 | * equivalent to json_object_set_string_len (obj, new_value, strlen(new_value)) |
| 979 | * @returns 1 if value is set correctly, 0 otherwise |
| 980 | */ |
| 981 | JSON_EXPORT int json_object_set_string(json_object *obj, const char *new_value); |
| 982 | |
| 983 | /** Set the string value of a json_object str |
| 984 | * |
| 985 | * The type of obj is checked to be a json_type_string and 0 is returned |
| 986 | * if it is not without any further actions. If type of obj is json_type_string |
| 987 | * the object value is changed to new_value |
| 988 | * |
| 989 | * @param obj the json_object instance |
| 990 | * @param new_value the value to be set; Since string length is given in len this need not be zero terminated |
| 991 | * @param len the length of new_value |
| 992 | * @returns 1 if value is set correctly, 0 otherwise |
| 993 | */ |
| 994 | JSON_EXPORT int json_object_set_string_len(json_object *obj, const char *new_value, int len); |
| 995 | |
| 996 | /** This method exists only to provide a complementary function |
| 997 | * along the lines of the other json_object_new_* functions. |
| 998 | * It always returns NULL, and it is entirely acceptable to simply use NULL directly. |
| 999 | */ |
| 1000 | JSON_EXPORT struct json_object *json_object_new_null(void); |
| 1001 | |
| 1002 | /** Check if two json_object's are equal |
| 1003 | * |
| 1004 | * If the passed objects are equal 1 will be returned. |
| 1005 | * Equality is defined as follows: |
| 1006 | * - json_objects of different types are never equal |
| 1007 | * - json_objects of the same primitive type are equal if the |
| 1008 | * c-representation of their value is equal |
| 1009 | * - json-arrays are considered equal if all values at the same |
| 1010 | * indices are equal (same order) |
| 1011 | * - Complex json_objects are considered equal if all |
| 1012 | * contained objects referenced by their key are equal, |
| 1013 | * regardless their order. |
| 1014 | * |
| 1015 | * @param obj1 the first json_object instance |
| 1016 | * @param obj2 the second json_object instance |
| 1017 | * @returns whether both objects are equal or not |
| 1018 | */ |
| 1019 | JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *obj2); |
| 1020 | |
| 1021 | /** |
| 1022 | * Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy(). |
| 1023 | * |
| 1024 | * If src is part of a containing object or array, parent will be non-NULL, |
| 1025 | * and key or index will be provided. |
| 1026 | * When shallow_copy is called *dst will be NULL, and must be non-NULL when it returns. |
| 1027 | * src will never be NULL. |
| 1028 | * |
| 1029 | * If shallow_copy sets the serializer on an object, return 2 to indicate to |
| 1030 | * json_object_deep_copy that it should not attempt to use the standard userdata |
| 1031 | * copy function. |
| 1032 | * |
| 1033 | * @return On success 1 or 2, -1 on errors |
| 1034 | */ |
| 1035 | typedef int(json_c_shallow_copy_fn)(json_object *src, json_object *parent, const char *key, |
| 1036 | size_t index, json_object **dst); |
| 1037 | |
| 1038 | /** |
| 1039 | * The default shallow copy implementation for use with json_object_deep_copy(). |
| 1040 | * This simply calls the appropriate json_object_new_<type>() function and |
| 1041 | * copies over the serializer function (_to_json_string internal field of |
| 1042 | * the json_object structure) but not any _userdata or _user_delete values. |
| 1043 | * |
| 1044 | * If you're writing a custom shallow_copy function, perhaps because you're using |
| 1045 | * your own custom serializer, you can call this first to create the new object |
| 1046 | * before customizing it with json_object_set_serializer(). |
| 1047 | * |
| 1048 | * @return 1 on success, -1 on errors, but never 2. |
| 1049 | */ |
| 1050 | JSON_EXPORT json_c_shallow_copy_fn json_c_shallow_copy_default; |
| 1051 | |
| 1052 | /** |
| 1053 | * Copy the contents of the JSON object. |
| 1054 | * The destination object must be initialized to NULL, |
| 1055 | * to make sure this function won't overwrite an existing JSON object. |
| 1056 | * |
| 1057 | * This does roughly the same thing as |
| 1058 | * `json_tokener_parse(json_object_get_string(src))`. |
| 1059 | * |
| 1060 | * @param src source JSON object whose contents will be copied |
| 1061 | * @param dst pointer to the destination object where the contents of `src`; |
| 1062 | * make sure this pointer is initialized to NULL |
| 1063 | * @param shallow_copy an optional function to copy individual objects, needed |
| 1064 | * when custom serializers are in use. See also |
| 1065 | * json_object set_serializer. |
| 1066 | * |
| 1067 | * @returns 0 if the copy went well, -1 if an error occurred during copy |
| 1068 | * or if the destination pointer is non-NULL |
| 1069 | */ |
| 1070 | |
| 1071 | JSON_EXPORT int json_object_deep_copy(struct json_object *src, struct json_object **dst, |
| 1072 | json_c_shallow_copy_fn *shallow_copy); |
| 1073 | #ifdef __cplusplus |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
| 1077 | #endif |