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