コード例 #1
0
ファイル: JsonReader.cs プロジェクト: jithuin/infogeezer
        private static object ParseExpression(JsonToken token, TokenStream ts)
        {
            if (token == null)
            {
                token = ts.GetNextToken();
            }

            switch (token.Type)
            {
                case JsonToken.TokenType.String:
                    logger.Debug("Found String " + token.Value);
                    return token.Value;
                case JsonToken.TokenType.Number:
                    logger.Debug("Found Number " + token.Number);
                    return token.Number;
                case JsonToken.TokenType.False:
                    logger.Debug("Found False");
                    return false;
                case JsonToken.TokenType.True:
                    logger.Debug("Found True");
                    return true;
                case JsonToken.TokenType.Null:
                    logger.Debug("Found Null");
                    return null;
                case JsonToken.TokenType.ObjectStart:
                    logger.Debug("Found ObjectStart");
                    return ParseObject(ts);
                case JsonToken.TokenType.ArrayStart:
                    logger.Debug("Found ArrayStart");
                    return ParseArray(ts);
                case JsonToken.TokenType.Undefined:
                default:
                    throw new ArgumentException("parse error");
            }
        }
コード例 #2
0
 public void JsonTokenConstructorTest()
 {
     JsonToken target = new JsonToken();
     Assert.IsNotNull(target);
 }
コード例 #3
0
        /// <summary>
        /// From the current position, extract the next token
        /// </summary>
        /// <returns>The next token, or null if the stream is at the EOF</returns>
        public JsonToken GetNextToken()
        {
            var token = new JsonToken();
            try
            {
                // Read char (and first skip over all whitespaces)
                // We use .Read() instead of .Peek() here, as Read() is blocking.
                // .Peek() would suggest an EOF even if there is none when used in combination a WebRequest
                char cur;
                do
                {
                    int read = reader.Read();

                    // Check for EOF
                    if (read == -1)
                    {
                        return null;
                    }

                    cur = (char) read;
                } while (Char.IsWhiteSpace(cur));

                // Check for Json tokens
                switch (cur)
                {
                    case '{':
                        token.Type = JsonToken.TokenType.ObjectStart;
                        break;
                    case '}':
                        token.Type = JsonToken.TokenType.ObjectEnd;
                        break;
                    case '[':
                        token.Type = JsonToken.TokenType.ArrayStart;
                        break;
                    case ']':
                        token.Type = JsonToken.TokenType.ArrayEnd;
                        break;
                    case ':':
                        token.Type = JsonToken.TokenType.NameSeperator;
                        break;
                    case ',':
                        token.Type = JsonToken.TokenType.MemberSeperator;
                        break;
                    case '"':
                    case '\'':
                        ParseString(token, cur);
                        break;
                    case 'f':
                        GetFalseToken(token);
                        break;
                    case 't':
                        // this might be the true token
                        GetTrueToken(token);
                        break;
                    case 'n':
                        // this might be nul
                        GetNullToken(token);
                        break;
                    default:
                        token.Type = JsonToken.TokenType.Undefined;
                        if (Char.IsNumber(cur) || cur == '-')
                        {
                            var sb = new StringBuilder(BuilderBufferSize);
                            sb.Append(cur);
                            while (IsTokenSeperator((char) reader.Peek()) == false || ((char) reader.Peek() == '.') ||
                                   ((char) reader.Peek() == '+') || ((char) reader.Peek() == '-'))
                            {
                                sb.Append((char) reader.Read());
                            }
                            token.Value = sb.ToString();
                            decimal decNumber;
                            if (Decimal.TryParse(
                                token.Value, NumberStyles.Number | NumberStyles.AllowExponent,
                                CultureInfo.InvariantCulture, out decNumber))
                            {
                                token.Number = decNumber;
                                token.Type = JsonToken.TokenType.Number;
                            }
                        }
                        break;
                }
            }
            catch (IOException ex)
            {
                throw new IOException("Unable to retrieve the next token", ex);
            }
            return token;
        }
コード例 #4
0
        /// <summary>
        /// Parses a Json String as per http://www.json.org/ acknowledging escaped chars
        /// </summary>
        private void ParseString(JsonToken token, char cur)
        {
            char seperator = cur;
            token.Type = JsonToken.TokenType.String;

            // let's read in the string
            var sb = new StringBuilder(BuilderBufferSize);
            char next;
            while ((next = (char) reader.Read()) != seperator)
            {
                if (next == '\\')
                {
                    next = (char) reader.Read();
                    switch (next)
                    {
                        case 'n':
                            next = '\n';
                            break;
                        case 'r':
                            next = '\r';
                            break;
                        case 't':
                            next = '\t';
                            break;
                        case '\'':
                            next = '\'';
                            break;
                        case '/':
                            next = '/';
                            break;
                        case '"':
                            next = '\"';
                            break;
                        case '\\':
                            next = '\\';
                            break;
                        case 'b':
                            // backspace
                            next = '\b';
                            break;
                        case 'u':
                            // 4 digit hexadecimal unicode escape
                            char[] escapedCharAry = new char[4];
                            for (int i = 0; i < 4; i++)
                            {
                                escapedCharAry[i] = (char) reader.Read();
                            }

                            String escapedCharStr = new String(escapedCharAry);
                            int escapedCharInt = int.Parse(
                                escapedCharStr, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo);
                            next = Convert.ToChar(escapedCharInt);
                            break;
                        default:
                            break;
                    }
                }
                sb.Append(next);
            }
            token.Value = sb.ToString();
        }
コード例 #5
0
 // starts at the a of the false
 private void GetTrueToken(JsonToken token)
 {
     token.Type = JsonToken.TokenType.Undefined;
     // this might be the false token
     if (LookupToken("rue"))
     {
         token.Type = JsonToken.TokenType.True;
     }
     return;
 }
コード例 #6
0
 // starts at the a of the false
 private void GetNullToken(JsonToken token)
 {
     token.Type = JsonToken.TokenType.Undefined;
     // this might be the false token
     if (LookupToken("ull"))
     {
         token.Type = JsonToken.TokenType.Null;
     }
     return;
 }
コード例 #7
0
ファイル: TokenStream.cs プロジェクト: artzub/LoggenCSG
        /// <summary>
        /// Parses a Json String as per http://www.json.org/ acknowledging escaped chars
        /// </summary>
        private void ParseString(JsonToken token, char cur)
        {
            char seperator = cur;

            token.Type = JsonToken.TokenType.String;

            // let's read in the string
            var  sb = new StringBuilder(BuilderBufferSize);
            char next;

            while ((next = (char)reader.Read()) != seperator)
            {
                if (next == '\\')
                {
                    next = (char)reader.Read();
                    switch (next)
                    {
                    case 'n':
                        next = '\n';
                        break;

                    case 'r':
                        next = '\r';
                        break;

                    case 't':
                        next = '\t';
                        break;

                    case '\'':
                        next = '\'';
                        break;

                    case '/':
                        next = '/';
                        break;

                    case '"':
                        next = '\"';
                        break;

                    case '\\':
                        next = '\\';
                        break;

                    case 'b':
                        // backspace
                        next = '\b';
                        break;

                    case 'u':
                        // 4 digit hexadecimal unicode escape
                        char[] escapedCharAry = new char[4];
                        for (int i = 0; i < 4; i++)
                        {
                            escapedCharAry[i] = (char)reader.Read();
                        }

                        String escapedCharStr = new String(escapedCharAry);
                        int    escapedCharInt = int.Parse(
                            escapedCharStr, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo);
                        next = Convert.ToChar(escapedCharInt);
                        break;

                    default:
                        break;
                    }
                }
                sb.Append(next);
            }
            token.Value = sb.ToString();
        }
コード例 #8
0
ファイル: TokenStream.cs プロジェクト: artzub/LoggenCSG
        /// <summary>
        /// From the current position, extract the next token
        /// </summary>
        /// <returns>The next token, or null if the stream is at the EOF</returns>
        public JsonToken GetNextToken()
        {
            var token = new JsonToken();

            try
            {
                // Read char (and first skip over all whitespaces)
                // We use .Read() instead of .Peek() here, as Read() is blocking.
                // .Peek() would suggest an EOF even if there is none when used in combination a WebRequest
                char cur;
                do
                {
                    int read = reader.Read();

                    // Check for EOF
                    if (read == -1)
                    {
                        return(null);
                    }

                    cur = (char)read;
                } while (Char.IsWhiteSpace(cur));

                // Check for Json tokens
                switch (cur)
                {
                case '{':
                    token.Type = JsonToken.TokenType.ObjectStart;
                    break;

                case '}':
                    token.Type = JsonToken.TokenType.ObjectEnd;
                    break;

                case '[':
                    token.Type = JsonToken.TokenType.ArrayStart;
                    break;

                case ']':
                    token.Type = JsonToken.TokenType.ArrayEnd;
                    break;

                case ':':
                    token.Type = JsonToken.TokenType.NameSeperator;
                    break;

                case ',':
                    token.Type = JsonToken.TokenType.MemberSeperator;
                    break;

                case '"':
                case '\'':
                    ParseString(token, cur);
                    break;

                case 'f':
                    GetFalseToken(token);
                    break;

                case 't':
                    // this might be the true token
                    GetTrueToken(token);
                    break;

                case 'n':
                    // this might be nul
                    GetNullToken(token);
                    break;

                default:
                    token.Type = JsonToken.TokenType.Undefined;
                    if (Char.IsNumber(cur) || cur == '-')
                    {
                        var sb = new StringBuilder(BuilderBufferSize);
                        sb.Append(cur);
                        while (IsTokenSeperator((char)reader.Peek()) == false || ((char)reader.Peek() == '.') ||
                               ((char)reader.Peek() == '+') || ((char)reader.Peek() == '-'))
                        {
                            sb.Append((char)reader.Read());
                        }
                        token.Value = sb.ToString();
                        decimal decNumber;
                        if (Decimal.TryParse(
                                token.Value, NumberStyles.Number | NumberStyles.AllowExponent,
                                CultureInfo.InvariantCulture, out decNumber))
                        {
                            token.Number = decNumber;
                            token.Type   = JsonToken.TokenType.Number;
                        }
                    }
                    break;
                }
            }
            catch (IOException ex)
            {
                throw new IOException("Unable to retrieve the next token", ex);
            }
            return(token);
        }