| 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 } |
| 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() |