1 /* 2 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. 3 * Michael Clark <michael@metaparadigm.com> 4 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. 5 * Copyright (c) 2015-2016 Rainer Gerhards 6 * 7 * This library is free software; you can redistribute it and/or modify 8 * it under the terms of the MIT license. See COPYING for details. 9 * 10 */ 11 12 #ifndef _fj_json_object_h_ 13 #define _fj_json_object_h_ 14 15 #ifdef __GNUC__ 16 #define THIS_FUNCTION_IS_DEPRECATED(func) func __attribute__ ((deprecated)) 17 #else 18 #define THIS_FUNCTION_IS_DEPRECATED(func) func 19 #endif 20 21 #include <stdint.h> 22 #include <inttypes.h> 23 #include <stdlib.h> 24 #include <stdio.h> 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #define FJSON_OBJECT_DEF_HASH_ENTRIES 16 31 /* number of subjects within a children page. One page is allocated 32 * with each json object, and extensions are alwas done in page 33 * size increments. The size should be a compromise between not 34 * wasting too much space but also not doing too frequent mallocs. 35 * note: each page *entry* currently needs ~20 Bytes (x64). If this 36 * is important, check the actual number (sizeof(struct _fjson_child)). 37 */ 38 #define FJSON_OBJECT_CHLD_PG_SIZE 8 39 40 /** 41 * A flag for the fjson_object_to_json_string_ext() and 42 * fjson_object_to_file_ext() functions which causes the output 43 * to have no extra whitespace or formatting applied. 44 */ 45 #define FJSON_TO_STRING_PLAIN 0 46 /** 47 * A flag for the fjson_object_to_json_string_ext() and 48 * fjson_object_to_file_ext() functions which causes the output to have 49 * minimal whitespace inserted to make things slightly more readable. 50 */ 51 #define FJSON_TO_STRING_SPACED (1<<0) 52 /** 53 * A flag for the fjson_object_to_json_string_ext() and 54 * fjson_object_to_file_ext() functions which causes 55 * the output to be formatted. 56 * 57 * See the "Two Space Tab" option at http://jsonformatter.curiousconcept.com/ 58 * for an example of the format. 59 */ 60 #define FJSON_TO_STRING_PRETTY (1<<1) 61 /** 62 * A flag for the fjson_object_to_json_string_ext() and 63 * fjson_object_to_file_ext() functions which causes 64 * the output to be formatted. 65 * 66 * Instead of a "Two Space Tab" this gives a single tab character. 67 */ 68 #define FJSON_TO_STRING_PRETTY_TAB (1<<3) 69 /** 70 * A flag to drop trailing zero for float values 71 */ 72 #define FJSON_TO_STRING_NOZERO (1<<2) 73 74 /** 75 * A flag for the fjson_object_object_add_ex function which 76 * causes the value to be added without a check if it already exists. 77 * Note: it is the responsibilty of the caller to ensure that no 78 * key is added multiple times. If this is done, results are 79 * unpredictable. While this option is somewhat dangerous, it 80 * permits potentially large performance savings in code that 81 * knows for sure the key values are unique (e.g. because the 82 * code adds a well-known set of constant key values). 83 */ 84 #define FJSON_OBJECT_ADD_KEY_IS_NEW (1<<1) 85 /** 86 * A flag for the fjson_object_object_add_ex function which 87 * flags the key as being constant memory. This means that 88 * the key will NOT be copied via strdup(), resulting in a 89 * potentially huge performance win (malloc, strdup and 90 * free are usually performance hogs). It is acceptable to 91 * use this flag for keys in non-constant memory blocks if 92 * the caller ensure that the memory holding the key lives 93 * longer than the corresponding json object. However, this 94 * is somewhat dangerous and should only be done if really 95 * justified. 96 * The general use-case for this flag is cases where the 97 * key is given as a real constant value in the function 98 * call, e.g. as in 99 * fjson_object_object_add_ex(obj, "ip", json, 100 * FJSON_OBJECT_KEY_IS_CONSTANT); 101 */ 102 #define FJSON_OBJECT_KEY_IS_CONSTANT (1<<2) 103 104 #undef FALSE 105 #define FALSE ((fjson_bool)0) 106 107 #undef TRUE 108 #define TRUE ((fjson_bool)1) 109 110 extern const char *fjson_number_chars; 111 extern const char *fjson_hex_chars; 112 113 /* CAW: added for ANSI C iteration correctness */ 114 struct fjson_object_iter 115 { 116 char *key; 117 struct fjson_object *val; 118 struct lh_entry *entry; 119 }; 120 121 /* forward structure definitions */ 122 123 typedef int fjson_bool; 124 typedef struct printbuf printbuf; 125 typedef struct lh_table lh_table; 126 typedef struct array_list array_list; 127 typedef struct fjson_object fjson_object; 128 typedef struct fjson_object_iter fjson_object_iter; 129 typedef struct fjson_tokener fjson_tokener; 130 131 /** 132 * Type for a user-supplied write function 133 */ 134 typedef size_t (fjson_write_fn)(void *ptr, const char *buffer, size_t size); 135 136 /* supported object types */ 137 138 typedef enum fjson_type { 139 /* If you change this, be sure to update fjson_type_to_name() too */ 140 fjson_type_null, 141 fjson_type_boolean, 142 fjson_type_double, 143 fjson_type_int, 144 fjson_type_object, 145 fjson_type_array, 146 fjson_type_string 147 } fjson_type; 148 149 /* reference counting functions */ 150 151 /** 152 * Increment the reference count of fjson_object, thereby grabbing shared 153 * ownership of obj. 154 * 155 * @param obj the fjson_object instance 156 */ 157 extern struct fjson_object* fjson_object_get(struct fjson_object *obj); 158 159 /** 160 * Decrement the reference count of fjson_object and free if it reaches zero. 161 * You must have ownership of obj prior to doing this or you will cause an 162 * imbalance in the reference count. 163 * 164 * @param obj the fjson_object instance 165 * @returns 1 if the object was freed. 166 */ 167 int fjson_object_put(struct fjson_object *obj); 168 169 /** 170 * Check if the fjson_object is of a given type 171 * @param obj the fjson_object instance 172 * @param type one of: 173 fjson_type_null (i.e. obj == NULL), 174 fjson_type_boolean, 175 fjson_type_double, 176 fjson_type_int, 177 fjson_type_object, 178 fjson_type_array, 179 fjson_type_string 180 */ 181 extern int fjson_object_is_type(struct fjson_object *obj, enum fjson_type type); 182 183 /** 184 * Get the type of the fjson_object. See also fjson_type_to_name() to turn this 185 * into a string suitable, for instance, for logging. 186 * 187 * @param obj the fjson_object instance 188 * @returns type being one of: 189 fjson_type_null (i.e. obj == NULL), 190 fjson_type_boolean, 191 fjson_type_double, 192 fjson_type_int, 193 fjson_type_object, 194 fjson_type_array, 195 fjson_type_string 196 */ 197 extern enum fjson_type fjson_object_get_type(struct fjson_object *obj); 198 199 /** 200 * Get the size of the json string if it was dumped 201 * @param obj object to calculate the size of 202 * @returns the size of the json string 203 */ 204 extern size_t fjson_object_size(struct fjson_object *obj); 205 206 /** 207 * Extended version of the above function that accept a flags parameter identical 208 * to the fjson_object_dump_ext() function that you can use the specify how to 209 * format the string for which the size is calculated 210 * @param obj the object to calculate the size of 211 * @param flags extra flags 212 * @return size_t 213 */ 214 extern size_t fjson_object_size_ext(struct fjson_object *obj, int flags); 215 216 /** 217 * Dump object to a user-supplied function. 218 * Equivalent to fjson_object_write_ext(obj, FJSON_TO_STRING_SPACED, func, ptr) 219 * @param obj object to be written 220 * @param func your function that will be called to write the data 221 * @param ptr pointer that will be passed as first argument to your function 222 * @returns number of bytes written (the sum of all return values of calls to func) 223 */ 224 extern size_t fjson_object_dump(struct fjson_object *obj, fjson_write_fn *func, void *ptr); 225 226 /** 227 * Extended dump function that allows passing extra option. You can use all 228 * FJSON_TO_STRING_* constants for the flags 229 * @param obj object to be written 230 * @param flags extra flags 231 * @param func your function that will be called to write the data 232 * @param ptr pointer that will be passed as first argument to your function 233 * @returns number of bytes written (the sum of all return values of calls to func) 234 */ 235 extern size_t fjson_object_dump_ext(struct fjson_object *obj, int flags, fjson_write_fn *func, void *ptr); 236 237 /** 238 * Dump function that uses a user-supplied temporary buffer for dumping the 239 * json. Both the above declared fjson_object_dump() and fjson_object_dump_ext() 240 * functions uses an internal buffer of 128 bytes that is first filled before 241 * the user-supplied function is called. This buffer prevents that many calls 242 * to the callback function are done for single quotes, comma's and curly 243 * braces. All these calls are first buffered and grouped into a single call 244 * to the user space function. However, since the buffer limit is somewhat 245 * arbitrary, you can also use this fjson_object_dump_buffered() function to 246 * use your own temporary buffer. Note that the buffer might be completely 247 * overwritten during the call to this function, and that the contents of the 248 * buffer are undefined after the call. 249 * @param obj object to be written 250 * @param flags extra flags 251 * @param temp your temporary buffer that is used to group calls 252 * @param size size of your temporary buffer 253 * @param func your function that will be called to write the data 254 * @param ptr pointer that will be passed as first argument to your function 255 */ 256 extern size_t fjson_object_dump_buffered(struct fjson_object *obj, int flags, char *temp, 257 size_t size, fjson_write_fn *func, void *ptr); 258 259 /** 260 * Write the json tree to a file 261 * Equivalent to fjson_object_write_ext(obj, FJSON_TO_STRING_SPACED, fp) 262 * @param obj object to be written 263 * @param fp file-pointer to which output will be written 264 * @returns number of bytes written 265 */ 266 extern size_t fjson_object_write(struct fjson_object *obj, FILE *fp); 267 268 /** 269 * Extended write function that allows flags to be passed 270 * @param obj object to be written 271 * @param flags extra flags 272 * @param fp file-pointer to which output will be written 273 * @returns number of bytes written 274 */ 275 extern size_t fjson_object_write_ext(struct fjson_object *obj, int flags, FILE *fp); 276 277 /** Stringify object to json format. 278 * Equivalent to fjson_object_to_json_string_ext(obj, FJSON_TO_STRING_SPACED) 279 * The pointer you get is an internal of your json object. You don't 280 * have to free it, later use of fjson_object_put() should be sufficient. 281 * If you can not ensure there's no concurrent access to *obj use 282 * strdup(). 283 * @param obj the fjson_object instance 284 * @returns a string in JSON format 285 */ 286 extern const char* fjson_object_to_json_string(struct fjson_object *obj); 287 288 /** Stringify object to json format 289 * @see fjson_object_to_json_string() for details on how to free string. 290 * @param obj the fjson_object instance 291 * @param flags formatting options, see FJSON_TO_STRING_PRETTY and other constants 292 * @returns a string in JSON format 293 */ 294 extern const char* fjson_object_to_json_string_ext(struct fjson_object *obj, int 295 flags); 296 297 298 /* object type methods */ 299 300 /** Create a new empty object with a reference count of 1. The caller of 301 * this object initially has sole ownership. Remember, when using 302 * fjson_object_object_add or fjson_object_array_put_idx, ownership will 303 * transfer to the object/array. Call fjson_object_get if you want to maintain 304 * shared ownership or also add this object as a child of multiple objects or 305 * arrays. Any ownerships you acquired but did not transfer must be released 306 * through fjson_object_put. 307 * 308 * @returns a fjson_object of type fjson_type_object 309 */ 310 extern struct fjson_object* fjson_object_new_object(void); 311 312 /** Get the size of an object in terms of the number of fields it has. 313 * @param obj the fjson_object whose length to return 314 */ 315 extern int fjson_object_object_length(struct fjson_object* obj); 316 317 /** Add an object field to a fjson_object of type fjson_type_object 318 * 319 * The reference count will *not* be incremented. This is to make adding 320 * fields to objects in code more compact. If you want to retain a reference 321 * to an added object, independent of the lifetime of obj, you must wrap the 322 * passed object with fjson_object_get. 323 * 324 * Upon calling this, the ownership of val transfers to obj. Thus you must 325 * make sure that you do in fact have ownership over this object. For instance, 326 * fjson_object_new_object will give you ownership until you transfer it, 327 * whereas fjson_object_object_get does not. 328 * 329 * @param obj the fjson_object instance 330 * @param key the object field name (a private copy will be duplicated) 331 * @param val a fjson_object or NULL member to associate with the given field 332 */ 333 extern void fjson_object_object_add(struct fjson_object* obj, const char *key, 334 struct fjson_object *val); 335 336 /** Add an object field to a fjson_object of type fjson_type_object 337 * 338 * The semantics are identical to fjson_object_object_add, except that an 339 * additional flag fields gives you more control over some detail aspects 340 * of processing. See the description of FJSON_OBJECT_ADD_* flags for more 341 * details. 342 * 343 * @param obj the fjson_object instance 344 * @param key the object field name (a private copy will be duplicated) 345 * @param val a fjson_object or NULL member to associate with the given field 346 * @param opts process-modifying options. To specify multiple options, use 347 * arithmetic or (OPT1|OPT2) 348 */ 349 extern void fjson_object_object_add_ex(struct fjson_object* obj, const char *key, 350 struct fjson_object *val, const unsigned opts); 351 352 /** Get the fjson_object associate with a given object field 353 * 354 * *No* reference counts will be changed. There is no need to manually adjust 355 * reference counts through the fjson_object_put/fjson_object_get methods unless 356 * you need to have the child (value) reference maintain a different lifetime 357 * than the owning parent (obj). Ownership of the returned value is retained 358 * by obj (do not do fjson_object_put unless you have done a fjson_object_get). 359 * If you delete the value from obj (fjson_object_object_del) and wish to access 360 * the returned reference afterwards, make sure you have first gotten shared 361 * ownership through fjson_object_get (& don't forget to do a fjson_object_put 362 * or transfer ownership to prevent a memory leak). 363 * 364 * @param obj the fjson_object instance 365 * @param key the object field name 366 * @returns the fjson_object associated with the given field name 367 * @deprecated Please use fjson_object_object_get_ex 368 */ 369 THIS_FUNCTION_IS_DEPRECATED(extern struct fjson_object* fjson_object_object_get(struct fjson_object* obj, 370 const char *key)); 371 372 /** Get the fjson_object associated with a given object field. 373 * 374 * This returns true if the key is found, false in all other cases (including 375 * if obj isn't a fjson_type_object). 376 * 377 * *No* reference counts will be changed. There is no need to manually adjust 378 * reference counts through the fjson_object_put/fjson_object_get methods unless 379 * you need to have the child (value) reference maintain a different lifetime 380 * than the owning parent (obj). Ownership of value is retained by obj. 381 * 382 * @param obj the fjson_object instance 383 * @param key the object field name 384 * @param value a pointer where to store a reference to the fjson_object 385 * associated with the given field name. 386 * 387 * It is safe to pass a NULL value. 388 * @returns whether or not the key exists 389 */ 390 extern fjson_bool fjson_object_object_get_ex(struct fjson_object* obj, 391 const char *key, 392 struct fjson_object **value); 393 394 /** Delete the given fjson_object field 395 * 396 * The reference count will be decremented for the deleted object. If there 397 * are no more owners of the value represented by this key, then the value is 398 * freed. Otherwise, the reference to the value will remain in memory. 399 * 400 * @param obj the fjson_object instance 401 * @param key the object field name 402 */ 403 extern void fjson_object_object_del(struct fjson_object* obj, const char *key); 404 405 406 /* Array type methods */ 407 408 /** Create a new empty fjson_object of type fjson_type_array 409 * @returns a fjson_object of type fjson_type_array 410 */ 411 extern struct fjson_object* fjson_object_new_array(void); 412 413 /** Get the arraylist of a fjson_object of type fjson_type_array 414 * @param obj the fjson_object instance 415 * @returns an arraylist 416 */ 417 extern struct array_list* fjson_object_get_array(struct fjson_object *obj); 418 419 /** Get the length of a fjson_object of type fjson_type_array 420 * @param obj the fjson_object instance 421 * @returns an int 422 */ 423 extern int fjson_object_array_length(struct fjson_object *obj); 424 425 /** Sorts the elements of jso of type fjson_type_array 426 * 427 * Pointers to the fjson_object pointers will be passed as the two arguments 428 * to @sort_fn 429 * 430 * @param obj the fjson_object instance 431 * @param sort_fn a sorting function 432 */ 433 extern void fjson_object_array_sort(struct fjson_object *jso, int(*sort_fn)(const void *, const void *)); 434 435 /** Binary search a sorted array for a specified key object. 436 * 437 * It depends on your compare function what's sufficient as a key. 438 * Usually you create some dummy object with the parameter compared in 439 * it, to identify the right item you're actually looking for. 440 * 441 * @see fjson_object_array_sort() for hints on the compare function. 442 * 443 * @param key a dummy fjson_object with the right key 444 * @param jso the array object we're searching 445 * @param sort_fn the sort/compare function 446 * 447 * @return the wanted fjson_object instance 448 */ 449 extern struct fjson_object* fjson_object_array_bsearch( 450 const struct fjson_object *key, 451 const struct fjson_object *jso, 452 int (*sort_fn)(const void *, const void *)); 453 454 /** Add an element to the end of a fjson_object of type fjson_type_array 455 * 456 * The reference count will *not* be incremented. This is to make adding 457 * fields to objects in code more compact. If you want to retain a reference 458 * to an added object you must wrap the passed object with fjson_object_get 459 * 460 * @param obj the fjson_object instance 461 * @param val the fjson_object to be added 462 */ 463 extern int fjson_object_array_add(struct fjson_object *obj, 464 struct fjson_object *val); 465 466 /** Insert or replace an element at a specified index in an array (a fjson_object of type fjson_type_array) 467 * 468 * The reference count will *not* be incremented. This is to make adding 469 * fields to objects in code more compact. If you want to retain a reference 470 * to an added object you must wrap the passed object with fjson_object_get 471 * 472 * The reference count of a replaced object will be decremented. 473 * 474 * The array size will be automatically be expanded to the size of the 475 * index if the index is larger than the current size. 476 * 477 * @param obj the fjson_object instance 478 * @param idx the index to insert the element at 479 * @param val the fjson_object to be added 480 */ 481 extern int fjson_object_array_put_idx(struct fjson_object *obj, int idx, 482 struct fjson_object *val); 483 484 /** Get the element at specificed index of the array (a fjson_object of type fjson_type_array) 485 * @param obj the fjson_object instance 486 * @param idx the index to get the element at 487 * @returns the fjson_object at the specified index (or NULL) 488 */ 489 extern struct fjson_object* fjson_object_array_get_idx(struct fjson_object *obj, 490 int idx); 491 492 extern void fjson_object_array_del_idx(struct fjson_object *jso, int idx); 493 494 /* fjson_bool type methods */ 495 496 /** Create a new empty fjson_object of type fjson_type_boolean 497 * @param b a fjson_bool TRUE or FALSE (1 or 0) 498 * @returns a fjson_object of type fjson_type_boolean 499 */ 500 extern struct fjson_object* fjson_object_new_boolean(fjson_bool b); 501 502 /** Get the fjson_bool value of a fjson_object 503 * 504 * The type is coerced to a fjson_bool if the passed object is not a fjson_bool. 505 * integer and double objects will return FALSE if there value is zero 506 * or TRUE otherwise. If the passed object is a string it will return 507 * TRUE if it has a non zero length. If any other object type is passed 508 * TRUE will be returned if the object is not NULL. 509 * 510 * @param obj the fjson_object instance 511 * @returns a fjson_bool 512 */ 513 extern fjson_bool fjson_object_get_boolean(struct fjson_object *obj); 514 515 516 /* int type methods */ 517 518 /** Create a new empty fjson_object of type fjson_type_int 519 * Note that values are stored as 64-bit values internally. 520 * To ensure the full range is maintained, use fjson_object_new_int64 instead. 521 * @param i the integer 522 * @returns a fjson_object of type fjson_type_int 523 */ 524 extern struct fjson_object* fjson_object_new_int(int32_t i); 525 526 527 /** Create a new empty fjson_object of type fjson_type_int 528 * @param i the integer 529 * @returns a fjson_object of type fjson_type_int 530 */ 531 extern struct fjson_object* fjson_object_new_int64(int64_t i); 532 533 534 /** Get the int value of a fjson_object 535 * 536 * The type is coerced to a int if the passed object is not a int. 537 * double objects will return their integer conversion. Strings will be 538 * parsed as an integer. If no conversion exists then 0 is returned 539 * and errno is set to EINVAL. null is equivalent to 0 (no error values set) 540 * 541 * Note that integers are stored internally as 64-bit values. 542 * If the value of too big or too small to fit into 32-bit, INT32_MAX or 543 * INT32_MIN are returned, respectively. 544 * 545 * @param obj the fjson_object instance 546 * @returns an int 547 */ 548 extern int32_t fjson_object_get_int(struct fjson_object *obj); 549 550 /** Get the uint32 value of a fjson_object 551 * 552 * The type is coerced to a int if the passed object is not a int. 553 * double objects will return their integer conversion. Strings will be 554 * parsed as an integer. If no conversion exists then 0 is returned 555 * and errno is set to EINVAL. null is equivalent to 0 (no error values set) 556 * 557 * Note that integers are stored internally as 64-bit values. 558 * If the value of too big or too small to fit into unsigned 32-bit 559 * representation, UINT32_MAX or UINT32_MIN are returned, respectively. 560 * 561 * @param obj the fjson_object instance 562 * @returns an int 563 */ 564 extern uint32_t fjson_object_get_uint(struct fjson_object *obj); 565 566 /** Get the int value of a fjson_object 567 * 568 * The type is coerced to a int64 if the passed object is not a int64. 569 * double objects will return their int64 conversion. Strings will be 570 * parsed as an int64. If no conversion exists then 0 is returned. 571 * 572 * NOTE: Set errno to 0 directly before a call to this function to determine 573 * whether or not conversion was successful (it does not clear the value for 574 * you). 575 * 576 * @param obj the fjson_object instance 577 * @returns an int64 578 */ 579 extern int64_t fjson_object_get_int64(struct fjson_object *obj); 580 581 582 /* double type methods */ 583 584 /** Create a new empty fjson_object of type fjson_type_double 585 * @param d the double 586 * @returns a fjson_object of type fjson_type_double 587 */ 588 extern struct fjson_object* fjson_object_new_double(double d); 589 590 /** 591 * Create a new fjson_object of type fjson_type_double, using 592 * the exact representation of the value. 593 * 594 * This allows for numbers that would otherwise get displayed 595 * inefficiently (e.g. 12.3 => "12.300000000000001") to be 596 * serialized with the more convenient form. 597 * 598 * Note: this is used by fjson_tokener_parse_ex() to allow for 599 * an exact re-serialization of a parsed object. 600 * 601 * @param d the numeric value of the double. 602 * @param ds the string representation of the double. This will be copied. 603 */ 604 extern struct fjson_object* fjson_object_new_double_s(double d, const char *ds); 605 606 /** Get the double floating point value of a fjson_object 607 * 608 * The type is coerced to a double if the passed object is not a double. 609 * integer objects will return their double conversion. Strings will be 610 * parsed as a double. If no conversion exists then 0.0 is returned and 611 * errno is set to EINVAL. null is equivalent to 0 (no error values set) 612 * 613 * If the value is too big to fit in a double, then the value is set to 614 * the closest infinity with errno set to ERANGE. If strings cannot be 615 * converted to their double value, then EINVAL is set & NaN is returned. 616 * 617 * Arrays of length 0 are interpreted as 0 (with no error flags set). 618 * Arrays of length 1 are effectively cast to the equivalent object and 619 * converted using the above rules. All other arrays set the error to 620 * EINVAL & return NaN. 621 * 622 * NOTE: Set errno to 0 directly before a call to this function to 623 * determine whether or not conversion was successful (it does not clear 624 * the value for you). 625 * 626 * @param obj the fjson_object instance 627 * @returns a double floating point number 628 */ 629 extern double fjson_object_get_double(struct fjson_object *obj); 630 631 632 /* string type methods */ 633 634 /** Create a new empty fjson_object of type fjson_type_string 635 * 636 * A copy of the string is made and the memory is managed by the fjson_object 637 * 638 * @param s the string 639 * @returns a fjson_object of type fjson_type_string 640 */ 641 extern struct fjson_object* fjson_object_new_string(const char *s); 642 643 extern struct fjson_object* fjson_object_new_string_len(const char *s, int len); 644 645 /** Get the string value of a fjson_object 646 * 647 * If the passed object is not of type fjson_type_string then the JSON 648 * representation of the object is returned. 649 * 650 * The returned string memory is managed by the fjson_object and will 651 * be freed when the reference count of the fjson_object drops to zero. 652 * 653 * @param obj the fjson_object instance 654 * @returns a string 655 */ 656 extern const char* fjson_object_get_string(struct fjson_object *obj); 657 658 /** Get the string length of a fjson_object 659 * 660 * If the passed object is not of type fjson_type_string then zero 661 * will be returned. 662 * 663 * @param obj the fjson_object instance 664 * @returns int 665 */ 666 extern int fjson_object_get_string_len(struct fjson_object *obj); 667 668 669 /** Get the number of direct members inside a json object. 670 * 671 * @param obj the fjson_object instance 672 * @returns int 673 */ 674 int fjson_object_get_member_count(struct fjson_object *jso); 675 676 677 /* The following is a source code compatibility layer 678 * in regard to json-c. 679 * It currently is aimed at the rsyslog family of projects, 680 * we may extend or drop it later. 681 */ 682 #ifndef FJSON_NATIVE_API_ONLY 683 684 #define JSON_C_TO_STRING_PLAIN FJSON_TO_STRING_PLAIN 685 #define JSON_C_TO_STRING_SPACED FJSON_TO_STRING_SPACED 686 #define JSON_C_TO_STRING_PRETTY FJSON_TO_STRING_PRETTY 687 #define JSON_C_TO_STRING_PRETTY_TAB FJSON_TO_STRING_PRETTY_TAB 688 #define JSON_C_TO_STRING_NOZERO FJSON_TO_STRING_NOZERO 689 #define JSON_C_OBJECT_ADD_KEY_IS_NEW FJSON_OBJECT_ADD_KEY_IS_NEW 690 #define JSON_C_OBJECT_KEY_IS_CONSTANT FJSON_OBJECT_KEY_IS_CONSTANT 691 692 693 /* forward structure definitions */ 694 695 #if 0 696 typedef int fjson_bool; 697 typedef struct printbuf printbuf; 698 typedef struct lh_table lh_table; 699 typedef struct array_list array_list; 700 typedef struct fjson_object fjson_object; 701 typedef struct fjson_tokener fjson_tokener; 702 #endif 703 704 #define json_bool fjson_bool 705 #define json_type fjson_type 706 #define json_type_null fjson_type_null 707 #define json_type_boolean fjson_type_boolean 708 #define json_type_double fjson_type_double 709 #define json_type_int fjson_type_int 710 #define json_type_object fjson_type_object 711 #define json_type_array fjson_type_array 712 #define json_type_string fjson_type_string 713 #define json_object_iter fjson_object_iter 714 715 #define json_object fjson_object 716 717 #define json_object_get fjson_object_get 718 #define json_object_put fjson_object_put 719 #define json_object_is_type fjson_object_is_type 720 #define json_object_get_type(x) fjson_object_get_type((x)) 721 #define json_object_to_json_string(x) fjson_object_to_json_string((x)) 722 #define json_object_to_json_string_ext(a, b) fjson_object_to_json_string_ext((a), (b)) 723 #define json_object_new_object() fjson_object_new_object() 724 #define json_object_object_length(a) fjson_object_object_length((a)) 725 #define json_object_object_add(a, b, c) fjson_object_object_add((a), (b), (c)) 726 #define json_object_object_add_ex fjson_object_object_add_ex 727 #define json_object_object_get_ex fjson_object_object_get_ex 728 #define json_object_object_get fjson_object_object_get 729 #define json_object_object_del fjson_object_object_del 730 #define json_object_new_array fjson_object_new_array 731 #define json_object_get_array fjson_object_get_array 732 #define json_object_array_length fjson_object_array_length 733 #define json_object_array_sort fjson_object_array_sort 734 #define json_object_array_bsearch fjson_object_array_bsearch 735 #define json_object_array_add fjson_object_array_add 736 #define json_object_array_put_idx fjson_object_array_put_idx 737 #define json_object_array_get_idx fjson_object_array_get_idx 738 #define json_object_new_boolean fjson_object_new_boolean 739 #define json_object_get_boolean fjson_object_get_boolean 740 #define json_object_new_int fjson_object_new_int 741 #define json_object_new_int64 fjson_object_new_int64 742 #define json_object_new_double fjson_object_new_double 743 #define json_object_new_double_s fjson_object_new_double_s 744 #define json_object_get_double fjson_object_get_double 745 #define json_object_new_string fjson_object_new_string 746 #define json_object_new_string_len fjson_object_new_string_len 747 #define json_object_get_string fjson_object_get_string 748 #define json_object_get_int fjson_object_get_int 749 #define json_object_get_uint fjson_object_get_uint 750 #define json_object_get_int64 fjson_object_get_int64 751 #define json_object_get_string_len fjson_object_get_string_len 752 #define json_object_get_member_count fjson_object_get_member_count 753 #define json_object_array_del_idx fjson_object_array_del_idx 754 755 756 #endif 757 #ifdef __cplusplus 758 } 759 #endif 760 761 #endif 762