示例#1
0
        private static JsonObject VisitObject(ref JsonTokenizer tokenizer)
        {
            var instance = new JsonObject();

            while (tokenizer.MoveNext())
            {
                var current = tokenizer.Current;
                if (current.TokenType == JsonTokenType.ObjectEnd)
                {
                    break;
                }

                var property = current.Value;

                tokenizer.MoveNext();

                instance.Add(property, Visit(ref tokenizer));
            }

            return(instance);
        }
示例#2
0
        private TOut Deserialize <TOut>(JsonReader reader)
        {
            _buffer ??= new StringBuilder(200);

            var converter = Converters.Get <TOut>();

            using var tokenizer = new JsonTokenizer(reader, _buffer);
            if (converter.IsPrimitive)
            {
                tokenizer.MoveNext();
            }

            return(converter.Deserialize(tokenizer));
        }
示例#3
0
        public override TElement[] Deserialize(JsonTokenizer tokenizer)
        {
            // it's maybe different tokens
            if (tokenizer.Current.TokenType == JsonTokenType.None)
            {
                tokenizer.MoveNext();
            }
            if (tokenizer.Current.TokenType == JsonTokenType.Null)
            {
                return(null !);
            }

            var buffer = GetBuffer();

            while (tokenizer.MoveNext())
            {
                var tokenType = tokenizer.Current.TokenType;

                if (tokenType == JsonTokenType.ArrayStart)
                {
                    continue;
                }
                if (tokenType == JsonTokenType.ArrayEnd)
                {
                    break;
                }

                var element = DeserializeElement(tokenizer);
                buffer.Add(element);
            }

            var result = buffer.ToArray();

            ReturnBuffer(buffer);

            return(result);
        }
示例#4
0
        private static JsonData Parse(JsonReader reader)
        {
            if (_buffer == null)
            {
                _buffer = new StringBuilder(200);
            }

            var tokenizer = new JsonTokenizer(reader, _buffer);

            tokenizer.MoveNext();

            var result = Visit(ref tokenizer);

            tokenizer.Dispose();

            return(result);
        }
示例#5
0
        private static JsonArray VisitArray(ref JsonTokenizer tokenizer)
        {
            var elements = new LocalList <JsonData>();

            while (tokenizer.MoveNext())
            {
                var current = tokenizer.Current;
                if (current.TokenType == JsonTokenType.ArrayEnd)
                {
                    break;
                }

                var element = Visit(ref tokenizer);
                elements.Add(element);
            }

            return(elements.Length == 0
                ? JsonArray.Empty
                : new JsonArray(elements.ToArray()));
        }
示例#6
0
        private TOut Deserialize <TOut>(JsonReader reader)
        {
            if (_buffer == null)
            {
                _buffer = new StringBuilder(200);
            }

            var converter = _converters.Get <TOut>();

            var tokenizer = new JsonTokenizer(reader, _buffer);

            if (converter.IsPrimitive)
            {
                tokenizer.MoveNext();
            }

            var result = converter.Deserialize(ref tokenizer);

            tokenizer.Dispose();

            return(result);
        }
示例#7
0
        public static JsonObject VisitObject(JsonTokenizer tokenizer)
        {
            var instance = new JsonObject();

            while (tokenizer.MoveNext())
            {
                var current = tokenizer.Current;
                if (current.TokenType == JsonTokenType.ObjectStart)
                {
                    continue;
                }
                if (current.TokenType == JsonTokenType.ObjectEnd)
                {
                    break;
                }

                var propertyValue = VisitProperty(tokenizer, out var propertyName);
                instance.Add(propertyName, propertyValue);
            }

            return(instance);
        }
示例#8
0
        public List <TElement> Deserialize(ref JsonTokenizer tokenizer)
        {
            var buffer = new LocalList <TElement>();

            while (tokenizer.MoveNext())
            {
                var token     = tokenizer.Current;
                var tokenType = token.TokenType;

                // ReSharper disable once ConvertIfStatementToSwitchStatement
                if (tokenType == JsonTokenType.Null)
                {
                    return(null);
                }
                if (tokenType == JsonTokenType.ArrayStart)
                {
                    continue;
                }
                if (tokenType == JsonTokenType.ArrayEnd)
                {
                    break;
                }

                var element = _elementConverter.Deserialize(ref tokenizer);
                buffer.Add(element);
            }

            var list = new List <TElement>(buffer.Length);

            foreach (var element in buffer)
            {
                list.Add(element);
            }

            return(list);
        }
示例#9
0
 public static JsonData Parse(JsonTokenizer tokenizer)
 {
     tokenizer.MoveNext();
     return(JsonVisitor.Visit(tokenizer));
 }
示例#10
0
 public static JsonData VisitProperty(JsonTokenizer tokenizer)
 {
     tokenizer.MoveNext();
     return(Visit(tokenizer));
 }