Exemplo n.º 1
0
        private JsonValue ParseObject(ICharReader charReader)
        {
            var members = new List <JsonObjectMember>();

            if (!charReader.TrimRead('{'))
            {
                throw new JsonException("Expected '{' when parsing json object.");
            }

            var peek = charReader.TrimThenPeek();

            if (!peek.HasValue)
            {
                throw new JsonException("Unterminated object");
            }

            while (peek.Value != '}')
            {
                if (members.Any())
                {
                    if (!charReader.TrimRead(','))
                    {
                        throw new JsonException("Missing comma separater between array items.");
                    }
                }
                members.Add(ParseObjectMember(charReader));
                peek = charReader.TrimThenPeek();
                if (!peek.HasValue)
                {
                    throw new JsonException("Unterminated object");
                }
            }
            charReader.Read(); // }
            return(new JsonObject(members));
        }
Exemplo n.º 2
0
        private JsonValue ParseArray(ICharReader charReader)
        {
            charReader.TrimRead(); // [

            var items = new List <JsonValue>();

            while (charReader.TrimThenPeek() != ']')
            {
                if (items.Any())
                {
                    if (!charReader.TrimRead(','))
                    {
                        throw new JsonException("Missing comma separater between array items.");
                    }
                }
                items.Add(ParseJsonValue(charReader));
            }
            charReader.Read(); // ]

            return(new JsonArray(items));
        }
Exemplo n.º 3
0
        private JsonObjectMember ParseObjectMember(ICharReader charReader)
        {
            var name = ParseString(charReader);

            if (!charReader.TrimRead(':'))
            {
                throw new JsonException("expected ':' after member name.");
            }
            var value = ParseJsonValue(charReader);

            return(new JsonObjectMember(name.Value, value));
        }
Exemplo n.º 4
0
        private JsonString ParseString(ICharReader charReader)
        {
            var delimiter = charReader.TrimRead();

            if (delimiter != '\'' && delimiter != '"')
            {
                throw new JsonException("Strings must be delimiated with either single or double quotes");
            }

            var sb = new StringBuilder();
            var c  = GetNextStringCharacter(charReader, delimiter);

            while (c.HasValue)
            {
                sb.Append(c);
                c = GetNextStringCharacter(charReader, delimiter);
            }

            return(new JsonString(sb.ToString()));
        }