Пример #1
0
        public void Test_TryParseDateTime_InvalidFormat(string jsonData)
        {
            DateTime value;
            bool     res = JsonValueParser.TryParseDateTime(jsonData, out value);

            Assert.That(res, Is.False);
        }
Пример #2
0
        /// <inheritdoc />
        public JsonItem ParseNext()
        {
            List <JsonItem> items = new List <JsonItem>();
            JsonItem        item;

            this.state = JsonArrayParseState.Start;
            while (this.state != JsonArrayParseState.End)
            {
                switch (this.state)
                {
                case JsonArrayParseState.Start:
                    if (this.Consume('['))
                    {
                        this.state = JsonArrayParseState.Value;
                        new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                    }
                    else
                    {
                        return(this.MakeError('['));
                    }
                    break;

                case JsonArrayParseState.Value:
                    if (this.Consume(']'))
                    {
                        this.state = JsonArrayParseState.End;
                    }
                    else
                    {
                        item = new JsonValueParser(this.Buffer).ParseNext();
                        if (item.IsError)
                        {
                            return(item);
                        }
                        items.Add(item);
                        new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                        this.state = JsonArrayParseState.Seperator;
                    }
                    break;

                case JsonArrayParseState.Seperator:
                    if (this.Consume(','))
                    {
                        new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                        this.state = JsonArrayParseState.Value;
                    }
                    else if (this.Consume(']'))
                    {
                        this.state = JsonArrayParseState.End;
                    }
                    else
                    {
                        return(this.MakeError(',', ']'));
                    }
                    break;
                }
            }
            return(new JsonArray(items));
        }
Пример #3
0
        public void Test_TryParseTime_Valid(string jsonData, string expectedData)
        {
            DateTime value;
            DateTime expected = DateTime.Parse(expectedData, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
            bool     res      = JsonValueParser.TryParseTime(jsonData, out value);

            Assert.That(res, Is.True);
            Assert.That(value, Is.EqualTo(expected));
        }
Пример #4
0
 /// <inheritdoc />
 public JsonItem ParseNext()
 {
     List<JsonItem> items = new List<JsonItem>();
     JsonItem item;
     this.state = JsonArrayParseState.Start;
     while (this.state != JsonArrayParseState.End)
     {
         switch (this.state)
         {
             case JsonArrayParseState.Start:
                 if (this.Consume('['))
                 {
                     this.state = JsonArrayParseState.Value;
                     new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                 }
                 else
                 {
                     return this.MakeError('[');
                 }
                 break;
             case JsonArrayParseState.Value:
                 if (this.Consume(']'))
                 {
                     this.state = JsonArrayParseState.End;
                 }
                 else
                 {
                     item = new JsonValueParser(this.Buffer).ParseNext();
                     if (item.IsError)
                     {
                         return item;
                     }
                     items.Add(item);
                     new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                     this.state = JsonArrayParseState.Seperator;
                 }
                 break;
             case JsonArrayParseState.Seperator:
                 if (this.Consume(','))
                 {
                     new JsonWhiteSpaceParser(JsonParseType.Array, this.Buffer).ParseNext();
                     this.state = JsonArrayParseState.Value;
                 }
                 else if (this.Consume(']'))
                 {
                     this.state = JsonArrayParseState.End;
                 }
                 else
                 {
                     return this.MakeError(',', ']');
                 }
                 break;
         }
     }
     return new JsonArray(items);
 }
Пример #5
0
        public static bool ParseProperty(ReadOnlySpan <byte> property, out JsonValueParser parser)
        {
            var hash = 0;

            if (property.Length >= 4)
            {
                hash = BitConverter.ToInt32(property.Slice(0, 4));
            }
            else if (property.Length >= 2)
            {
                hash = BitConverter.ToInt16(property.Slice(0, 2));
            }
            else
            {
                parser = null;
                return(false);
            }

            return(properties.TryGetValue(hash, out parser));
        }
Пример #6
0
        /// <inheritdoc />
        public JsonItem ParseNext()
        {
            JsonString propertyName = null;
            string     propertyNameAsString;
            IDictionary <string, JsonItem> propertyBag = new Dictionary <string, JsonItem>();

            this.state = JsonObjectParseState.Start;
            while (this.state != JsonObjectParseState.End)
            {
                JsonItem item = null;
                switch (this.state)
                {
                case JsonObjectParseState.Start:
                    if (this.Consume('{'))
                    {
                        this.state = JsonObjectParseState.PropertyName;
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                    }
                    else
                    {
                        return(this.MakeError('{'));
                    }
                    break;

                case JsonObjectParseState.PropertyName:
                    if (this.Peek('"'))
                    {
                        item = new JsonStringParser(this.Buffer).ParseNext();
                        if (item.IsString)
                        {
                            propertyName = (JsonString)item;
                        }
                        else
                        {
                            return(item);
                        }
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                        this.state = JsonObjectParseState.KeyValueSperator;
                    }
                    else if (this.Consume('}'))
                    {
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                        this.state = JsonObjectParseState.End;
                    }
                    else
                    {
                        return(this.MakeError('"'));
                    }
                    break;

                case JsonObjectParseState.KeyValueSperator:
                    if (this.Consume(':'))
                    {
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                        this.state = JsonObjectParseState.Value;
                    }
                    else
                    {
                        return(this.MakeError(':'));
                    }
                    break;

                case JsonObjectParseState.Value:
                    item = new JsonValueParser(this.Buffer).ParseNext();
                    if (item.IsError)
                    {
                        return(item);
                    }
                    propertyName.TryGetValue(out propertyNameAsString);
                    propertyBag.Add(propertyNameAsString, item);
                    new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                    this.state = JsonObjectParseState.PropertySetSeperator;
                    break;

                case JsonObjectParseState.PropertySetSeperator:
                    if (this.Consume(','))
                    {
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                        this.state = JsonObjectParseState.PropertyName;
                    }
                    else if (this.Consume('}'))
                    {
                        new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                        this.state = JsonObjectParseState.End;
                    }
                    else
                    {
                        return(this.MakeError(','));
                    }
                    break;
                }
            }
            return(new JsonObject(propertyBag));
        }
 /// <inheritdoc />
 public JsonItem ParseNext()
 {
     JsonString propertyName = null;
     string propertyNameAsString;
     IDictionary<string, JsonItem> propertyBag = new Dictionary<string, JsonItem>();
     this.state = JsonObjectParseState.Start;
     while (this.state != JsonObjectParseState.End)
     {
         JsonItem item = null;
         switch (this.state)
         {
             case JsonObjectParseState.Start:
                 if (this.Consume('{'))
                 {
                     this.state = JsonObjectParseState.PropertyName;
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                 }
                 else
                 {
                     return this.MakeError('{');
                 }
                 break;
             case JsonObjectParseState.PropertyName:
                 if (this.Peek('"'))
                 {
                     item = new JsonStringParser(this.Buffer).ParseNext();
                     if (item.IsString)
                     {
                         propertyName = (JsonString)item;
                     }
                     else
                     {
                         return item;
                     }
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                     this.state = JsonObjectParseState.KeyValueSperator;
                 }
                 else if (this.Consume('}'))
                 {
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                     this.state = JsonObjectParseState.End;
                 }
                 else
                 {
                     return this.MakeError('"');
                 }
                 break;
             case JsonObjectParseState.KeyValueSperator:
                 if (this.Consume(':'))
                 {
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                     this.state = JsonObjectParseState.Value;
                 }
                 else
                 {
                     return this.MakeError(':');
                 }
                 break;
             case JsonObjectParseState.Value:
                 item = new JsonValueParser(this.Buffer).ParseNext();
                 if (item.IsError)
                 {
                     return item;
                 }
                 propertyName.TryGetValue(out propertyNameAsString);
                 propertyBag.Add(propertyNameAsString, item);
                 new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                 this.state = JsonObjectParseState.PropertySetSeperator;
                 break;
             case JsonObjectParseState.PropertySetSeperator:
                 if (this.Consume(','))
                 {
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                     this.state = JsonObjectParseState.PropertyName;
                 }
                 else if (this.Consume('}'))
                 {
                     new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext();
                     this.state = JsonObjectParseState.End;
                 }
                 else
                 {
                     return this.MakeError(',');
                 }
                 break;
         }
     }
     return new JsonObject(propertyBag);
 }