Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #3
0
        private JsonValue ParseJsonValue(ICharReader charReader)
        {
            var leadingCharacter = charReader.TrimThenPeek();

            if (!leadingCharacter.HasValue)
            {
                throw new JsonException("Unexpected end of stream");
            }
            var valueType = IdentifyValueType(leadingCharacter.Value);

            switch (valueType)
            {
            case JsonValueType.String:
                return(ParseString(charReader));

            case JsonValueType.Number:
                return(ParseNumber(charReader));

            case JsonValueType.Object:
                return(ParseObject(charReader));

            case JsonValueType.Array:
                return(ParseArray(charReader));

            case JsonValueType.True:
                return(ParseTrue(charReader));

            case JsonValueType.False:
                return(ParseFalse(charReader));

            case JsonValueType.Null:
                return(ParseNull(charReader));

            case JsonValueType.Unrecognised:
                throw new JsonException($"Unexpected character '{leadingCharacter.Value}'");

            default:
                throw new ArgumentOutOfRangeException(nameof(valueType), valueType, null);
            }
        }