Ejemplo n.º 1
0
        protected static JObject _ParseJObject(CharReader reader)
        {
            JObject result = new JObject();
            char    c;

            try
            {
                _SkipWhitespace(reader);
                if (reader.Read() != '{')
                {
                    throw new SystemException("Begining brace expected");
                }
                string key;
                object value;
                _SkipWhitespace(reader);
                while (reader.Peek() != '}')
                {
                    _SkipWhitespace(reader);
                    if (reader.Peek() == ',')
                    {
                        // allow extraneous commas
                        reader.Read(); // gobble char
                        _SkipWhitespace(reader);
                        continue;
                    }
                    if (reader.Peek() != '\"')
                    {
                        throw new SystemException("Quote char expected");
                    }
                    key = _GetStringValue(reader);
                    _SkipWhitespace(reader);
                    if (reader.Read() != ':')
                    {
                        throw new SystemException("Colon char expected");
                    }
                    _SkipWhitespace(reader);
                    value = _GetValue(reader);
                    result.Add(key, value);
                    _SkipWhitespace(reader);
                    c = reader.Peek();
                    if (c != ',' && c != '}')
                    {
                        throw new SystemException("Comma or end brace expected");
                    }
                    if (c == ',')
                    {
                        reader.Read(); // gobble comma char
                        _SkipWhitespace(reader);
                    }
                }
                reader.Read(); // gobble } char
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Invalid JObject input\r\n{ex.Message}");
            }
            return(result);
        }
Ejemplo n.º 2
0
        protected static JArray _ParseJArray(CharReader reader)
        {
            JArray result = new JArray();
            char   c;

            try
            {
                _SkipWhitespace(reader);
                c = reader.Read();
                if (c != '[')
                {
                    throw new SystemException("Begining bracket expected");
                }
                object value;
                _SkipWhitespace(reader);
                while (reader.Peek() != ']')
                {
                    _SkipWhitespace(reader);
                    if (reader.Peek() == ',')
                    {
                        // allow extraneous commas
                        reader.Read(); // gobble char
                        _SkipWhitespace(reader);
                        continue;
                    }
                    value = _GetValue(reader);
                    result.Add(value);
                    _SkipWhitespace(reader);
                    c = reader.Peek();
                    if (c != ',' && c != ']')
                    {
                        throw new SystemException("Comma or end bracket expected");
                    }
                    if (c == ',')
                    {
                        reader.Read(); // gobble comma char
                        _SkipWhitespace(reader);
                    }
                }
                reader.Read(); // gobble end bracket char
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Invalid JArray input\r\n{ex.Message}");
            }
            return(result);
        }
Ejemplo n.º 3
0
        public static object Parse(CharReader reader)
        {
            _SkipWhitespace(reader);
            char startChar;

            startChar = reader.Peek();
            if (startChar == '{')
            {
                return(_ParseJObject(reader));
            }
            else if (startChar == '[')
            {
                return(_ParseJArray(reader));
            }
            else
            {
                throw new ArgumentException("Invalid JSON input");
            }
        }
Ejemplo n.º 4
0
        private static object _GetValue(CharReader reader)
        {
            char   c;
            string token;

            c = reader.Peek();
            switch (c)
            {
            case 'n':
                token = _GetToken(reader);
                if (token != "null")
                {
                    throw new ArgumentException($"Invalid token: {token}");
                }
                return(null);

            case 'f':
                token = _GetToken(reader);
                if (token != "false")
                {
                    throw new ArgumentException($"Invalid token: {token}");
                }
                return(false);

            case 't':
                token = _GetToken(reader);
                if (token != "true")
                {
                    throw new ArgumentException($"Invalid token: {token}");
                }
                return(true);

            case '\"':
                return(_GetStringValue(reader));

            case '[':
                return(_ParseJArray(reader));

            case '{':
                return(_ParseJObject(reader));

            case '-':
            case '+':
            case '.':
            case 'e':
            case 'E':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return(_GetNumeric(reader));

            default:
                throw new ArgumentException($"Unexpected character: {c}");
            }
        }
Ejemplo n.º 5
0
        protected static void _SkipWhitespace(CharReader reader)
        {
            char currChar;

            while (true)
            {
                if (reader.EOF())
                {
                    break;
                }
                currChar = reader.Peek();
                // use c# definition of whitespace
                if (char.IsWhiteSpace(currChar))
                {
                    reader.Read(); // gobble char
                    continue;
                }
                // check for comments
                if (currChar == '/')
                {
                    reader.Read();       // gobble char
                    char nextChar = reader.Read();
                    if (nextChar == '/') // to EOF
                    {
                        while (true)
                        {
                            // not error if EOF before CR-LF
                            if (reader.EOF())
                            {
                                break;
                            }
                            currChar = reader.Read();
                            if (currChar == '\r' || currChar == '\n')
                            {
                                break;
                            }
                        }
                        continue;
                    }
                    else if (nextChar == '*') /* to */
                    {
                        nextChar = ' ';       // clear for comparison
                        while (true)
                        {
                            // will throw an error if EOF before '*/' is found
                            currChar = nextChar;
                            nextChar = reader.Read();
                            if (currChar == '*' && nextChar == '/')
                            {
                                break;
                            }
                        }
                        continue;
                    }
                    else // not a comment
                    {
                        // push back in reverse order
                        reader.Push(nextChar);
                        reader.Push(currChar);
                        break;
                    }
                }
                break;
            }
        }