Ejemplo n.º 1
0
        public ArraySegment <byte> ReadNumberSegment()
        {
            SkipWhiteSpace();
            var initialOffset = _offset;

            for (var i = _offset; i < _bytes.Length; i++)
            {
                if (!NumberConverter.IsNumberRepresentation(_bytes[i]))
                {
                    if (NumberConverter.IsENotation(_bytes[i]) && (i + 1) < _bytes.Length && NumberConverter.IsNumberRepresentation(_bytes[i + 1]))
                    {
                        i++;
                        continue;
                    }

                    _offset = i;
                    goto END;
                }
            }
            _offset = _bytes.Length;

END:
            return(new ArraySegment <byte>(_bytes, initialOffset, _offset - initialOffset));
        }
        public object Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var token = reader.GetCurrentJsonToken();

            switch (token)
            {
            case JsonToken.BeginObject:
            {
                var dict = new Dictionary <string, object>();
                reader.ReadIsBeginObjectWithVerify();
                var count = 0;
                while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref count))
                {
                    var key   = reader.ReadPropertyName();
                    var value = Deserialize(ref reader, formatterResolver);
                    dict.Add(key, value);
                }
                return(dict);
            }

            case JsonToken.BeginArray:
            {
                var list = new List <object>(4);
                reader.ReadIsBeginArrayWithVerify();
                var count = 0;
                while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
                {
                    list.Add(Deserialize(ref reader, formatterResolver));
                }
                return(list);
            }

            case JsonToken.Number:
                var numberSegment = reader.ReadNumberSegment();
                // conditional operator here would cast both to double, so don't use.
                // Check for IsLong first, avoid floating point rounding
                if (numberSegment.IsLong())
                {
                    return(NumberConverter.ReadInt64(numberSegment.Array, numberSegment.Offset, out _));
                }

                return(NumberConverter.ReadDouble(numberSegment.Array, numberSegment.Offset, out _));

            case JsonToken.String:
                return(reader.ReadString());

            case JsonToken.True:
                return(reader.ReadBoolean());                        // require advance

            case JsonToken.False:
                return(reader.ReadBoolean());                        // require advance

            case JsonToken.ValueSeparator:
            case JsonToken.NameSeparator:
            case JsonToken.EndArray:
            case JsonToken.EndObject:
                throw new InvalidOperationException("Invalid Json Token:" + token);

            case JsonToken.Null:
                reader.ReadIsNull();
                return(null);

            case JsonToken.None:
            default:
                return(null);
            }
        }