|
|
|||
File indexing completed on 2025-12-16 10:19:15
0001 /* 0002 * $Id: json_object.h,v 1.12 2006/01/30 23:07:57 mclark Exp $ 0003 * 0004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. 0005 * Michael Clark <michael@metaparadigm.com> 0006 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. 0007 * 0008 * This library is free software; you can redistribute it and/or modify 0009 * it under the terms of the MIT license. See COPYING for details. 0010 * 0011 */ 0012 0013 /** 0014 * @file 0015 * @brief Core json-c API. Start here, or with json_tokener.h 0016 */ 0017 #ifndef _json_object_h_ 0018 #define _json_object_h_ 0019 0020 #ifdef __GNUC__ 0021 #define JSON_C_CONST_FUNCTION(func) func __attribute__((const)) 0022 #else 0023 #define JSON_C_CONST_FUNCTION(func) func 0024 #endif 0025 0026 #include "json_inttypes.h" 0027 #include "json_types.h" 0028 #include "printbuf.h" 0029 0030 #include <stddef.h> 0031 0032 #ifdef __cplusplus 0033 extern "C" { 0034 #endif 0035 0036 #define JSON_OBJECT_DEF_HASH_ENTRIES 16 0037 0038 /** 0039 * A flag for the json_object_to_json_string_ext() and 0040 * json_object_to_file_ext() functions which causes the output 0041 * to have no extra whitespace or formatting applied. 0042 */ 0043 #define JSON_C_TO_STRING_PLAIN 0 0044 /** 0045 * A flag for the json_object_to_json_string_ext() and 0046 * json_object_to_file_ext() functions which causes the output to have 0047 * minimal whitespace inserted to make things slightly more readable. 0048 */ 0049 #define JSON_C_TO_STRING_SPACED (1 << 0) 0050 /** 0051 * A flag for the json_object_to_json_string_ext() and 0052 * json_object_to_file_ext() functions which causes 0053 * the output to be formatted. 0054 * 0055 * See the "Two Space Tab" option at https://jsonformatter.curiousconcept.com/ 0056 * for an example of the format. 0057 */ 0058 #define JSON_C_TO_STRING_PRETTY (1 << 1) 0059 /** 0060 * A flag for the json_object_to_json_string_ext() and 0061 * json_object_to_file_ext() functions which causes 0062 * the output to be formatted. 0063 * 0064 * Instead of a "Two Space Tab" this gives a single tab character. 0065 */ 0066 #define JSON_C_TO_STRING_PRETTY_TAB (1 << 3) 0067 /** 0068 * A flag to drop trailing zero for float values 0069 */ 0070 #define JSON_C_TO_STRING_NOZERO (1 << 2) 0071 0072 /** 0073 * Don't escape forward slashes. 0074 */ 0075 #define JSON_C_TO_STRING_NOSLASHESCAPE (1 << 4) 0076 0077 /** 0078 * A flag for the json_object_to_json_string_ext() and 0079 * json_object_to_file_ext() functions which causes 0080 * the output to be formatted. 0081 * 0082 * Use color for printing json. 0083 */ 0084 #define JSON_C_TO_STRING_COLOR (1 << 5) 0085 0086 /** 0087 * A flag for the json_object_object_add_ex function which 0088 * causes the value to be added without a check if it already exists. 0089 * Note: it is the responsibility of the caller to ensure that no 0090 * key is added multiple times. If this is done, results are 0091 * unpredictable. While this option is somewhat dangerous, it 0092 * permits potentially large performance savings in code that 0093 * knows for sure the key values are unique (e.g. because the 0094 * code adds a well-known set of constant key values). 0095 */ 0096 #define JSON_C_OBJECT_ADD_KEY_IS_NEW (1 << 1) 0097 /** 0098 * A flag for the json_object_object_add_ex function which 0099 * flags the key as being constant memory. This means that 0100 * the key will NOT be copied via strdup(), resulting in a 0101 * potentially huge performance win (malloc, strdup and 0102 * free are usually performance hogs). It is acceptable to 0103 * use this flag for keys in non-constant memory blocks if 0104 * the caller ensure that the memory holding the key lives 0105 * longer than the corresponding json object. However, this 0106 * is somewhat dangerous and should only be done if really 0107 * justified. 0108 * The general use-case for this flag is cases where the 0109 * key is given as a real constant value in the function 0110 * call, e.g. as in 0111 * json_object_object_add_ex(obj, "ip", json, 0112 * JSON_C_OBJECT_ADD_CONSTANT_KEY); 0113 */ 0114 #define JSON_C_OBJECT_ADD_CONSTANT_KEY (1 << 2) 0115 /** 0116 * This flag is an alias to JSON_C_OBJECT_ADD_CONSTANT_KEY. 0117 * Historically, this flag was used first and the new name 0118 * JSON_C_OBJECT_ADD_CONSTANT_KEY was introduced for version 0119 * 0.16.00 in order to have regular naming. 0120 * Use of this flag is now legacy. 0121 */ 0122 #define JSON_C_OBJECT_KEY_IS_CONSTANT JSON_C_OBJECT_ADD_CONSTANT_KEY 0123 0124 /** 0125 * Set the global value of an option, which will apply to all 0126 * current and future threads that have not set a thread-local value. 0127 * 0128 * @see json_c_set_serialization_double_format 0129 */ 0130 #define JSON_C_OPTION_GLOBAL (0) 0131 /** 0132 * Set a thread-local value of an option, overriding the global value. 0133 * This will fail if json-c is not compiled with threading enabled, and 0134 * with the __thread specifier (or equivalent) available. 0135 * 0136 * @see json_c_set_serialization_double_format 0137 */ 0138 #define JSON_C_OPTION_THREAD (1) 0139 0140 /* reference counting functions */ 0141 0142 /** 0143 * Increment the reference count of json_object, thereby taking ownership of it. 0144 * 0145 * Cases where you might need to increase the refcount include: 0146 * - Using an object field or array index (retrieved through 0147 * `json_object_object_get()` or `json_object_array_get_idx()`) 0148 * beyond the lifetime of the parent object. 0149 * - Detaching an object field or array index from its parent object 0150 * (using `json_object_object_del()` or `json_object_array_del_idx()`) 0151 * - Sharing a json_object with multiple (not necessarily parallel) threads 0152 * of execution that all expect to free it (with `json_object_put()`) when 0153 * they're done. 0154 * 0155 * @param obj the json_object instance 0156 * @see json_object_put() 0157 * @see json_object_object_get() 0158 * @see json_object_array_get_idx() 0159 */ 0160 JSON_EXPORT struct json_object *json_object_get(struct json_object *obj); 0161 0162 /** 0163 * Decrement the reference count of json_object and free if it reaches zero. 0164 * 0165 * You must have ownership of obj prior to doing this or you will cause an 0166 * imbalance in the reference count, leading to a classic use-after-free bug. 0167 * In particular, you normally do not need to call `json_object_put()` on the 0168 * json_object returned by `json_object_object_get()` or `json_object_array_get_idx()`. 0169 * 0170 * Just like after calling `free()` on a block of memory, you must not use 0171 * `obj` after calling `json_object_put()` on it or any object that it 0172 * is a member of (unless you know you've called `json_object_get(obj)` to 0173 * explicitly increment the refcount). 0174 * 0175 * NULL may be passed, in which case this is a no-op. 0176 * 0177 * @param obj the json_object instance 0178 * @returns 1 if the object was freed, 0 if only the refcount was decremented 0179 * @see json_object_get() 0180 */ 0181 JSON_EXPORT int json_object_put(struct json_object *obj); 0182 0183 /** 0184 * Check if the json_object is of a given type 0185 * @param obj the json_object instance 0186 * @param type one of: 0187 json_type_null (i.e. obj == NULL), 0188 json_type_boolean, 0189 json_type_double, 0190 json_type_int, 0191 json_type_object, 0192 json_type_array, 0193 json_type_string 0194 * @returns 1 if the object is of the specified type, 0 otherwise 0195 */ 0196 JSON_EXPORT int json_object_is_type(const struct json_object *obj, enum json_type type); 0197 0198 /** 0199 * Get the type of the json_object. See also json_type_to_name() to turn this 0200 * into a string suitable, for instance, for logging. 0201 * 0202 * @param obj the json_object instance 0203 * @returns type being one of: 0204 json_type_null (i.e. obj == NULL), 0205 json_type_boolean, 0206 json_type_double, 0207 json_type_int, 0208 json_type_object, 0209 json_type_array, 0210 json_type_string 0211 */ 0212 JSON_EXPORT enum json_type json_object_get_type(const struct json_object *obj); 0213 0214 /** Stringify object to json format. 0215 * Equivalent to json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED) 0216 * The pointer you get is an internal of your json object. You don't 0217 * have to free it, later use of json_object_put() should be sufficient. 0218 * If you can not ensure there's no concurrent access to *obj use 0219 * strdup(). 0220 * @param obj the json_object instance 0221 * @returns a string in JSON format 0222 */ 0223 JSON_EXPORT const char *json_object_to_json_string(struct json_object *obj); 0224 0225 /** Stringify object to json format 0226 * @see json_object_to_json_string() for details on how to free string. 0227 * @param obj the json_object instance 0228 * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants 0229 * @returns a string in JSON format 0230 */ 0231 JSON_EXPORT const char *json_object_to_json_string_ext(struct json_object *obj, int flags); 0232 0233 /** Stringify object to json format 0234 * @see json_object_to_json_string() for details on how to free string. 0235 * @param obj the json_object instance 0236 * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants 0237 * @param length a pointer where, if not NULL, the length (without null) is stored 0238 * @returns a string in JSON format and the length if not NULL 0239 */ 0240 JSON_EXPORT const char *json_object_to_json_string_length(struct json_object *obj, int flags, 0241 size_t *length); 0242 0243 /** 0244 * Returns the userdata set by json_object_set_userdata() or 0245 * json_object_set_serializer() 0246 * 0247 * @param jso the object to return the userdata for 0248 */ 0249 JSON_EXPORT void *json_object_get_userdata(json_object *jso); 0250 0251 /** 0252 * Set an opaque userdata value for an object 0253 * 0254 * The userdata can be retrieved using json_object_get_userdata(). 0255 * 0256 * If custom userdata is already set on this object, any existing user_delete 0257 * function is called before the new one is set. 0258 * 0259 * The user_delete parameter is optional and may be passed as NULL, even if 0260 * the userdata parameter is non-NULL. It will be called just before the 0261 * json_object is deleted, after it's reference count goes to zero 0262 * (see json_object_put()). 0263 * If this is not provided, it is up to the caller to free the userdata at 0264 * an appropriate time. (i.e. after the json_object is deleted) 0265 * 0266 * Note: Objects created by parsing strings may have custom serializers set 0267 * which expect the userdata to contain specific data (due to use of 0268 * json_object_new_double_s()). In this case, json_object_set_serialiser() with 0269 * NULL as to_string_func should be used instead to set the userdata and reset 0270 * the serializer to its default value. 0271 * 0272 * @param jso the object to set the userdata for 0273 * @param userdata an optional opaque cookie 0274 * @param user_delete an optional function from freeing userdata 0275 */ 0276 JSON_EXPORT void json_object_set_userdata(json_object *jso, void *userdata, 0277 json_object_delete_fn *user_delete); 0278 0279 /** 0280 * Set a custom serialization function to be used when this particular object 0281 * is converted to a string by json_object_to_json_string. 0282 * 0283 * If custom userdata is already set on this object, any existing user_delete 0284 * function is called before the new one is set. 0285 * 0286 * If to_string_func is NULL the default behaviour is reset (but the userdata 0287 * and user_delete fields are still set). 0288 * 0289 * The userdata parameter is optional and may be passed as NULL. It can be used 0290 * to provide additional data for to_string_func to use. This parameter may 0291 * be NULL even if user_delete is non-NULL. 0292 * 0293 * The user_delete parameter is optional and may be passed as NULL, even if 0294 * the userdata parameter is non-NULL. It will be called just before the 0295 * json_object is deleted, after it's reference count goes to zero 0296 * (see json_object_put()). 0297 * If this is not provided, it is up to the caller to free the userdata at 0298 * an appropriate time. (i.e. after the json_object is deleted) 0299 * 0300 * Note that the userdata is the same as set by json_object_set_userdata(), so 0301 * care must be taken not to overwrite the value when both a custom serializer 0302 * and json_object_set_userdata() are used. 0303 * 0304 * @param jso the object to customize 0305 * @param to_string_func the custom serialization function 0306 * @param userdata an optional opaque cookie 0307 * @param user_delete an optional function from freeing userdata 0308 */ 0309 JSON_EXPORT void json_object_set_serializer(json_object *jso, 0310 json_object_to_json_string_fn *to_string_func, 0311 void *userdata, json_object_delete_fn *user_delete); 0312 0313 #ifdef __clang__ 0314 /* 0315 * Clang doesn't pay attention to the parameters defined in the 0316 * function typedefs used here, so turn off spurious doc warnings. 0317 * { 0318 */ 0319 #pragma clang diagnostic push 0320 #pragma clang diagnostic ignored "-Wdocumentation" 0321 #endif 0322 0323 /** 0324 * Simply call free on the userdata pointer. 0325 * Can be used with json_object_set_serializer(). 0326 * 0327 * @param jso unused 0328 * @param userdata the pointer that is passed to free(). 0329 */ 0330 JSON_EXPORT json_object_delete_fn json_object_free_userdata; 0331 0332 /** 0333 * Copy the jso->_userdata string over to pb as-is. 0334 * Can be used with json_object_set_serializer(). 0335 * 0336 * @param jso The object whose _userdata is used. 0337 * @param pb The destination buffer. 0338 * @param level Ignored. 0339 * @param flags Ignored. 0340 */ 0341 JSON_EXPORT json_object_to_json_string_fn json_object_userdata_to_json_string; 0342 0343 #ifdef __clang__ 0344 /* } */ 0345 #pragma clang diagnostic pop 0346 #endif 0347 0348 /* object type methods */ 0349 0350 /** Create a new empty object with a reference count of 1. The caller of 0351 * this object initially has sole ownership. Remember, when using 0352 * json_object_object_add or json_object_array_put_idx, ownership will 0353 * transfer to the object/array. Call json_object_get if you want to maintain 0354 * shared ownership or also add this object as a child of multiple objects or 0355 * arrays. Any ownerships you acquired but did not transfer must be released 0356 * through json_object_put. 0357 * 0358 * @returns a json_object of type json_type_object 0359 */ 0360 JSON_EXPORT struct json_object *json_object_new_object(void); 0361 0362 /** Get the hashtable of a json_object of type json_type_object 0363 * @param obj the json_object instance 0364 * @returns a linkhash 0365 */ 0366 JSON_EXPORT struct lh_table *json_object_get_object(const struct json_object *obj); 0367 0368 /** Get the size of an object in terms of the number of fields it has. 0369 * @param obj the json_object whose length to return 0370 */ 0371 JSON_EXPORT int json_object_object_length(const struct json_object *obj); 0372 0373 /** Get the sizeof (struct json_object). 0374 * @returns a size_t with the sizeof (struct json_object) 0375 */ 0376 JSON_C_CONST_FUNCTION(JSON_EXPORT size_t json_c_object_sizeof(void)); 0377 0378 /** Add an object field to a json_object of type json_type_object 0379 * 0380 * The reference count of `val` will *not* be incremented, in effect 0381 * transferring ownership that object to `obj`, and thus `val` will be 0382 * freed when `obj` is. (i.e. through `json_object_put(obj)`) 0383 * 0384 * If you want to retain a reference to the added object, independent 0385 * of the lifetime of obj, you must increment the refcount with 0386 * `json_object_get(val)` (and later release it with json_object_put()). 0387 * 0388 * Since ownership transfers to `obj`, you must make sure 0389 * that you do in fact have ownership over `val`. For instance, 0390 * json_object_new_object() will give you ownership until you transfer it, 0391 * whereas json_object_object_get() does not. 0392 * 0393 * Any previous object stored under `key` in `obj` will have its refcount 0394 * decremented, and be freed normally if that drops to zero. 0395 * 0396 * @param obj the json_object instance 0397 * @param key the object field name (a private copy will be duplicated) 0398 * @param val a json_object or NULL member to associate with the given field 0399 * 0400 * @return On success, <code>0</code> is returned. 0401 * On error, a negative value is returned. 0402 */ 0403 JSON_EXPORT int json_object_object_add(struct json_object *obj, const char *key, 0404 struct json_object *val); 0405 0406 /** Add an object field to a json_object of type json_type_object 0407 * 0408 * The semantics are identical to json_object_object_add, except that an 0409 * additional flag fields gives you more control over some detail aspects 0410 * of processing. See the description of JSON_C_OBJECT_ADD_* flags for more 0411 * details. 0412 * 0413 * @param obj the json_object instance 0414 * @param key the object field name (a private copy will be duplicated) 0415 * @param val a json_object or NULL member to associate with the given field 0416 * @param opts process-modifying options. To specify multiple options, use 0417 * (OPT1|OPT2) 0418 */ 0419 JSON_EXPORT int json_object_object_add_ex(struct json_object *obj, const char *const key, 0420 struct json_object *const val, const unsigned opts); 0421 0422 /** Get the json_object associate with a given object field. 0423 * Deprecated/discouraged: used json_object_object_get_ex instead. 0424 * 0425 * This returns NULL if the field is found but its value is null, or if 0426 * the field is not found, or if obj is not a json_type_object. If you 0427 * need to distinguish between these cases, use json_object_object_get_ex(). 0428 * 0429 * *No* reference counts will be changed. There is no need to manually adjust 0430 * reference counts through the json_object_put/json_object_get methods unless 0431 * you need to have the child (value) reference maintain a different lifetime 0432 * than the owning parent (obj). Ownership of the returned value is retained 0433 * by obj (do not do json_object_put unless you have done a json_object_get). 0434 * If you delete the value from obj (json_object_object_del) and wish to access 0435 * the returned reference afterwards, make sure you have first gotten shared 0436 * ownership through json_object_get (& don't forget to do a json_object_put 0437 * or transfer ownership to prevent a memory leak). 0438 * 0439 * @param obj the json_object instance 0440 * @param key the object field name 0441 * @returns the json_object associated with the given field name 0442 */ 0443 JSON_EXPORT struct json_object *json_object_object_get(const struct json_object *obj, 0444 const char *key); 0445 0446 /** Get the json_object associated with a given object field. 0447 * 0448 * This returns true if the key is found, false in all other cases (including 0449 * if obj isn't a json_type_object). 0450 * 0451 * *No* reference counts will be changed. There is no need to manually adjust 0452 * reference counts through the json_object_put/json_object_get methods unless 0453 * you need to have the child (value) reference maintain a different lifetime 0454 * than the owning parent (obj). Ownership of value is retained by obj. 0455 * 0456 * @param obj the json_object instance 0457 * @param key the object field name 0458 * @param value a pointer where to store a reference to the json_object 0459 * associated with the given field name. 0460 * 0461 * It is safe to pass a NULL value. 0462 * @returns 1 if the key exists, 0 otherwise 0463 */ 0464 JSON_EXPORT int json_object_object_get_ex(const struct json_object *obj, const char *key, 0465 struct json_object **value); 0466 0467 /** Delete the given json_object field 0468 * 0469 * The reference count will be decremented for the deleted object. If there 0470 * are no more owners of the value represented by this key, then the value is 0471 * freed. Otherwise, the reference to the value will remain in memory. 0472 * 0473 * @param obj the json_object instance 0474 * @param key the object field name 0475 */ 0476 JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key); 0477 0478 /** 0479 * Iterate through all keys and values of an object. 0480 * 0481 * Adding keys to the object while iterating is NOT allowed. 0482 * 0483 * Deleting an existing key, or replacing an existing key with a 0484 * new value IS allowed. 0485 * 0486 * @param obj the json_object instance 0487 * @param key the local name for the char* key variable defined in the body 0488 * @param val the local name for the json_object* object variable defined in 0489 * the body 0490 */ 0491 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) 0492 0493 #define json_object_object_foreach(obj, key, val) \ 0494 char *key = NULL; \ 0495 struct json_object *val __attribute__((__unused__)) = NULL; \ 0496 for (struct lh_entry *entry##key = lh_table_head(json_object_get_object(obj)), \ 0497 *entry_next##key = NULL; \ 0498 ({ \ 0499 if (entry##key) \ 0500 { \ 0501 key = (char *)lh_entry_k(entry##key); \ 0502 val = (struct json_object *)lh_entry_v(entry##key); \ 0503 entry_next##key = lh_entry_next(entry##key); \ 0504 }; \ 0505 entry##key; \ 0506 }); \ 0507 entry##key = entry_next##key) 0508 0509 #else /* ANSI C or MSC */ 0510 0511 #define json_object_object_foreach(obj, key, val) \ 0512 char *key = NULL; \ 0513 struct json_object *val = NULL; \ 0514 struct lh_entry *entry##key; \ 0515 struct lh_entry *entry_next##key = NULL; \ 0516 for (entry##key = lh_table_head(json_object_get_object(obj)); \ 0517 (entry##key ? (key = (char *)lh_entry_k(entry##key), \ 0518 val = (struct json_object *)lh_entry_v(entry##key), \ 0519 entry_next##key = lh_entry_next(entry##key), entry##key) \ 0520 : 0); \ 0521 entry##key = entry_next##key) 0522 0523 #endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) && (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) */ 0524 0525 /** Iterate through all keys and values of an object (ANSI C Safe) 0526 * @param obj the json_object instance 0527 * @param iter the object iterator, use type json_object_iter 0528 */ 0529 #define json_object_object_foreachC(obj, iter) \ 0530 for (iter.entry = lh_table_head(json_object_get_object(obj)); \ 0531 (iter.entry ? (iter.key = (char *)lh_entry_k(iter.entry), \ 0532 iter.val = (struct json_object *)lh_entry_v(iter.entry), iter.entry) \ 0533 : 0); \ 0534 iter.entry = lh_entry_next(iter.entry)) 0535 0536 /* Array type methods */ 0537 0538 /** Create a new empty json_object of type json_type_array 0539 * with 32 slots allocated. 0540 * If you know the array size you'll need ahead of time, use 0541 * json_object_new_array_ext() instead. 0542 * @see json_object_new_array_ext() 0543 * @see json_object_array_shrink() 0544 * @returns a json_object of type json_type_array 0545 */ 0546 JSON_EXPORT struct json_object *json_object_new_array(void); 0547 0548 /** Create a new empty json_object of type json_type_array 0549 * with the desired number of slots allocated. 0550 * @see json_object_array_shrink() 0551 * @param initial_size the number of slots to allocate 0552 * @returns a json_object of type json_type_array 0553 */ 0554 JSON_EXPORT struct json_object *json_object_new_array_ext(int initial_size); 0555 0556 /** Get the arraylist of a json_object of type json_type_array 0557 * @param obj the json_object instance 0558 * @returns an arraylist 0559 */ 0560 JSON_EXPORT struct array_list *json_object_get_array(const struct json_object *obj); 0561 0562 /** Get the length of a json_object of type json_type_array 0563 * @param obj the json_object instance 0564 * @returns the length of the array 0565 */ 0566 JSON_EXPORT size_t json_object_array_length(const struct json_object *obj); 0567 0568 /** Sorts the elements of jso of type json_type_array 0569 * 0570 * Pointers to the json_object pointers will be passed as the two arguments 0571 * to sort_fn 0572 * 0573 * @param jso the json_object instance 0574 * @param sort_fn a sorting function 0575 */ 0576 JSON_EXPORT void json_object_array_sort(struct json_object *jso, 0577 int (*sort_fn)(const void *, const void *)); 0578 0579 /** Binary search a sorted array for a specified key object. 0580 * 0581 * It depends on your compare function what's sufficient as a key. 0582 * Usually you create some dummy object with the parameter compared in 0583 * it, to identify the right item you're actually looking for. 0584 * 0585 * @see json_object_array_sort() for hints on the compare function. 0586 * 0587 * @param key a dummy json_object with the right key 0588 * @param jso the array object we're searching 0589 * @param sort_fn the sort/compare function 0590 * 0591 * @return the wanted json_object instance 0592 */ 0593 JSON_EXPORT struct json_object * 0594 json_object_array_bsearch(const struct json_object *key, const struct json_object *jso, 0595 int (*sort_fn)(const void *, const void *)); 0596 0597 /** Add an element to the end of a json_object of type json_type_array 0598 * 0599 * The reference count will *not* be incremented. This is to make adding 0600 * fields to objects in code more compact. If you want to retain a reference 0601 * to an added object you must wrap the passed object with json_object_get 0602 * 0603 * @param obj the json_object instance 0604 * @param val the json_object to be added 0605 */ 0606 JSON_EXPORT int json_object_array_add(struct json_object *obj, struct json_object *val); 0607 0608 /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array) 0609 * 0610 * The reference count will *not* be incremented. This is to make adding 0611 * fields to objects in code more compact. If you want to retain a reference 0612 * to an added object you must wrap the passed object with json_object_get 0613 * 0614 * The reference count of a replaced object will be decremented. 0615 * 0616 * The array size will be automatically be expanded to the size of the 0617 * index if the index is larger than the current size. 0618 * 0619 * @param obj the json_object instance 0620 * @param idx the index to insert the element at 0621 * @param val the json_object to be added 0622 */ 0623 JSON_EXPORT int json_object_array_put_idx(struct json_object *obj, size_t idx, 0624 struct json_object *val); 0625 0626 /** Insert an element at a specified index in an array (a json_object of type json_type_array) 0627 * 0628 * The reference count will *not* be incremented. This is to make adding 0629 * fields to objects in code more compact. If you want to retain a reference 0630 * to an added object you must wrap the passed object with json_object_get 0631 * 0632 * The array size will be automatically be expanded to the size of the 0633 * index if the index is larger than the current size. 0634 * If the index is within the existing array limits, then the element will be 0635 * inserted and all elements will be shifted. This is the only difference between 0636 * this function and json_object_array_put_idx(). 0637 * 0638 * @param obj the json_object instance 0639 * @param idx the index to insert the element at 0640 * @param val the json_object to be added 0641 */ 0642 JSON_EXPORT int json_object_array_insert_idx(struct json_object *obj, size_t idx, 0643 struct json_object *val); 0644 0645 /** Get the element at specified index of array `obj` (which must be a json_object of type json_type_array) 0646 * 0647 * *No* reference counts will be changed, and ownership of the returned 0648 * object remains with `obj`. See json_object_object_get() for additional 0649 * implications of this behavior. 0650 * 0651 * Calling this with anything other than a json_type_array will trigger 0652 * an assert. 0653 * 0654 * @param obj the json_object instance 0655 * @param idx the index to get the element at 0656 * @returns the json_object at the specified index (or NULL) 0657 */ 0658 JSON_EXPORT struct json_object *json_object_array_get_idx(const struct json_object *obj, 0659 size_t idx); 0660 0661 /** Delete an elements from a specified index in an array (a json_object of type json_type_array) 0662 * 0663 * The reference count will be decremented for each of the deleted objects. If there 0664 * are no more owners of an element that is being deleted, then the value is 0665 * freed. Otherwise, the reference to the value will remain in memory. 0666 * 0667 * @param obj the json_object instance 0668 * @param idx the index to start deleting elements at 0669 * @param count the number of elements to delete 0670 * @returns 0 if the elements were successfully deleted 0671 */ 0672 JSON_EXPORT int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count); 0673 0674 /** 0675 * Shrink the internal memory allocation of the array to just 0676 * enough to fit the number of elements in it, plus empty_slots. 0677 * 0678 * @param jso the json_object instance, must be json_type_array 0679 * @param empty_slots the number of empty slots to leave allocated 0680 */ 0681 JSON_EXPORT int json_object_array_shrink(struct json_object *jso, int empty_slots); 0682 0683 /* json_bool type methods */ 0684 0685 /** Create a new empty json_object of type json_type_boolean 0686 * @param b a json_bool 1 or 0 0687 * @returns a json_object of type json_type_boolean 0688 */ 0689 JSON_EXPORT struct json_object *json_object_new_boolean(json_bool b); 0690 0691 /** Get the json_bool value of a json_object 0692 * 0693 * The type is coerced to a json_bool if the passed object is not a json_bool. 0694 * integer and double objects will return 0 if there value is zero 0695 * or 1 otherwise. If the passed object is a string it will return 0696 * 1 if it has a non zero length. 0697 * If any other object type is passed 0 will be returned, even non-empty 0698 * json_type_array and json_type_object objects. 0699 * 0700 * @param obj the json_object instance 0701 * @returns a json_bool 0702 */ 0703 JSON_EXPORT json_bool json_object_get_boolean(const struct json_object *obj); 0704 0705 /** Set the json_bool value of a json_object 0706 * 0707 * The type of obj is checked to be a json_type_boolean and 0 is returned 0708 * if it is not without any further actions. If type of obj is json_type_boolean 0709 * the object value is changed to new_value 0710 * 0711 * @param obj the json_object instance 0712 * @param new_value the value to be set 0713 * @returns 1 if value is set correctly, 0 otherwise 0714 */ 0715 JSON_EXPORT int json_object_set_boolean(struct json_object *obj, json_bool new_value); 0716 0717 /* int type methods */ 0718 0719 /** Create a new empty json_object of type json_type_int 0720 * Note that values are stored as 64-bit values internally. 0721 * To ensure the full range is maintained, use json_object_new_int64 instead. 0722 * @param i the integer 0723 * @returns a json_object of type json_type_int 0724 */ 0725 JSON_EXPORT struct json_object *json_object_new_int(int32_t i); 0726 0727 /** Create a new empty json_object of type json_type_int 0728 * @param i the integer 0729 * @returns a json_object of type json_type_int 0730 */ 0731 JSON_EXPORT struct json_object *json_object_new_int64(int64_t i); 0732 0733 /** Create a new empty json_object of type json_type_uint 0734 * @param i the integer 0735 * @returns a json_object of type json_type_uint 0736 */ 0737 JSON_EXPORT struct json_object *json_object_new_uint64(uint64_t i); 0738 0739 /** Get the int value of a json_object 0740 * 0741 * The type is coerced to a int if the passed object is not a int. 0742 * double objects will return their integer conversion. Strings will be 0743 * parsed as an integer. If no conversion exists then 0 is returned 0744 * and errno is set to EINVAL. null is equivalent to 0 (no error values set) 0745 * 0746 * Note that integers are stored internally as 64-bit values. 0747 * If the value of too big or too small to fit into 32-bit, INT32_MAX or 0748 * INT32_MIN are returned, respectively. 0749 * 0750 * @param obj the json_object instance 0751 * @returns an int 0752 */ 0753 JSON_EXPORT int32_t json_object_get_int(const struct json_object *obj); 0754 0755 /** Set the int value of a json_object 0756 * 0757 * The type of obj is checked to be a json_type_int and 0 is returned 0758 * if it is not without any further actions. If type of obj is json_type_int 0759 * the object value is changed to new_value 0760 * 0761 * @param obj the json_object instance 0762 * @param new_value the value to be set 0763 * @returns 1 if value is set correctly, 0 otherwise 0764 */ 0765 JSON_EXPORT int json_object_set_int(struct json_object *obj, int new_value); 0766 0767 /** Increment a json_type_int object by the given amount, which may be negative. 0768 * 0769 * If the type of obj is not json_type_int then 0 is returned with no further 0770 * action taken. 0771 * If the addition would result in a overflow, the object value 0772 * is set to INT64_MAX. 0773 * If the addition would result in a underflow, the object value 0774 * is set to INT64_MIN. 0775 * Neither overflow nor underflow affect the return value. 0776 * 0777 * @param obj the json_object instance 0778 * @param val the value to add 0779 * @returns 1 if the increment succeeded, 0 otherwise 0780 */ 0781 JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val); 0782 0783 /** Get the int value of a json_object 0784 * 0785 * The type is coerced to a int64 if the passed object is not a int64. 0786 * double objects will return their int64 conversion. Strings will be 0787 * parsed as an int64. If no conversion exists then 0 is returned. 0788 * 0789 * NOTE: Set errno to 0 directly before a call to this function to determine 0790 * whether or not conversion was successful (it does not clear the value for 0791 * you). 0792 * 0793 * @param obj the json_object instance 0794 * @returns an int64 0795 */ 0796 JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj); 0797 0798 /** Get the uint value of a json_object 0799 * 0800 * The type is coerced to a uint64 if the passed object is not a uint64. 0801 * double objects will return their uint64 conversion. Strings will be 0802 * parsed as an uint64. If no conversion exists then 0 is returned. 0803 * 0804 * NOTE: Set errno to 0 directly before a call to this function to determine 0805 * whether or not conversion was successful (it does not clear the value for 0806 * you). 0807 * 0808 * @param obj the json_object instance 0809 * @returns an uint64 0810 */ 0811 JSON_EXPORT uint64_t json_object_get_uint64(const struct json_object *obj); 0812 0813 /** Set the int64_t value of a json_object 0814 * 0815 * The type of obj is checked to be a json_type_int and 0 is returned 0816 * if it is not without any further actions. If type of obj is json_type_int 0817 * the object value is changed to new_value 0818 * 0819 * @param obj the json_object instance 0820 * @param new_value the value to be set 0821 * @returns 1 if value is set correctly, 0 otherwise 0822 */ 0823 JSON_EXPORT int json_object_set_int64(struct json_object *obj, int64_t new_value); 0824 0825 /** Set the uint64_t value of a json_object 0826 * 0827 * The type of obj is checked to be a json_type_uint and 0 is returned 0828 * if it is not without any further actions. If type of obj is json_type_uint 0829 * the object value is changed to new_value 0830 * 0831 * @param obj the json_object instance 0832 * @param new_value the value to be set 0833 * @returns 1 if value is set correctly, 0 otherwise 0834 */ 0835 JSON_EXPORT int json_object_set_uint64(struct json_object *obj, uint64_t new_value); 0836 0837 /* double type methods */ 0838 0839 /** Create a new empty json_object of type json_type_double 0840 * 0841 * @see json_object_double_to_json_string() for how to set a custom format string. 0842 * 0843 * @param d the double 0844 * @returns a json_object of type json_type_double 0845 */ 0846 JSON_EXPORT struct json_object *json_object_new_double(double d); 0847 0848 /** 0849 * Create a new json_object of type json_type_double, using 0850 * the exact serialized representation of the value. 0851 * 0852 * This allows for numbers that would otherwise get displayed 0853 * inefficiently (e.g. 12.3 => "12.300000000000001") to be 0854 * serialized with the more convenient form. 0855 * 0856 * Notes: 0857 * 0858 * This is used by json_tokener_parse_ex() to allow for 0859 * an exact re-serialization of a parsed object. 0860 * 0861 * The userdata field is used to store the string representation, so it 0862 * can't be used for other data if this function is used. 0863 * 0864 * A roughly equivalent sequence of calls, with the difference being that 0865 * the serialization function won't be reset by json_object_set_double(), is: 0866 * @code 0867 * jso = json_object_new_double(d); 0868 * json_object_set_serializer(jso, json_object_userdata_to_json_string, 0869 * strdup(ds), json_object_free_userdata); 0870 * @endcode 0871 * 0872 * @param d the numeric value of the double. 0873 * @param ds the string representation of the double. This will be copied. 0874 */ 0875 JSON_EXPORT struct json_object *json_object_new_double_s(double d, const char *ds); 0876 0877 /** 0878 * Set a global or thread-local json-c option, depending on whether 0879 * JSON_C_OPTION_GLOBAL or JSON_C_OPTION_THREAD is passed. 0880 * Thread-local options default to undefined, and inherit from the global 0881 * value, even if the global value is changed after the thread is created. 0882 * Attempting to set thread-local options when threading is not compiled in 0883 * will result in an error. Be sure to check the return value. 0884 * 0885 * double_format is a "%g" printf format, such as "%.20g" 0886 * 0887 * @return -1 on errors, 0 on success. 0888 */ 0889 JSON_EXPORT int json_c_set_serialization_double_format(const char *double_format, 0890 int global_or_thread); 0891 0892 /** Serialize a json_object of type json_type_double to a string. 0893 * 0894 * This function isn't meant to be called directly. Instead, you can set a 0895 * custom format string for the serialization of this double using the 0896 * following call (where "%.17g" actually is the default): 0897 * 0898 * @code 0899 * jso = json_object_new_double(d); 0900 * json_object_set_serializer(jso, json_object_double_to_json_string, 0901 * "%.17g", NULL); 0902 * @endcode 0903 * 0904 * @see printf(3) man page for format strings 0905 * 0906 * @param jso The json_type_double object that is serialized. 0907 * @param pb The destination buffer. 0908 * @param level Ignored. 0909 * @param flags Ignored. 0910 */ 0911 JSON_EXPORT int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, 0912 int level, int flags); 0913 0914 /** Get the double floating point value of a json_object 0915 * 0916 * The type is coerced to a double if the passed object is not a double. 0917 * integer objects will return their double conversion. Strings will be 0918 * parsed as a double. If no conversion exists then 0.0 is returned and 0919 * errno is set to EINVAL. null is equivalent to 0 (no error values set) 0920 * 0921 * If the value is too big to fit in a double, then the value is set to 0922 * the closest infinity with errno set to ERANGE. If strings cannot be 0923 * converted to their double value, then EINVAL is set & NaN is returned. 0924 * 0925 * Arrays of length 0 are interpreted as 0 (with no error flags set). 0926 * Arrays of length 1 are effectively cast to the equivalent object and 0927 * converted using the above rules. All other arrays set the error to 0928 * EINVAL & return NaN. 0929 * 0930 * NOTE: Set errno to 0 directly before a call to this function to 0931 * determine whether or not conversion was successful (it does not clear 0932 * the value for you). 0933 * 0934 * @param obj the json_object instance 0935 * @returns a double floating point number 0936 */ 0937 JSON_EXPORT double json_object_get_double(const struct json_object *obj); 0938 0939 /** Set the double value of a json_object 0940 * 0941 * The type of obj is checked to be a json_type_double and 0 is returned 0942 * if it is not without any further actions. If type of obj is json_type_double 0943 * the object value is changed to new_value 0944 * 0945 * If the object was created with json_object_new_double_s(), the serialization 0946 * function is reset to the default and the cached serialized value is cleared. 0947 * 0948 * @param obj the json_object instance 0949 * @param new_value the value to be set 0950 * @returns 1 if value is set correctly, 0 otherwise 0951 */ 0952 JSON_EXPORT int json_object_set_double(struct json_object *obj, double new_value); 0953 0954 /* string type methods */ 0955 0956 /** Create a new empty json_object of type json_type_string 0957 * 0958 * A copy of the string is made and the memory is managed by the json_object 0959 * 0960 * @param s the string 0961 * @returns a json_object of type json_type_string 0962 * @see json_object_new_string_len() 0963 */ 0964 JSON_EXPORT struct json_object *json_object_new_string(const char *s); 0965 0966 /** Create a new empty json_object of type json_type_string and allocate 0967 * len characters for the new string. 0968 * 0969 * A copy of the string is made and the memory is managed by the json_object 0970 * 0971 * @param s the string 0972 * @param len max length of the new string 0973 * @returns a json_object of type json_type_string 0974 * @see json_object_new_string() 0975 */ 0976 JSON_EXPORT struct json_object *json_object_new_string_len(const char *s, const int len); 0977 0978 /** Get the string value of a json_object 0979 * 0980 * If the passed object is of type json_type_null (i.e. obj == NULL), 0981 * NULL is returned. 0982 * 0983 * If the passed object of type json_type_string, the string contents 0984 * are returned. 0985 * 0986 * Otherwise the JSON representation of the object is returned. 0987 * 0988 * The returned string memory is managed by the json_object and will 0989 * be freed when the reference count of the json_object drops to zero. 0990 * 0991 * @param obj the json_object instance 0992 * @returns a string or NULL 0993 */ 0994 JSON_EXPORT const char *json_object_get_string(struct json_object *obj); 0995 0996 /** Get the string length of a json_object 0997 * 0998 * If the passed object is not of type json_type_string then zero 0999 * will be returned. 1000 * 1001 * @param obj the json_object instance 1002 * @returns int 1003 */ 1004 JSON_EXPORT int json_object_get_string_len(const struct json_object *obj); 1005 1006 /** Set the string value of a json_object with zero terminated strings 1007 * equivalent to json_object_set_string_len (obj, new_value, strlen(new_value)) 1008 * @returns 1 if value is set correctly, 0 otherwise 1009 */ 1010 JSON_EXPORT int json_object_set_string(json_object *obj, const char *new_value); 1011 1012 /** Set the string value of a json_object str 1013 * 1014 * The type of obj is checked to be a json_type_string and 0 is returned 1015 * if it is not without any further actions. If type of obj is json_type_string 1016 * the object value is changed to new_value 1017 * 1018 * @param obj the json_object instance 1019 * @param new_value the value to be set; Since string length is given in len this need not be zero terminated 1020 * @param len the length of new_value 1021 * @returns 1 if value is set correctly, 0 otherwise 1022 */ 1023 JSON_EXPORT int json_object_set_string_len(json_object *obj, const char *new_value, int len); 1024 1025 /** This method exists only to provide a complementary function 1026 * along the lines of the other json_object_new_* functions. 1027 * It always returns NULL, and it is entirely acceptable to simply use NULL directly. 1028 */ 1029 JSON_EXPORT struct json_object *json_object_new_null(void); 1030 1031 /** Check if two json_object's are equal 1032 * 1033 * If the passed objects are equal 1 will be returned. 1034 * Equality is defined as follows: 1035 * - json_objects of different types are never equal 1036 * - json_objects of the same primitive type are equal if the 1037 * c-representation of their value is equal 1038 * - json-arrays are considered equal if all values at the same 1039 * indices are equal (same order) 1040 * - Complex json_objects are considered equal if all 1041 * contained objects referenced by their key are equal, 1042 * regardless their order. 1043 * 1044 * @param obj1 the first json_object instance 1045 * @param obj2 the second json_object instance 1046 * @returns 1 if both objects are equal, 0 otherwise 1047 */ 1048 JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *obj2); 1049 1050 /** 1051 * Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy(). 1052 * 1053 * If src is part of a containing object or array, parent will be non-NULL, 1054 * and key or index will be provided. 1055 * When shallow_copy is called *dst will be NULL, and must be non-NULL when it returns. 1056 * src will never be NULL. 1057 * 1058 * If shallow_copy sets the serializer on an object, return 2 to indicate to 1059 * json_object_deep_copy that it should not attempt to use the standard userdata 1060 * copy function. 1061 * 1062 * @return On success 1 or 2, -1 on errors 1063 */ 1064 typedef int(json_c_shallow_copy_fn)(json_object *src, json_object *parent, const char *key, 1065 size_t index, json_object **dst); 1066 1067 /** 1068 * The default shallow copy implementation for use with json_object_deep_copy(). 1069 * This simply calls the appropriate json_object_new_<type>() function and 1070 * copies over the serializer function (_to_json_string internal field of 1071 * the json_object structure) but not any _userdata or _user_delete values. 1072 * 1073 * If you're writing a custom shallow_copy function, perhaps because you're using 1074 * your own custom serializer, you can call this first to create the new object 1075 * before customizing it with json_object_set_serializer(). 1076 * 1077 * @return 1 on success, -1 on errors, but never 2. 1078 */ 1079 JSON_EXPORT json_c_shallow_copy_fn json_c_shallow_copy_default; 1080 1081 /** 1082 * Copy the contents of the JSON object. 1083 * The destination object must be initialized to NULL, 1084 * to make sure this function won't overwrite an existing JSON object. 1085 * 1086 * This does roughly the same thing as 1087 * `json_tokener_parse(json_object_get_string(src))`. 1088 * 1089 * @param src source JSON object whose contents will be copied 1090 * @param dst pointer to the destination object where the contents of `src`; 1091 * make sure this pointer is initialized to NULL 1092 * @param shallow_copy an optional function to copy individual objects, needed 1093 * when custom serializers are in use. See also 1094 * json_object set_serializer. 1095 * 1096 * @returns 0 if the copy went well, -1 if an error occurred during copy 1097 * or if the destination pointer is non-NULL 1098 */ 1099 1100 JSON_EXPORT int json_object_deep_copy(struct json_object *src, struct json_object **dst, 1101 json_c_shallow_copy_fn *shallow_copy); 1102 #ifdef __cplusplus 1103 } 1104 #endif 1105 1106 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|