Changeset 61716 in vbox for trunk/src/VBox/Runtime/common/misc/json.cpp
- Timestamp:
- Jun 15, 2016 1:25:52 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/json.cpp
r61708 r61716 641 641 rc = rtJsonTokenizerGetEos(pTokenizer, pToken); 642 642 else if (ch == '{') 643 rc = pToken->enmClass == RTJSONTOKENCLASS_BEGIN_OBJECT; 643 { 644 pToken->enmClass = RTJSONTOKENCLASS_BEGIN_OBJECT; 645 rtJsonTokenizerSkipCh(pTokenizer); 646 } 644 647 else if (ch == '}') 645 rc = pToken->enmClass == RTJSONTOKENCLASS_END_OBJECT; 648 { 649 pToken->enmClass = RTJSONTOKENCLASS_END_OBJECT; 650 rtJsonTokenizerSkipCh(pTokenizer); 651 } 646 652 else if (ch == '[') 647 rc = pToken->enmClass == RTJSONTOKENCLASS_BEGIN_ARRAY; 653 { 654 pToken->enmClass = RTJSONTOKENCLASS_BEGIN_ARRAY; 655 rtJsonTokenizerSkipCh(pTokenizer); 656 } 648 657 else if (ch == ']') 649 rc = pToken->enmClass == RTJSONTOKENCLASS_END_ARRAY; 658 { 659 pToken->enmClass = RTJSONTOKENCLASS_END_ARRAY; 660 rtJsonTokenizerSkipCh(pTokenizer); 661 } 650 662 else if (ch == ':') 651 rc = pToken->enmClass == RTJSONTOKENCLASS_NAME_SEPARATOR; 663 { 664 pToken->enmClass = RTJSONTOKENCLASS_NAME_SEPARATOR; 665 rtJsonTokenizerSkipCh(pTokenizer); 666 } 652 667 else if (ch == ',') 653 rc = pToken->enmClass == RTJSONTOKENCLASS_VALUE_SEPARATOR; 668 { 669 pToken->enmClass = RTJSONTOKENCLASS_VALUE_SEPARATOR; 670 rtJsonTokenizerSkipCh(pTokenizer); 671 } 654 672 else 655 673 { … … 775 793 RTJsonValueRelease(pThis->Type.Object.papValues[i]); 776 794 } 795 RTMemFree(pThis->Type.Object.papszNames); 796 RTMemFree(pThis->Type.Object.papValues); 777 797 break; 778 798 case RTJSONVALTYPE_ARRAY: 779 799 for (unsigned i = 0; i < pThis->Type.Array.cItems; i++) 780 800 RTJsonValueRelease(pThis->Type.Array.papItems[i]); 801 RTMemFree(pThis->Type.Array.papItems); 781 802 break; 782 803 case RTJSONVALTYPE_STRING: … … 826 847 PRTJSONTOKEN pToken = NULL; 827 848 uint32_t cItems = 0; 849 uint32_t cItemsMax = 0; 850 PRTJSONVALINT *papItems = NULL; 828 851 829 852 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 830 853 while ( RT_SUCCESS(rc) 831 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY) 854 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY 855 && pToken->enmClass != RTJSONTOKENCLASS_EOS) 832 856 { 833 857 PRTJSONVALINT pVal = NULL; … … 835 859 if (RT_SUCCESS(rc)) 836 860 { 861 if (cItems == cItemsMax) 862 { 863 cItemsMax += 10; 864 PRTJSONVALINT *papItemsNew = (PRTJSONVALINT *)RTMemRealloc(papItems, cItemsMax * sizeof(PRTJSONVALINT)); 865 if (RT_UNLIKELY(!papItemsNew)) 866 { 867 rc = VERR_NO_MEMORY; 868 break; 869 } 870 papItems = papItemsNew; 871 } 872 873 Assert(cItems < cItemsMax); 874 papItems[cItems] = pVal; 837 875 cItems++; 838 /** @todo: Add value to array. */839 876 } 840 877 841 /* Next token. */ 842 rtJsonTokenizerConsume(pTokenizer); 843 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 878 /* Skip value separator and continue with next token. */ 879 if (rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_VALUE_SEPARATOR)) 880 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 881 else 882 rc = VERR_JSON_MALFORMED; 844 883 } 845 884 846 885 if (RT_SUCCESS(rc)) 847 886 { 848 Assert(pToken->enmClass == RTJSONTOKENCLASS_END_ARRAY); 849 rtJsonTokenizerConsume(pTokenizer); 850 pJsonVal->Type.Array.cItems = cItems; 887 if (pToken->enmClass == RTJSONTOKENCLASS_END_ARRAY) 888 { 889 rtJsonTokenizerConsume(pTokenizer); 890 pJsonVal->Type.Array.cItems = cItems; 891 pJsonVal->Type.Array.papItems = papItems; 892 } 893 else 894 rc = VERR_JSON_MALFORMED; 895 } 896 897 if (RT_FAILURE(rc)) 898 { 899 for (uint32_t i = 0; i < cItems; i++) 900 RTJsonValueRelease(papItems[i]); 901 RTMemFree(papItems); 851 902 } 852 903 … … 867 918 PRTJSONTOKEN pToken = NULL; 868 919 uint32_t cMembers = 0; 920 uint32_t cMembersMax = 0; 921 PRTJSONVALINT *papValues = NULL; 922 char **papszNames = NULL; 869 923 870 924 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); … … 883 937 if (RT_SUCCESS(rc)) 884 938 { 939 if (cMembers == cMembersMax) 940 { 941 cMembersMax += 10; 942 PRTJSONVALINT *papValuesNew = (PRTJSONVALINT *)RTMemRealloc(papValues, cMembersMax * sizeof(PRTJSONVALINT)); 943 char **papszNamesNew = (char **)RTMemRealloc(papValues, cMembersMax * sizeof(char *)); 944 if (RT_UNLIKELY(!papValuesNew || !papszNamesNew)) 945 { 946 rc = VERR_NO_MEMORY; 947 break; 948 } 949 } 950 951 Assert(cMembers < cMembers); 952 papszNames[cMembers] = pszName; 953 papValues[cMembers] = pVal; 885 954 cMembers++; 886 /** @todo: Add name/value pair to object. */887 955 888 956 /* Next token. */ 889 rtJsonTokenizerConsume(pTokenizer);890 957 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken); 891 958 } … … 901 968 rtJsonTokenizerConsume(pTokenizer); 902 969 pJsonVal->Type.Object.cMembers = cMembers; 970 pJsonVal->Type.Object.papValues = papValues; 971 pJsonVal->Type.Object.papszNames = papszNames; 903 972 } 904 973 else 905 974 rc = VERR_JSON_MALFORMED; 975 } 976 977 if (RT_FAILURE(rc)) 978 { 979 for (uint32_t i = 0; i < cMembers; i++) 980 { 981 RTJsonValueRelease(papValues[i]); 982 RTStrFree(papszNames[i]); 983 } 984 RTMemFree(papValues); 985 RTMemFree(papszNames); 906 986 } 907 987 … … 927 1007 { 928 1008 case RTJSONTOKENCLASS_BEGIN_ARRAY: 1009 rtJsonTokenizerConsume(pTokenizer); 929 1010 pVal = rtJsonValueCreate(RTJSONVALTYPE_ARRAY); 930 1011 if (RT_LIKELY(pVal)) … … 932 1013 break; 933 1014 case RTJSONTOKENCLASS_BEGIN_OBJECT: 1015 rtJsonTokenizerConsume(pTokenizer); 934 1016 pVal = rtJsonValueCreate(RTJSONVALTYPE_OBJECT); 935 1017 if (RT_LIKELY(pVal)) … … 940 1022 if (RT_LIKELY(pVal)) 941 1023 pVal->Type.String.pszStr = pToken->Class.String.pszStr; 1024 rtJsonTokenizerConsume(pTokenizer); 942 1025 break; 943 1026 case RTJSONTOKENCLASS_NUMBER: … … 945 1028 if (RT_LIKELY(pVal)) 946 1029 pVal->Type.Number.i64Num = pToken->Class.Number.i64Num; 1030 rtJsonTokenizerConsume(pTokenizer); 947 1031 break; 948 1032 case RTJSONTOKENCLASS_NULL: 1033 rtJsonTokenizerConsume(pTokenizer); 949 1034 pVal = rtJsonValueCreate(RTJSONVALTYPE_NULL); 950 1035 break; 951 1036 case RTJSONTOKENCLASS_FALSE: 1037 rtJsonTokenizerConsume(pTokenizer); 952 1038 pVal = rtJsonValueCreate(RTJSONVALTYPE_FALSE); 953 1039 break; 954 1040 case RTJSONTOKENCLASS_TRUE: 1041 rtJsonTokenizerConsume(pTokenizer); 955 1042 pVal = rtJsonValueCreate(RTJSONVALTYPE_TRUE); 956 1043 break;
Note:
See TracChangeset
for help on using the changeset viewer.