示例#1
0
        static JSONDataItem GetJSONArray(JSONTokeniser tokeniser)
        {
            JSONArray    jsonArray = new JSONArray();;
            JSONDataItem data;

            // Check for empty array
            data = JSONData.GetJSONDataItem(tokeniser);
            if ((data.dataType == JSONDataType.JSON_NON_DATA) && ((JSONNonData)data).value.tokenType == JSONToken.TOKEN_CLOSE_SQUARE)
            {
                return(jsonArray);
            }

            while (true)
            {
                if (data.dataType == JSONDataType.JSON_NON_DATA)
                {
                    return(new JSONDataError("Expected data value, got " + data, tokeniser.GetCurrentSectionString()));
                }
                else if (data.dataType == JSONDataType.JSON_ERROR)
                {
                    return(data);
                }
                jsonArray.AddItem(data);

                data = JSONData.GetJSONDataItem(tokeniser);

                if (data.dataType == JSONDataType.JSON_NON_DATA)
                {
                    JSONToken token = ((JSONNonData)data).value;
                    if (token.tokenType == JSONToken.TOKEN_COMMA)
                    {
                        data = JSONData.GetJSONDataItem(tokeniser);
                        continue;
                    }
                    else if (token.tokenType == JSONToken.TOKEN_CLOSE_SQUARE)
                    {
                        break; //End of Array
                    }
                    else
                    {
                        return(new JSONDataError("Expected ',' or '], got " + token, tokeniser.GetCurrentSectionString()));
                    }
                }
                else
                {
                    return(new JSONDataError("Expected ',' or '], got " + data, tokeniser.GetCurrentSectionString()));
                }
            }

            return(jsonArray);
        }
示例#2
0
	JSONToken ParseNumberToken(int firstChar){
		int index = stringIndex-1;
		// First just get the full string
		while (character != ASCIICodes.CHR_SPACE && character != ASCIICodes.CHR_COMMA && 
               character != ASCIICodes.CHR_CLOSE_CURLY && character != ASCIICodes.CHR_CLOSE_SQUARE && character != ASCIICodes.CHR_NUL){
			NextChar();
		}
		if (character == ASCIICodes.CHR_NUL){
			ParseFailure("Unterminated Number");
			return JSONToken.CreateToken(JSONToken.TOKEN_UNKNOWN,null);
		}

		string numberString = jsonString.Slice(index,stringIndex-1);
		
		if (numberString.IndexOf(".") != -1 || numberString.IndexOf("e") != -1 || numberString.IndexOf("E") != -1){
		    return JSONToken.CreateToken(JSONToken.TOKEN_UNPARSED_FLOAT,numberString);
		} else {
			int value = ParseInteger(numberString);
			return JSONToken.CreateToken(JSONToken.TOKEN_INTEGER,value);
		} 
	}
示例#3
0
        public static JSONDataItem GetJSONDataItem(JSONTokeniser tokeniser)
        {
            JSONToken token = tokeniser.NextToken();

            //Print token
            switch (token.tokenType)
            {
            case JSONToken.TOKEN_OPEN_CURLY:
                return(GetJSONObject(tokeniser));

            case JSONToken.TOKEN_OPEN_SQUARE:
                return(GetJSONArray(tokeniser));

            //Boxing
            case JSONToken.TOKEN_STRING:
                return(new JSONString((string)(token.value), false));

            case JSONToken.TOKEN_FLOAT:
                return(new JSONFloat((float)token.value));

            case JSONToken.TOKEN_UNPARSED_FLOAT:
                return(new JSONFloat(token.value.ToString()));

            case JSONToken.TOKEN_INTEGER:
                return(new JSONInteger((int)token.value));

            case JSONToken.TOKEN_TRUE:
                return(new JSONBool(true));

            case JSONToken.TOKEN_FALSE:
                return(new JSONBool(false));

            case JSONToken.TOKEN_NULL:
                return(new JSONnull());

            default:
                return(new JSONNonData(token));
            }
        }
示例#4
0
	public JSONToken NextToken(){
		JSONToken retToken=null;
		SkipIgnored();

        switch (character){
			case ASCIICodes.CHR_OPEN_CURLY:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_OPEN_CURLY,"{");
                break;
			case ASCIICodes.CHR_CLOSE_CURLY:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_CLOSE_CURLY,"}");
                break;
			case ASCIICodes.CHR_OPEN_SQUARE:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_OPEN_SQUARE,"[");
                break;
			case ASCIICodes.CHR_CLOSE_SQUARE:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_CLOSE_SQUARE,"]");
                break;
			case ASCIICodes.CHR_COMMA:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_COMMA,",");
                break;
			case ASCIICodes.CHR_COLON:
				retToken = JSONToken.CreateToken(JSONToken.TOKEN_COLON,":");
                break;
			case ASCIICodes.CHR_LOWER_T:
				if (String.Compare(jsonString.Slice(stringIndex,stringIndex+3), "rue") == 0){
					stringIndex += 3;
					retToken = JSONToken.CreateToken(JSONToken.TOKEN_TRUE,"true");
				}
                break;
			case ASCIICodes.CHR_LOWER_F:
				if (String.Compare(jsonString.Slice(stringIndex,stringIndex+4), "alse") == 0){
					stringIndex += 4;
					retToken = JSONToken.CreateToken(JSONToken.TOKEN_FALSE,"false");
				}
                break;
			case ASCIICodes.CHR_LOWER_N:
				if (String.Compare(jsonString.Slice(stringIndex,stringIndex+3),"ull") == 0){
					stringIndex += 3;
					retToken = JSONToken.CreateToken(JSONToken.TOKEN_NULL,"null");
				}
                break;
			case ASCIICodes.CHR_DOUBLE_QUOTE:
				int startIndex = stringIndex;
        		int endIndex = jsonString.IndexOf("\"",stringIndex);
                while (endIndex != -1 && jsonString[endIndex-1] == ASCIICodes.CHR_BACKSLASH){
                    endIndex = jsonString.IndexOf("\"",endIndex+1);
                }
                if (endIndex == -1){
                    ParseFailure("Unterminated string");
                }
				
                retToken = JSONToken.CreateToken(JSONToken.TOKEN_STRING,jsonString.Slice(startIndex,endIndex));
				stringIndex = endIndex+1;
                break;
						
			default:
				// Is it a Number?
				if (character == ASCIICodes.CHR_HYPHEN || IsDigit(character)){
					return ParseNumberToken(character); // We return here because ParseNumberToken moves the token pointer forward
				} else if (character == ASCIICodes.CHR_NUL) {
					retToken = null; // End of string so just leave// 
				}
                break;								
		}

		if (retToken == null){
			ParseFailure("Unknown token, character: " + character.ToString());
			retToken = JSONToken.CreateToken(JSONToken.TOKEN_UNKNOWN,null);
		} else {
			NextChar();
		}
		return retToken;

	}
示例#5
0
 public JSONNonData(JSONToken token)
 {
     dataType = JSONDataType.JSON_NON_DATA;
     value    = token;
 }