ucx/cx/json.h

changeset 16
04c9f8d8f03b
parent 11
0aa8cbd7912e
child 22
112b85020dc9
equal deleted inserted replaced
15:862ab606ee06 16:04c9f8d8f03b
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file json.h 29 * @file json.h
30 * \brief Interface for parsing data from JSON files. 30 * @brief Interface for parsing data from JSON files.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \copyright 2-Clause BSD License 33 * @copyright 2-Clause BSD License
34 */ 34 */
35 35
36 #ifndef UCX_JSON_H 36 #ifndef UCX_JSON_H
37 #define UCX_JSON_H 37 #define UCX_JSON_H
38 38
144 /** 144 /**
145 * JSON literal types. 145 * JSON literal types.
146 */ 146 */
147 enum cx_json_literal { 147 enum cx_json_literal {
148 /** 148 /**
149 * The \c null literal. 149 * The @c null literal.
150 */ 150 */
151 CX_JSON_NULL, 151 CX_JSON_NULL,
152 /** 152 /**
153 * The \c true literal. 153 * The @c true literal.
154 */ 154 */
155 CX_JSON_TRUE, 155 CX_JSON_TRUE,
156 /** 156 /**
157 * The \c false literal. 157 * The @c false literal.
158 */ 158 */
159 CX_JSON_FALSE 159 CX_JSON_FALSE
160 }; 160 };
161 161
162 /** 162 /**
262 */ 262 */
263 const CxAllocator *allocator; 263 const CxAllocator *allocator;
264 /** 264 /**
265 * The type of this value. 265 * The type of this value.
266 * 266 *
267 * Specifies how the \c value union shall be resolved. 267 * Specifies how the @c value union shall be resolved.
268 */ 268 */
269 CxJsonValueType type; 269 CxJsonValueType type;
270 /** 270 /**
271 * The value data. 271 * The value data.
272 */ 272 */
307 /** 307 /**
308 * The token type. 308 * The token type.
309 */ 309 */
310 CxJsonTokenType tokentype; 310 CxJsonTokenType tokentype;
311 /** 311 /**
312 * True, iff the \c content must be passed to cx_strfree(). 312 * True, if the @c content must be passed to cx_strfree().
313 */ 313 */
314 bool allocated; 314 bool allocated;
315 /** 315 /**
316 * The token text, if any. 316 * The token text, if any.
317 * 317 *
372 372
373 /** 373 /**
374 * Internally reserved memory for the value buffer stack. 374 * Internally reserved memory for the value buffer stack.
375 */ 375 */
376 CxJsonValue* vbuf_internal[8]; 376 CxJsonValue* vbuf_internal[8];
377
378 /**
379 * Used internally.
380 */
381 bool tokenizer_escape; // TODO: check if it can be replaced with look-behind
382 }; 377 };
383 378
384 /** 379 /**
385 * Status codes for the json interface. 380 * Status codes for the json interface.
386 */ 381 */
401 CX_JSON_INCOMPLETE_DATA, 396 CX_JSON_INCOMPLETE_DATA,
402 /** 397 /**
403 * Not used as a status and never returned by any function. 398 * Not used as a status and never returned by any function.
404 * 399 *
405 * You can use this enumerator to check for all "good" status results 400 * You can use this enumerator to check for all "good" status results
406 * by checking if the status is less than \c CX_JSON_OK. 401 * by checking if the status is less than @c CX_JSON_OK.
407 * 402 *
408 * A "good" status means, that you can refill data and continue parsing. 403 * A "good" status means, that you can refill data and continue parsing.
409 */ 404 */
410 CX_JSON_OK, 405 CX_JSON_OK,
411 /** 406 /**
447 * Set false to output the members in the order in which they were added. 442 * Set false to output the members in the order in which they were added.
448 */ 443 */
449 bool sort_members; 444 bool sort_members;
450 /** 445 /**
451 * The maximum number of fractional digits in a number value. 446 * The maximum number of fractional digits in a number value.
447 * The default value is 6 and values larger than 15 are reduced to 15.
448 * Note, that the actual number of digits may be lower, depending on the concrete number.
452 */ 449 */
453 uint8_t frac_max_digits; 450 uint8_t frac_max_digits;
454 /** 451 /**
455 * Set true to use spaces instead of tab characters. 452 * Set true to use spaces instead of tab characters.
456 * Indentation is only used in pretty output. 453 * Indentation is only used in pretty output.
457 */ 454 */
458 bool indent_space; 455 bool indent_space;
459 /** 456 /**
460 * If \c indent_space is true, this is the number of spaces per tab. 457 * If @c indent_space is true, this is the number of spaces per tab.
461 * Indentation is only used in pretty output. 458 * Indentation is only used in pretty output.
462 */ 459 */
463 uint8_t indent; 460 uint8_t indent;
461 /**
462 * Set true to enable escaping of the slash character (solidus).
463 */
464 bool escape_slash;
464 }; 465 };
465 466
466 /** 467 /**
467 * Typedef for the json writer. 468 * Typedef for the json writer.
468 */ 469 */
472 * Creates a default writer configuration for compact output. 473 * Creates a default writer configuration for compact output.
473 * 474 *
474 * @return new JSON writer settings 475 * @return new JSON writer settings
475 */ 476 */
476 cx_attr_nodiscard 477 cx_attr_nodiscard
478 cx_attr_export
477 CxJsonWriter cxJsonWriterCompact(void); 479 CxJsonWriter cxJsonWriterCompact(void);
478 480
479 /** 481 /**
480 * Creates a default writer configuration for pretty output. 482 * Creates a default writer configuration for pretty output.
481 * 483 *
482 * @param use_spaces false if you want tabs, true if you want four spaces instead 484 * @param use_spaces false if you want tabs, true if you want four spaces instead
483 * @return new JSON writer settings 485 * @return new JSON writer settings
484 */ 486 */
485 cx_attr_nodiscard 487 cx_attr_nodiscard
488 cx_attr_export
486 CxJsonWriter cxJsonWriterPretty(bool use_spaces); 489 CxJsonWriter cxJsonWriterPretty(bool use_spaces);
487 490
488 /** 491 /**
489 * Writes a JSON value to a buffer or stream. 492 * Writes a JSON value to a buffer or stream.
490 * 493 *
491 * This function blocks until all data is written or an error when trying 494 * This function blocks until either all data is written, or an error occurs.
492 * to write data occurs.
493 * The write operation is not atomic in the sense that it might happen 495 * The write operation is not atomic in the sense that it might happen
494 * that the data is only partially written when an error occurs with no 496 * that the data is only partially written when an error occurs with no
495 * way to indicate how much data was written. 497 * way to indicate how much data was written.
496 * To avoid this problem, you can use a CxBuffer as \p target which is 498 * To avoid this problem, you can use a CxBuffer as @p target which is
497 * unlikely to fail a write operation and either use the buffer's flush 499 * unlikely to fail a write operation and either use the buffer's flush
498 * feature to relay the data or use the data in the buffer manually to 500 * feature to relay the data or use the data in the buffer manually to
499 * write it to the actual target. 501 * write it to the actual target.
500 * 502 *
501 * @param target the buffer or stream where to write to 503 * @param target the buffer or stream where to write to
502 * @param value the value that shall be written 504 * @param value the value that shall be written
503 * @param wfunc the write function to use 505 * @param wfunc the write function to use
504 * @param settings formatting settings (or \c NULL to use a compact default) 506 * @param settings formatting settings (or @c NULL to use a compact default)
505 * @return zero on success, non-zero when no or not all data could be written 507 * @retval zero success
508 * @retval non-zero when no or not all data could be written
506 */ 509 */
507 cx_attr_nonnull_arg(1, 2, 3) 510 cx_attr_nonnull_arg(1, 2, 3)
511 cx_attr_export
508 int cxJsonWrite( 512 int cxJsonWrite(
509 void* target, 513 void* target,
510 const CxJsonValue* value, 514 const CxJsonValue* value,
511 cx_write_func wfunc, 515 cx_write_func wfunc,
512 const CxJsonWriter* settings 516 const CxJsonWriter* settings
518 * @param json the json interface 522 * @param json the json interface
519 * @param allocator the allocator that shall be used for the produced values 523 * @param allocator the allocator that shall be used for the produced values
520 * @see cxJsonDestroy() 524 * @see cxJsonDestroy()
521 */ 525 */
522 cx_attr_nonnull_arg(1) 526 cx_attr_nonnull_arg(1)
527 cx_attr_export
523 void cxJsonInit(CxJson *json, const CxAllocator *allocator); 528 void cxJsonInit(CxJson *json, const CxAllocator *allocator);
524 529
525 /** 530 /**
526 * Destroys the json interface. 531 * Destroys the json interface.
527 * 532 *
528 * @param json the json interface 533 * @param json the json interface
529 * @see cxJsonInit() 534 * @see cxJsonInit()
530 */ 535 */
531 cx_attr_nonnull 536 cx_attr_nonnull
537 cx_attr_export
532 void cxJsonDestroy(CxJson *json); 538 void cxJsonDestroy(CxJson *json);
533 539
534 /** 540 /**
535 * Destroys and re-initializes the json interface. 541 * Destroys and re-initializes the json interface.
536 * 542 *
558 * an allocation of a new buffer and copying the previous contents. 564 * an allocation of a new buffer and copying the previous contents.
559 * 565 *
560 * @param json the json interface 566 * @param json the json interface
561 * @param buf the source buffer 567 * @param buf the source buffer
562 * @param len the length of the source buffer 568 * @param len the length of the source buffer
563 * @return zero on success, non-zero on internal allocation error 569 * @retval zero success
570 * @retval non-zero internal allocation error
564 * @see cxJsonFill() 571 * @see cxJsonFill()
565 */ 572 */
566 cx_attr_nonnull 573 cx_attr_nonnull
567 cx_attr_access_r(2, 3) 574 cx_attr_access_r(2, 3)
575 cx_attr_export
568 int cxJsonFilln(CxJson *json, const char *buf, size_t len); 576 int cxJsonFilln(CxJson *json, const char *buf, size_t len);
569 577
570 #ifdef __cplusplus 578 #ifdef __cplusplus
571 } // extern "C" 579 } // extern "C"
572 580
598 extern "C" { 606 extern "C" {
599 #else // __cplusplus 607 #else // __cplusplus
600 /** 608 /**
601 * Fills the input buffer. 609 * Fills the input buffer.
602 * 610 *
603 * @remark The JSON interface tries to avoid copying the input data. 611 * The JSON interface tries to avoid copying the input data.
604 * When you use this function and cxJsonNext() interleaving, 612 * When you use this function and cxJsonNext() interleaving,
605 * no copies are performed. However, you must not free the 613 * no copies are performed. However, you must not free the
606 * pointer to the data in that case. When you invoke the fill 614 * pointer to the data in that case. When you invoke the fill
607 * function more than once before calling cxJsonNext(), 615 * function more than once before calling cxJsonNext(),
608 * the additional data is appended - inevitably leading to 616 * the additional data is appended - inevitably leading to
609 * an allocation of a new buffer and copying the previous contents. 617 * an allocation of a new buffer and copying the previous contents.
610 * 618 *
611 * @param json the json interface 619 * @param json the json interface
612 * @param buf the source string 620 * @param str the source string
613 * @return zero on success, non-zero on internal allocation error 621 * @retval zero success
622 * @retval non-zero internal allocation error
614 * @see cxJsonFilln() 623 * @see cxJsonFilln()
615 */ 624 */
616 #define cxJsonFill(json, str) _Generic((str), \ 625 #define cxJsonFill(json, str) _Generic((str), \
617 cxstring: cx_json_fill_cxstr, \ 626 cxstring: cx_json_fill_cxstr, \
618 cxmutstr: cx_json_fill_mutstr, \ 627 cxmutstr: cx_json_fill_mutstr, \
657 666
658 /** 667 /**
659 * Creates a new (empty) JSON object. 668 * Creates a new (empty) JSON object.
660 * 669 *
661 * @param allocator the allocator to use 670 * @param allocator the allocator to use
662 * @return the new JSON object or \c NULL if allocation fails 671 * @return the new JSON object or @c NULL if allocation fails
672 * @see cxJsonObjPutObj()
673 * @see cxJsonArrAddValues()
663 */ 674 */
664 cx_attr_nodiscard 675 cx_attr_nodiscard
676 cx_attr_export
665 CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator); 677 CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator);
666 678
667 /** 679 /**
668 * Creates a new (empty) JSON array. 680 * Creates a new (empty) JSON array.
669 * 681 *
670 * @param allocator the allocator to use 682 * @param allocator the allocator to use
671 * @return the new JSON array or \c NULL if allocation fails 683 * @return the new JSON array or @c NULL if allocation fails
684 * @see cxJsonObjPutArr()
685 * @see cxJsonArrAddValues()
672 */ 686 */
673 cx_attr_nodiscard 687 cx_attr_nodiscard
688 cx_attr_export
674 CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator); 689 CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator);
675 690
676 /** 691 /**
677 * Creates a new JSON number value. 692 * Creates a new JSON number value.
678 * 693 *
679 * @param allocator the allocator to use 694 * @param allocator the allocator to use
680 * @param num the numeric value 695 * @param num the numeric value
681 * @return the new JSON value or \c NULL if allocation fails 696 * @return the new JSON value or @c NULL if allocation fails
682 * @see cxJsonObjPutNumber() 697 * @see cxJsonObjPutNumber()
683 * @see cxJsonArrAddNumbers() 698 * @see cxJsonArrAddNumbers()
684 */ 699 */
685 cx_attr_nodiscard 700 cx_attr_nodiscard
701 cx_attr_export
686 CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num); 702 CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num);
687 703
688 /** 704 /**
689 * Creates a new JSON number value based on an integer. 705 * Creates a new JSON number value based on an integer.
690 * 706 *
691 * @param allocator the allocator to use 707 * @param allocator the allocator to use
692 * @param num the numeric value 708 * @param num the numeric value
693 * @return the new JSON value or \c NULL if allocation fails 709 * @return the new JSON value or @c NULL if allocation fails
694 * @see cxJsonObjPutInteger() 710 * @see cxJsonObjPutInteger()
695 * @see cxJsonArrAddIntegers() 711 * @see cxJsonArrAddIntegers()
696 */ 712 */
697 cx_attr_nodiscard 713 cx_attr_nodiscard
714 cx_attr_export
698 CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num); 715 CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num);
699 716
700 /** 717 /**
701 * Creates a new JSON string. 718 * Creates a new JSON string.
702 * 719 *
703 * @param allocator the allocator to use 720 * @param allocator the allocator to use
704 * @param str the string data 721 * @param str the string data
705 * @return the new JSON value or \c NULL if allocation fails 722 * @return the new JSON value or @c NULL if allocation fails
706 * @see cxJsonCreateCxString() 723 * @see cxJsonCreateString()
707 * @see cxJsonObjPutString() 724 * @see cxJsonObjPutString()
708 * @see cxJsonArrAddStrings() 725 * @see cxJsonArrAddStrings()
709 */ 726 */
710 cx_attr_nodiscard 727 cx_attr_nodiscard
711 cx_attr_nonnull_arg(2) 728 cx_attr_nonnull_arg(2)
712 cx_attr_cstr_arg(2) 729 cx_attr_cstr_arg(2)
730 cx_attr_export
713 CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char *str); 731 CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char *str);
714 732
715 /** 733 /**
716 * Creates a new JSON string. 734 * Creates a new JSON string.
717 * 735 *
718 * @param allocator the allocator to use 736 * @param allocator the allocator to use
719 * @param str the string data 737 * @param str the string data
720 * @return the new JSON value or \c NULL if allocation fails 738 * @return the new JSON value or @c NULL if allocation fails
721 * @see cxJsonCreateString() 739 * @see cxJsonCreateCxString()
722 * @see cxJsonObjPutCxString() 740 * @see cxJsonObjPutCxString()
723 * @see cxJsonArrAddCxStrings() 741 * @see cxJsonArrAddCxStrings()
724 */ 742 */
725 cx_attr_nodiscard 743 cx_attr_nodiscard
744 cx_attr_export
726 CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str); 745 CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str);
727 746
728 /** 747 /**
729 * Creates a new JSON literal. 748 * Creates a new JSON literal.
730 * 749 *
731 * @param allocator the allocator to use 750 * @param allocator the allocator to use
732 * @param lit the type of literal 751 * @param lit the type of literal
733 * @return the new JSON value or \c NULL if allocation fails 752 * @return the new JSON value or @c NULL if allocation fails
734 * @see cxJsonObjPutLiteral() 753 * @see cxJsonObjPutLiteral()
735 * @see cxJsonArrAddLiterals() 754 * @see cxJsonArrAddLiterals()
736 */ 755 */
737 cx_attr_nodiscard 756 cx_attr_nodiscard
757 cx_attr_export
738 CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit); 758 CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit);
739 759
740 /** 760 /**
741 * Adds number values to a JSON array. 761 * Adds number values to a JSON array.
742 * 762 *
743 * @param arr the JSON array 763 * @param arr the JSON array
744 * @param num the array of values 764 * @param num the array of values
745 * @param count the number of elements 765 * @param count the number of elements
746 * @return zero on success, non-zero on allocation failure 766 * @retval zero success
767 * @retval non-zero allocation failure
747 */ 768 */
748 cx_attr_nonnull 769 cx_attr_nonnull
749 cx_attr_access_r(2, 3) 770 cx_attr_access_r(2, 3)
771 cx_attr_export
750 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count); 772 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count);
751 773
752 /** 774 /**
753 * Adds number values, of which all are integers, to a JSON array. 775 * Adds number values, of which all are integers, to a JSON array.
754 * 776 *
755 * @param arr the JSON array 777 * @param arr the JSON array
756 * @param num the array of values 778 * @param num the array of values
757 * @param count the number of elements 779 * @param count the number of elements
758 * @return zero on success, non-zero on allocation failure 780 * @retval zero success
781 * @retval non-zero allocation failure
759 */ 782 */
760 cx_attr_nonnull 783 cx_attr_nonnull
761 cx_attr_access_r(2, 3) 784 cx_attr_access_r(2, 3)
785 cx_attr_export
762 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count); 786 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count);
763 787
764 /** 788 /**
765 * Adds strings to a JSON array. 789 * Adds strings to a JSON array.
766 * 790 *
767 * The strings will be copied with the allocator of the array. 791 * The strings will be copied with the allocator of the array.
768 * 792 *
769 * @param arr the JSON array 793 * @param arr the JSON array
770 * @param str the array of strings 794 * @param str the array of strings
771 * @param count the number of elements 795 * @param count the number of elements
772 * @return zero on success, non-zero on allocation failure 796 * @retval zero success
797 * @retval non-zero allocation failure
773 * @see cxJsonArrAddCxStrings() 798 * @see cxJsonArrAddCxStrings()
774 */ 799 */
775 cx_attr_nonnull 800 cx_attr_nonnull
776 cx_attr_access_r(2, 3) 801 cx_attr_access_r(2, 3)
802 cx_attr_export
777 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count); 803 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count);
778 804
779 /** 805 /**
780 * Adds strings to a JSON array. 806 * Adds strings to a JSON array.
781 * 807 *
782 * The strings will be copied with the allocator of the array. 808 * The strings will be copied with the allocator of the array.
783 * 809 *
784 * @param arr the JSON array 810 * @param arr the JSON array
785 * @param str the array of strings 811 * @param str the array of strings
786 * @param count the number of elements 812 * @param count the number of elements
787 * @return zero on success, non-zero on allocation failure 813 * @retval zero success
814 * @retval non-zero allocation failure
788 * @see cxJsonArrAddStrings() 815 * @see cxJsonArrAddStrings()
789 */ 816 */
790 cx_attr_nonnull 817 cx_attr_nonnull
791 cx_attr_access_r(2, 3) 818 cx_attr_access_r(2, 3)
819 cx_attr_export
792 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count); 820 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count);
793 821
794 /** 822 /**
795 * Adds literals to a JSON array. 823 * Adds literals to a JSON array.
796 * 824 *
797 * @param arr the JSON array 825 * @param arr the JSON array
798 * @param lit the array of literal types 826 * @param lit the array of literal types
799 * @param count the number of elements 827 * @param count the number of elements
800 * @return zero on success, non-zero on allocation failure 828 * @retval zero success
829 * @retval non-zero allocation failure
801 */ 830 */
802 cx_attr_nonnull 831 cx_attr_nonnull
803 cx_attr_access_r(2, 3) 832 cx_attr_access_r(2, 3)
833 cx_attr_export
804 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count); 834 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count);
805 835
806 /** 836 /**
807 * Add arbitrary values to a JSON array. 837 * Add arbitrary values to a JSON array.
808 * 838 *
809 * \note In contrast to all other add functions, this function adds the values 839 * @attention In contrast to all other add functions, this function adds the values
810 * directly to the array instead of copying them. 840 * directly to the array instead of copying them.
811 * 841 *
812 * @param arr the JSON array 842 * @param arr the JSON array
813 * @param val the values 843 * @param val the values
814 * @param count the number of elements 844 * @param count the number of elements
815 * @return zero on success, non-zero on allocation failure 845 * @retval zero success
846 * @retval non-zero allocation failure
816 */ 847 */
817 cx_attr_nonnull 848 cx_attr_nonnull
818 cx_attr_access_r(2, 3) 849 cx_attr_access_r(2, 3)
850 cx_attr_export
819 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count); 851 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count);
820 852
821 /** 853 /**
822 * Adds or replaces a value within a JSON object. 854 * Adds or replaces a value within a JSON object.
823 * 855 *
824 * The value will be directly added and not copied. 856 * The value will be directly added and not copied.
825 * 857 *
826 * \note If a value with the specified \p name already exists, 858 * @note If a value with the specified @p name already exists,
827 * it will be (recursively) freed with its own allocator. 859 * it will be (recursively) freed with its own allocator.
828 * 860 *
829 * @param obj the JSON object 861 * @param obj the JSON object
830 * @param name the name of the value 862 * @param name the name of the value
831 * @param child the value 863 * @param child the value
832 * @return zero on success, non-zero on allocation failure 864 * @retval zero success
833 */ 865 * @retval non-zero allocation failure
834 cx_attr_nonnull 866 */
867 cx_attr_nonnull
868 cx_attr_export
835 int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child); 869 int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child);
836 870
837 /** 871 /**
838 * Creates a new JSON object and adds it to an existing object. 872 * Creates a new JSON object and adds it to an existing object.
839 * 873 *
840 * @param obj the target JSON object 874 * @param obj the target JSON object
841 * @param name the name of the new value 875 * @param name the name of the new value
842 * @return the new value or \c NULL if allocation fails 876 * @return the new value or @c NULL if allocation fails
843 * @see cxJsonObjPut() 877 * @see cxJsonObjPut()
844 * @see cxJsonCreateObj() 878 * @see cxJsonCreateObj()
845 */ 879 */
846 cx_attr_nonnull 880 cx_attr_nonnull
881 cx_attr_export
847 CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name); 882 CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name);
848 883
849 /** 884 /**
850 * Creates a new JSON array and adds it to an object. 885 * Creates a new JSON array and adds it to an object.
851 * 886 *
852 * @param obj the target JSON object 887 * @param obj the target JSON object
853 * @param name the name of the new value 888 * @param name the name of the new value
854 * @return the new value or \c NULL if allocation fails 889 * @return the new value or @c NULL if allocation fails
855 * @see cxJsonObjPut() 890 * @see cxJsonObjPut()
856 * @see cxJsonCreateArr() 891 * @see cxJsonCreateArr()
857 */ 892 */
858 cx_attr_nonnull 893 cx_attr_nonnull
894 cx_attr_export
859 CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name); 895 CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name);
860 896
861 /** 897 /**
862 * Creates a new JSON number and adds it to an object. 898 * Creates a new JSON number and adds it to an object.
863 * 899 *
864 * @param obj the target JSON object 900 * @param obj the target JSON object
865 * @param name the name of the new value 901 * @param name the name of the new value
866 * @param num the numeric value 902 * @param num the numeric value
867 * @return the new value or \c NULL if allocation fails 903 * @return the new value or @c NULL if allocation fails
868 * @see cxJsonObjPut() 904 * @see cxJsonObjPut()
869 * @see cxJsonCreateNumber() 905 * @see cxJsonCreateNumber()
870 */ 906 */
871 cx_attr_nonnull 907 cx_attr_nonnull
908 cx_attr_export
872 CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num); 909 CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num);
873 910
874 /** 911 /**
875 * Creates a new JSON number, based on an integer, and adds it to an object. 912 * Creates a new JSON number, based on an integer, and adds it to an object.
876 * 913 *
877 * @param obj the target JSON object 914 * @param obj the target JSON object
878 * @param name the name of the new value 915 * @param name the name of the new value
879 * @param num the numeric value 916 * @param num the numeric value
880 * @return the new value or \c NULL if allocation fails 917 * @return the new value or @c NULL if allocation fails
881 * @see cxJsonObjPut() 918 * @see cxJsonObjPut()
882 * @see cxJsonCreateInteger() 919 * @see cxJsonCreateInteger()
883 */ 920 */
884 cx_attr_nonnull 921 cx_attr_nonnull
922 cx_attr_export
885 CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num); 923 CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num);
886 924
887 /** 925 /**
888 * Creates a new JSON string and adds it to an object. 926 * Creates a new JSON string and adds it to an object.
889 * 927 *
890 * The string data is copied. 928 * The string data is copied.
891 * 929 *
892 * @param obj the target JSON object 930 * @param obj the target JSON object
893 * @param name the name of the new value 931 * @param name the name of the new value
894 * @param str the string data 932 * @param str the string data
895 * @return the new value or \c NULL if allocation fails 933 * @return the new value or @c NULL if allocation fails
896 * @see cxJsonObjPut() 934 * @see cxJsonObjPut()
897 * @see cxJsonCreateString() 935 * @see cxJsonCreateString()
898 */ 936 */
899 cx_attr_nonnull 937 cx_attr_nonnull
900 cx_attr_cstr_arg(3) 938 cx_attr_cstr_arg(3)
939 cx_attr_export
901 CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str); 940 CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str);
902 941
903 /** 942 /**
904 * Creates a new JSON string and adds it to an object. 943 * Creates a new JSON string and adds it to an object.
905 * 944 *
906 * The string data is copied. 945 * The string data is copied.
907 * 946 *
908 * @param obj the target JSON object 947 * @param obj the target JSON object
909 * @param name the name of the new value 948 * @param name the name of the new value
910 * @param str the string data 949 * @param str the string data
911 * @return the new value or \c NULL if allocation fails 950 * @return the new value or @c NULL if allocation fails
912 * @see cxJsonObjPut() 951 * @see cxJsonObjPut()
913 * @see cxJsonCreateCxString() 952 * @see cxJsonCreateCxString()
914 */ 953 */
915 cx_attr_nonnull 954 cx_attr_nonnull
955 cx_attr_export
916 CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str); 956 CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str);
917 957
918 /** 958 /**
919 * Creates a new JSON literal and adds it to an object. 959 * Creates a new JSON literal and adds it to an object.
920 * 960 *
921 * @param obj the target JSON object 961 * @param obj the target JSON object
922 * @param name the name of the new value 962 * @param name the name of the new value
923 * @param lit the type of literal 963 * @param lit the type of literal
924 * @return the new value or \c NULL if allocation fails 964 * @return the new value or @c NULL if allocation fails
925 * @see cxJsonObjPut() 965 * @see cxJsonObjPut()
926 * @see cxJsonCreateLiteral() 966 * @see cxJsonCreateLiteral()
927 */ 967 */
928 cx_attr_nonnull 968 cx_attr_nonnull
969 cx_attr_export
929 CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit); 970 CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit);
930 971
931 /** 972 /**
932 * Recursively deallocates the memory of a JSON value. 973 * Recursively deallocates the memory of a JSON value.
933 * 974 *
934 * \remark The type of each deallocated value will be changed 975 * @remark The type of each deallocated value will be changed
935 * to #CX_JSON_NOTHING and values of such type will be skipped 976 * to #CX_JSON_NOTHING and values of such type will be skipped
936 * by the de-allocation. That means, this function protects 977 * by the de-allocation. That means, this function protects
937 * you from double-frees when you are accidentally freeing 978 * you from double-frees when you are accidentally freeing
938 * a nested value and then the parent value (or vice versa). 979 * a nested value and then the parent value (or vice versa).
939 * 980 *
940 * @param value the value 981 * @param value the value
941 */ 982 */
983 cx_attr_export
942 void cxJsonValueFree(CxJsonValue *value); 984 void cxJsonValueFree(CxJsonValue *value);
943 985
944 /** 986 /**
945 * Tries to obtain the next JSON value. 987 * Tries to obtain the next JSON value.
946 * 988 *
989 * Before this function can be called, the input buffer needs
990 * to be filled with cxJsonFill().
991 *
992 * When this function returns #CX_JSON_INCOMPLETE_DATA, you can
993 * add the missing data with another invocation of cxJsonFill()
994 * and then repeat the call to cxJsonNext().
947 * 995 *
948 * @param json the json interface 996 * @param json the json interface
949 * @param value a pointer where the next value shall be stored 997 * @param value a pointer where the next value shall be stored
950 * @return a status code 998 * @retval CX_JSON_NO_ERROR successfully retrieve the @p value
999 * @retval CX_JSON_NO_DATA there is no (more) data in the buffer to read from
1000 * @retval CX_JSON_INCOMPLETE_DATA an incomplete value was read
1001 * and more data needs to be filled
1002 * @retval CX_JSON_NULL_DATA the buffer was never initialized
1003 * @retval CX_JSON_BUFFER_ALLOC_FAILED allocating internal buffer space failed
1004 * @retval CX_JSON_VALUE_ALLOC_FAILED allocating memory for a CxJsonValue failed
1005 * @retval CX_JSON_FORMAT_ERROR_NUMBER the JSON text contains an illegally formatted number
1006 * @retval CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN JSON syntax error
951 */ 1007 */
952 cx_attr_nonnull 1008 cx_attr_nonnull
953 cx_attr_access_w(2) 1009 cx_attr_access_w(2)
1010 cx_attr_export
954 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value); 1011 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
955 1012
956 /** 1013 /**
957 * Checks if the specified value is a JSON object. 1014 * Checks if the specified value is a JSON object.
958 * 1015 *
959 * @param value a pointer to the value 1016 * @param value a pointer to the value
960 * @return true if the value is a JSON object, false otherwise 1017 * @retval true the value is a JSON object
1018 * @retval false otherwise
961 */ 1019 */
962 cx_attr_nonnull 1020 cx_attr_nonnull
963 static inline bool cxJsonIsObject(const CxJsonValue *value) { 1021 static inline bool cxJsonIsObject(const CxJsonValue *value) {
964 return value->type == CX_JSON_OBJECT; 1022 return value->type == CX_JSON_OBJECT;
965 } 1023 }
966 1024
967 /** 1025 /**
968 * Checks if the specified value is a JSON array. 1026 * Checks if the specified value is a JSON array.
969 * 1027 *
970 * @param value a pointer to the value 1028 * @param value a pointer to the value
971 * @return true if the value is a JSON array, false otherwise 1029 * @retval true the value is a JSON array
1030 * @retval false otherwise
972 */ 1031 */
973 cx_attr_nonnull 1032 cx_attr_nonnull
974 static inline bool cxJsonIsArray(const CxJsonValue *value) { 1033 static inline bool cxJsonIsArray(const CxJsonValue *value) {
975 return value->type == CX_JSON_ARRAY; 1034 return value->type == CX_JSON_ARRAY;
976 } 1035 }
977 1036
978 /** 1037 /**
979 * Checks if the specified value is a string. 1038 * Checks if the specified value is a string.
980 * 1039 *
981 * @param value a pointer to the value 1040 * @param value a pointer to the value
982 * @return true if the value is a string, false otherwise 1041 * @retval true the value is a string
1042 * @retval false otherwise
983 */ 1043 */
984 cx_attr_nonnull 1044 cx_attr_nonnull
985 static inline bool cxJsonIsString(const CxJsonValue *value) { 1045 static inline bool cxJsonIsString(const CxJsonValue *value) {
986 return value->type == CX_JSON_STRING; 1046 return value->type == CX_JSON_STRING;
987 } 1047 }
991 * 1051 *
992 * This function will return true for both floating point and 1052 * This function will return true for both floating point and
993 * integer numbers. 1053 * integer numbers.
994 * 1054 *
995 * @param value a pointer to the value 1055 * @param value a pointer to the value
996 * @return true if the value is a JSON number, false otherwise 1056 * @retval true the value is a JSON number
1057 * @retval false otherwise
997 * @see cxJsonIsInteger() 1058 * @see cxJsonIsInteger()
998 */ 1059 */
999 cx_attr_nonnull 1060 cx_attr_nonnull
1000 static inline bool cxJsonIsNumber(const CxJsonValue *value) { 1061 static inline bool cxJsonIsNumber(const CxJsonValue *value) {
1001 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER; 1062 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER;
1003 1064
1004 /** 1065 /**
1005 * Checks if the specified value is an integer number. 1066 * Checks if the specified value is an integer number.
1006 * 1067 *
1007 * @param value a pointer to the value 1068 * @param value a pointer to the value
1008 * @return true if the value is an integer number, false otherwise 1069 * @retval true the value is an integer number
1070 * @retval false otherwise
1009 * @see cxJsonIsNumber() 1071 * @see cxJsonIsNumber()
1010 */ 1072 */
1011 cx_attr_nonnull 1073 cx_attr_nonnull
1012 static inline bool cxJsonIsInteger(const CxJsonValue *value) { 1074 static inline bool cxJsonIsInteger(const CxJsonValue *value) {
1013 return value->type == CX_JSON_INTEGER; 1075 return value->type == CX_JSON_INTEGER;
1014 } 1076 }
1015 1077
1016 /** 1078 /**
1017 * Checks if the specified value is a JSON literal. 1079 * Checks if the specified value is a JSON literal.
1018 * 1080 *
1019 * JSON literals are \c true, \c false, and \c null. 1081 * JSON literals are @c true, @c false, and @c null.
1020 * 1082 *
1021 * @param value a pointer to the value 1083 * @param value a pointer to the value
1022 * @return true if the value is a JSON literal, false otherwise 1084 * @retval true the value is a JSON literal
1085 * @retval false otherwise
1023 * @see cxJsonIsTrue() 1086 * @see cxJsonIsTrue()
1024 * @see cxJsonIsFalse() 1087 * @see cxJsonIsFalse()
1025 * @see cxJsonIsNull() 1088 * @see cxJsonIsNull()
1026 */ 1089 */
1027 cx_attr_nonnull 1090 cx_attr_nonnull
1031 1094
1032 /** 1095 /**
1033 * Checks if the specified value is a Boolean literal. 1096 * Checks if the specified value is a Boolean literal.
1034 * 1097 *
1035 * @param value a pointer to the value 1098 * @param value a pointer to the value
1036 * @return true if the value is either \c true or \c false, false otherwise 1099 * @retval true the value is either @c true or @c false
1100 * @retval false otherwise
1037 * @see cxJsonIsTrue() 1101 * @see cxJsonIsTrue()
1038 * @see cxJsonIsFalse() 1102 * @see cxJsonIsFalse()
1039 */ 1103 */
1040 cx_attr_nonnull 1104 cx_attr_nonnull
1041 static inline bool cxJsonIsBool(const CxJsonValue *value) { 1105 static inline bool cxJsonIsBool(const CxJsonValue *value) {
1042 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL; 1106 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
1043 } 1107 }
1044 1108
1045 /** 1109 /**
1046 * Checks if the specified value is \c true. 1110 * Checks if the specified value is @c true.
1047 * 1111 *
1048 * \remark Be advised, that this is not the same as 1112 * @remark Be advised, that this is not the same as
1049 * testing \c !cxJsonIsFalse(v). 1113 * testing @c !cxJsonIsFalse(v).
1050 * 1114 *
1051 * @param value a pointer to the value 1115 * @param value a pointer to the value
1052 * @return true if the value is \c true, false otherwise 1116 * @retval true the value is @c true
1117 * @retval false otherwise
1053 * @see cxJsonIsBool() 1118 * @see cxJsonIsBool()
1054 * @see cxJsonIsFalse() 1119 * @see cxJsonIsFalse()
1055 */ 1120 */
1056 cx_attr_nonnull 1121 cx_attr_nonnull
1057 static inline bool cxJsonIsTrue(const CxJsonValue *value) { 1122 static inline bool cxJsonIsTrue(const CxJsonValue *value) {
1058 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE; 1123 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
1059 } 1124 }
1060 1125
1061 /** 1126 /**
1062 * Checks if the specified value is \c false. 1127 * Checks if the specified value is @c false.
1063 * 1128 *
1064 * \remark Be advised, that this is not the same as 1129 * @remark Be advised, that this is not the same as
1065 * testing \c !cxJsonIsTrue(v). 1130 * testing @c !cxJsonIsTrue(v).
1066 * 1131 *
1067 * @param value a pointer to the value 1132 * @param value a pointer to the value
1068 * @return true if the value is \c false, false otherwise 1133 * @retval true the value is @c false
1134 * @retval false otherwise
1069 * @see cxJsonIsBool() 1135 * @see cxJsonIsBool()
1070 * @see cxJsonIsTrue() 1136 * @see cxJsonIsTrue()
1071 */ 1137 */
1072 cx_attr_nonnull 1138 cx_attr_nonnull
1073 static inline bool cxJsonIsFalse(const CxJsonValue *value) { 1139 static inline bool cxJsonIsFalse(const CxJsonValue *value) {
1074 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE; 1140 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
1075 } 1141 }
1076 1142
1077 /** 1143 /**
1078 * Checks if the specified value is \c null. 1144 * Checks if the specified value is @c null.
1079 * 1145 *
1080 * @param value a pointer to the value 1146 * @param value a pointer to the value
1081 * @return true if the value is \c null, false otherwise 1147 * @retval true the value is @c null
1148 * @retval false otherwise
1082 * @see cxJsonIsLiteral() 1149 * @see cxJsonIsLiteral()
1083 */ 1150 */
1084 cx_attr_nonnull 1151 cx_attr_nonnull
1085 static inline bool cxJsonIsNull(const CxJsonValue *value) { 1152 static inline bool cxJsonIsNull(const CxJsonValue *value) {
1086 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; 1153 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
1087 } 1154 }
1088 1155
1089 /** 1156 /**
1090 * Obtains a C string from the given JSON value. 1157 * Obtains a C string from the given JSON value.
1091 * 1158 *
1092 * If the \p value is not a string, the behavior is undefined. 1159 * If the @p value is not a string, the behavior is undefined.
1093 * 1160 *
1094 * @param value the JSON value 1161 * @param value the JSON value
1095 * @return the value represented as C string 1162 * @return the value represented as C string
1096 * @see cxJsonIsString() 1163 * @see cxJsonIsString()
1097 */ 1164 */
1102 } 1169 }
1103 1170
1104 /** 1171 /**
1105 * Obtains a UCX string from the given JSON value. 1172 * Obtains a UCX string from the given JSON value.
1106 * 1173 *
1107 * If the \p value is not a string, the behavior is undefined. 1174 * If the @p value is not a string, the behavior is undefined.
1108 * 1175 *
1109 * @param value the JSON value 1176 * @param value the JSON value
1110 * @return the value represented as UCX string 1177 * @return the value represented as UCX string
1111 * @see cxJsonIsString() 1178 * @see cxJsonIsString()
1112 */ 1179 */
1116 } 1183 }
1117 1184
1118 /** 1185 /**
1119 * Obtains a mutable UCX string from the given JSON value. 1186 * Obtains a mutable UCX string from the given JSON value.
1120 * 1187 *
1121 * If the \p value is not a string, the behavior is undefined. 1188 * If the @p value is not a string, the behavior is undefined.
1122 * 1189 *
1123 * @param value the JSON value 1190 * @param value the JSON value
1124 * @return the value represented as mutable UCX string 1191 * @return the value represented as mutable UCX string
1125 * @see cxJsonIsString() 1192 * @see cxJsonIsString()
1126 */ 1193 */
1130 } 1197 }
1131 1198
1132 /** 1199 /**
1133 * Obtains a double-precision floating point value from the given JSON value. 1200 * Obtains a double-precision floating point value from the given JSON value.
1134 * 1201 *
1135 * If the \p value is not a JSON number, the behavior is undefined. 1202 * If the @p value is not a JSON number, the behavior is undefined.
1136 * 1203 *
1137 * @param value the JSON value 1204 * @param value the JSON value
1138 * @return the value represented as double 1205 * @return the value represented as double
1139 * @see cxJsonIsNumber() 1206 * @see cxJsonIsNumber()
1140 */ 1207 */
1148 } 1215 }
1149 1216
1150 /** 1217 /**
1151 * Obtains a 64-bit signed integer from the given JSON value. 1218 * Obtains a 64-bit signed integer from the given JSON value.
1152 * 1219 *
1153 * If the \p value is not a JSON number, the behavior is undefined. 1220 * If the @p value is not a JSON number, the behavior is undefined.
1154 * If it is a JSON number, but not an integer, the value will be 1221 * If it is a JSON number, but not an integer, the value will be
1155 * converted to an integer, possibly losing precision. 1222 * converted to an integer, possibly losing precision.
1156 * 1223 *
1157 * @param value the JSON value 1224 * @param value the JSON value
1158 * @return the value represented as double 1225 * @return the value represented as double
1169 } 1236 }
1170 1237
1171 /** 1238 /**
1172 * Obtains a Boolean value from the given JSON value. 1239 * Obtains a Boolean value from the given JSON value.
1173 * 1240 *
1174 * If the \p value is not a JSON literal, the behavior is undefined. 1241 * If the @p value is not a JSON literal, the behavior is undefined.
1175 * The \c null literal is interpreted as \c false. 1242 * The @c null literal is interpreted as @c false.
1176 * 1243 *
1177 * @param value the JSON value 1244 * @param value the JSON value
1178 * @return the value represented as double 1245 * @return the value represented as double
1179 * @see cxJsonIsLiteral() 1246 * @see cxJsonIsLiteral()
1180 */ 1247 */
1184 } 1251 }
1185 1252
1186 /** 1253 /**
1187 * Returns the size of a JSON array. 1254 * Returns the size of a JSON array.
1188 * 1255 *
1189 * If the \p value is not a JSON array, the behavior is undefined. 1256 * If the @p value is not a JSON array, the behavior is undefined.
1190 * 1257 *
1191 * @param value the JSON value 1258 * @param value the JSON value
1192 * @return the size of the array 1259 * @return the size of the array
1193 * @see cxJsonIsArray() 1260 * @see cxJsonIsArray()
1194 */ 1261 */
1198 } 1265 }
1199 1266
1200 /** 1267 /**
1201 * Returns an element from a JSON array. 1268 * Returns an element from a JSON array.
1202 * 1269 *
1203 * If the \p value is not a JSON array, the behavior is undefined. 1270 * If the @p value is not a JSON array, the behavior is undefined.
1204 * 1271 *
1205 * This function guarantees to return a value. If the index is 1272 * This function guarantees to return a value. If the index is
1206 * out of bounds, the returned value will be of type 1273 * out of bounds, the returned value will be of type
1207 * #CX_JSON_NOTHING, but never \c NULL. 1274 * #CX_JSON_NOTHING, but never @c NULL.
1208 * 1275 *
1209 * @param value the JSON value 1276 * @param value the JSON value
1210 * @param index the index in the array 1277 * @param index the index in the array
1211 * @return the value at the specified index 1278 * @return the value at the specified index
1212 * @see cxJsonIsArray() 1279 * @see cxJsonIsArray()
1213 */ 1280 */
1214 cx_attr_nonnull 1281 cx_attr_nonnull
1215 cx_attr_returns_nonnull 1282 cx_attr_returns_nonnull
1283 cx_attr_export
1216 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index); 1284 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index);
1217 1285
1218 /** 1286 /**
1219 * Returns an iterator over the JSON array elements. 1287 * Returns an iterator over the JSON array elements.
1220 * 1288 *
1221 * The iterator yields values of type \c CxJsonValue* . 1289 * The iterator yields values of type @c CxJsonValue* .
1222 * 1290 *
1223 * If the \p value is not a JSON array, the behavior is undefined. 1291 * If the @p value is not a JSON array, the behavior is undefined.
1224 * 1292 *
1225 * @param value the JSON value 1293 * @param value the JSON value
1226 * @return an iterator over the array elements 1294 * @return an iterator over the array elements
1227 * @see cxJsonIsArray() 1295 * @see cxJsonIsArray()
1228 */ 1296 */
1229 cx_attr_nonnull 1297 cx_attr_nonnull
1230 cx_attr_nodiscard 1298 cx_attr_nodiscard
1299 cx_attr_export
1231 CxIterator cxJsonArrIter(const CxJsonValue *value); 1300 CxIterator cxJsonArrIter(const CxJsonValue *value);
1232 1301
1233 /** 1302 /**
1234 * Returns an iterator over the JSON object members. 1303 * Returns an iterator over the JSON object members.
1235 * 1304 *
1236 * The iterator yields values of type \c CxJsonObjValue* which 1305 * The iterator yields values of type @c CxJsonObjValue* which
1237 * contain the name and value of the member. 1306 * contain the name and value of the member.
1238 * 1307 *
1239 * If the \p value is not a JSON object, the behavior is undefined. 1308 * If the @p value is not a JSON object, the behavior is undefined.
1240 * 1309 *
1241 * @param value the JSON value 1310 * @param value the JSON value
1242 * @return an iterator over the object members 1311 * @return an iterator over the object members
1243 * @see cxJsonIsObject() 1312 * @see cxJsonIsObject()
1244 */ 1313 */
1245 cx_attr_nonnull 1314 cx_attr_nonnull
1246 cx_attr_nodiscard 1315 cx_attr_nodiscard
1316 cx_attr_export
1247 CxIterator cxJsonObjIter(const CxJsonValue *value); 1317 CxIterator cxJsonObjIter(const CxJsonValue *value);
1248 1318
1249 /** 1319 /**
1250 * @copydoc cxJsonObjGet() 1320 * @copydoc cxJsonObjGet()
1251 */ 1321 */
1252 cx_attr_nonnull 1322 cx_attr_nonnull
1253 cx_attr_returns_nonnull 1323 cx_attr_returns_nonnull
1324 cx_attr_export
1254 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name); 1325 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name);
1255 1326
1256 #ifdef __cplusplus 1327 #ifdef __cplusplus
1257 } // extern "C" 1328 } // extern "C"
1258 1329
1259 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxstring name) { 1330 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxstring name) {
1260 return cx_json_obj_get_cxstr(value, name); 1331 return cx_json_obj_get_cxstr(value, name);
1261 } 1332 }
1262 1333
1263 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxmutstr name) { 1334 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxmutstr name) {
1264 return cx_json_obj_get_cxstr(value, cx_strcast(name)); 1335 return cx_json_obj_get_cxstr(value, cx_strcast(name));
1265 } 1336 }
1266 1337
1267 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) { 1338 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
1268 return cx_json_obj_get_cxstr(value, cx_str(name)); 1339 return cx_json_obj_get_cxstr(value, cx_str(name));
1269 } 1340 }
1270 1341
1271 extern "C" { 1342 extern "C" {
1272 #else 1343 #else
1273 /** 1344 /**
1274 * Returns a value corresponding to a key in a JSON object. 1345 * Returns a value corresponding to a key in a JSON object.
1275 * 1346 *
1276 * If the \p value is not a JSON object, the behavior is undefined. 1347 * If the @p value is not a JSON object, the behavior is undefined.
1277 * 1348 *
1278 * This function guarantees to return a JSON value. If the 1349 * This function guarantees to return a JSON value. If the
1279 * object does not contain \p name, the returned JSON value 1350 * object does not contain @p name, the returned JSON value
1280 * will be of type #CX_JSON_NOTHING, but never \c NULL. 1351 * will be of type #CX_JSON_NOTHING, but never @c NULL.
1281 * 1352 *
1282 * @param value the JSON object 1353 * @param value the JSON object
1283 * @param name the key to look up 1354 * @param name the key to look up
1284 * @return the value corresponding to the key 1355 * @return the value corresponding to the key
1285 * @see cxJsonIsObject() 1356 * @see cxJsonIsObject()

mercurial