Beispiel #1
0
        public static byte[] SequenceReturnBytesHelper(byte[] data, out int length, JsonCommentHandling commentHandling = JsonCommentHandling.Disallow, int maxDepth = 64)
        {
            ReadOnlySequence <byte> sequence = CreateSegments(data);
            var state = new JsonReaderState(new JsonReaderOptions {
                CommentHandling = commentHandling, MaxDepth = maxDepth
            });
            var reader = new Utf8JsonReader(sequence, true, state);

            return(ReaderLoop(data.Length, out length, ref reader));
        }
        private static void TestReadingJsonWithComments(byte[] inputData, ReadOnlySequence <byte> sequence, string expectedWithoutComments, string expectedWithComments)
        {
            var state = new JsonReaderState(options: new JsonReaderOptions {
                CommentHandling = JsonCommentHandling.Allow
            });
            var json = new Utf8JsonReader(sequence, isFinalBlock: true, state);

            var builder = new StringBuilder();

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.Number || json.TokenType == JsonTokenType.Comment || json.TokenType == JsonTokenType.PropertyName)
                {
                    builder.Append(Encoding.UTF8.GetString(json.HasValueSequence ? json.ValueSequence.ToArray() : json.ValueSpan.ToArray()));
                    if (json.HasValueSequence)
                    {
                        Assert.True(json.ValueSpan == default);
                    }
                    else
                    {
                        Assert.True(json.ValueSequence.IsEmpty);
                    }
                }
            }

            Assert.Equal(expectedWithComments, builder.ToString());
            Assert.Equal(inputData, sequence.Slice(0, json.Position).ToArray());
            Assert.True(json.Position.Equals(json.CurrentState.Position));

            state = new JsonReaderState(options: new JsonReaderOptions {
                CommentHandling = JsonCommentHandling.Skip
            });
            json = new Utf8JsonReader(sequence, isFinalBlock: true, state);

            builder = new StringBuilder();
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.Number || json.TokenType == JsonTokenType.Comment || json.TokenType == JsonTokenType.PropertyName)
                {
                    builder.Append(Encoding.UTF8.GetString(json.HasValueSequence ? json.ValueSequence.ToArray() : json.ValueSpan.ToArray()));
                    if (json.HasValueSequence)
                    {
                        Assert.True(json.ValueSpan == default);
                    }
                    else
                    {
                        Assert.True(json.ValueSequence.IsEmpty);
                    }
                }
            }

            Assert.Equal(expectedWithoutComments, builder.ToString());
            Assert.Equal(inputData, sequence.Slice(0, json.Position).ToArray());
            Assert.True(json.Position.Equals(json.CurrentState.Position));
        }
        public static async Task <(bool success, IEnumerable <string> failedIds)> FromStreamAsync(Stream stream,
                                                                                                  CancellationToken ct = default)
        {
            var buffer = ArrayPool <byte> .Shared.Rent(1024); // hardcoded as I know the length in this sample

            JsonReaderState state    = default;
            int             leftOver = 0;

            bool foundErrorsProperty = false;
            bool hasErrors           = true;

            List <string> errors           = null;
            var           insideMainObject = false;
            var           insideItemsArray = false;

            try
            {
                while (true)
                {
                    // could use pipelines here too
                    int dataLength = await stream.ReadAsync(buffer.AsMemory(leftOver,
                                                                            buffer.Length - leftOver), ct);

                    int  dataSize     = dataLength + leftOver;
                    bool isFinalBlock = dataSize == 0;

                    var consumed = ParseErrors(buffer.AsSpan(0, dataSize), isFinalBlock,
                                               ref state, ref foundErrorsProperty, ref hasErrors,
                                               ref insideMainObject, ref insideItemsArray, ref errors);

                    if (foundErrorsProperty && !hasErrors)
                    {
                        break; // there are no errors so we can short-circuit here.
                    }

                    leftOver = dataSize - (int)consumed;

                    if (leftOver != 0)
                    {
                        buffer.AsSpan(dataSize - leftOver).CopyTo(buffer); // can we avoid this copy?
                    }

                    if (isFinalBlock)
                    {
                        break;
                    }
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }

            return(!hasErrors, errors);
        }
Beispiel #4
0
        public JsonReader(TextReader stream)
        {
            this.state = JsonReaderState.Start;
            this.stack = new Stack<JsonReaderState>();

            this.buffer = new JsonReaderBuffer
            {
                Size = 1024,
                Stream = stream,
                Data = new char[1024]
            };
        }
Beispiel #5
0
        public bool Next()
        {
            if (state == JsonReaderState.Final)
                return false;

            if (state == JsonReaderState.SyntaxError)
                return false;

            if (state == JsonReaderState.StreamError)
                return false;

            try
            {
                EnsureOrThrow(false);
                SkipWhiteSpacesOrThrow();

                switch (state)
                {
                    case JsonReaderState.Start:
                        return ReadArrayOrObject();

                    case JsonReaderState.BeginObject:
                        return ReadPropertyOrEndObject();

                    case JsonReaderState.BeginArray:
                        return ReadValueOrEndArray();

                    case JsonReaderState.Property:
                        return ReadPropertySeparatorAndValue();

                    case JsonReaderState.ValueInObject:
                        return ReadItemSeparatorAndPropertyOrEndObject();

                    case JsonReaderState.ValueInArray:
                        return ReadItemSeparatorAndValueOrEndArray();
                }
            }
            catch (JsonReaderStreamException)
            {
                state = JsonReaderState.StreamError;
            }
            catch (JsonReaderSyntaxException)
            {
                state = JsonReaderState.SyntaxError;
            }
            finally
            {
                if (state == JsonReaderState.Final)
                    SkipWhiteSpaces();
            }

            return false;
        }
Beispiel #6
0
        private static object ParseCore(ReadOnlySpan <byte> utf8Json, Type returnType, JsonSerializerOptions options)
        {
            if (options == null)
            {
                options = s_defaultSettings;
            }

            var state  = new JsonReaderState(options: options.ReaderOptions);
            var reader = new Utf8JsonReader(utf8Json, isFinalBlock: true, state);

            return(ReadCore(reader, returnType, options));
        }
        private static void ReadCore <T>(ref JsonReaderState readerState, bool isFinalBlock, ReadOnlySpan <byte> buffer, ref DdbEntityReadStack readStack) where T : class
        {
            var ddbReader = new DdbReader(buffer, isFinalBlock, ref readerState, ref readStack)
            {
                State = { ReadAhead = !isFinalBlock, BytesConsumed = 0 }
            };

            ReadCore <T>(ref ddbReader);

            readerState = ddbReader.JsonReaderValue.CurrentState;
            readStack   = ddbReader.State;
        }
        //[Fact] //TODO: Fix and re-enable
        public void ReadFromPipeUsingSpan()
        {
            string actual     = "";
            var    taskReader = Task.Run(async() =>
            {
                SequencePosition position = default;
                JsonReaderState state     = default;
                string str = "";
                while (true)
                {
                    ReadResult result            = await _pipe.Reader.ReadAsync();
                    ReadOnlySequence <byte> data = result.Buffer;
                    bool isFinalBlock            = result.IsCompleted;
                    (state, position, str)       = ProcessDataSpan(data, isFinalBlock, state);
                    _pipe.Reader.AdvanceTo(position);
                    actual += str;
                    if (isFinalBlock)
                    {
                        break;
                    }
                }
            });

            var taskWriter = Task.Run(async() =>
            {
                while (true)
                {
                    Memory <byte> mem  = default;
                    bool isLastSegment = false;
                    if (_written >= _dataUtf8.Length - 4_000)
                    {
                        isLastSegment = true;
                        mem           = _dataUtf8.AsMemory(_written);
                    }
                    else
                    {
                        mem = _dataUtf8.AsMemory(_written, 4_000);
                    }
                    FlushResult result = await _pipe.Writer.WriteAsync(mem);
                    _written          += 4000;
                    if (isLastSegment)
                    {
                        break;
                    }
                }
                _pipe.Writer.Complete();
            });

            Task[] tasks = new Task[] { taskReader, taskWriter };
            Task.WaitAll(tasks, 30_000);    // The test shouldn't take more than 30 seconds to finish.

            Assert.Equal(_expectedString, actual);
        }
        public static void JsonReaderStateDefaultCtor()
        {
            var state = new JsonReaderState();

            var expectedOption = new JsonReaderOptions
            {
                CommentHandling = JsonCommentHandling.Disallow,
                MaxDepth        = 0
            };

            Assert.Equal(expectedOption, state.Options);
        }
        public static void JsonWithTrailingCommasMultiSegment_Valid(string jsonString)
        {
            byte[] utf8 = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8, 1);

            {
                JsonReaderState state = default;
                TrailingCommasHelper(sequence, state, allow: false, expectThrow: true);
            }

            {
                var state = new JsonReaderState(options: default);
        public void Utf8JsonReaderCommentParsing()
        {
            var state = new JsonReaderState(options: new JsonReaderOptions {
                CommentHandling = CommentHandling
            });
            Utf8JsonReader reader = MultiSegment ?
                                    new Utf8JsonReader(_jsonPayloadSequence, isFinalBlock: true, state) :
                                    new Utf8JsonReader(_jsonPayload, isFinalBlock: true, state);

            while (reader.Read())
            {
            }
        }
        public static void ReaderOptionsWinMaxDepth()
        {
            byte[] utf8 = Encoding.UTF8.GetBytes("[[]]");

            var readerOptions = new JsonReaderOptions
            {
                MaxDepth = 1,
            };

            var serializerOptions = new JsonSerializerOptions
            {
                MaxDepth = 5,
            };

            Assert.Throws <JsonException>(() =>
            {
                var reader = new Utf8JsonReader(utf8, readerOptions);
                JsonSerializer.Deserialize(ref reader, typeof(int[][]), serializerOptions);
            });

            var state = new JsonReaderState(readerOptions);

            Assert.Throws <JsonException>(() =>
            {
                var reader = new Utf8JsonReader(utf8, isFinalBlock: false, state);
                JsonSerializer.Deserialize(ref reader, typeof(int[][]), serializerOptions);
            });


            readerOptions = new JsonReaderOptions
            {
                MaxDepth = 5,
            };

            serializerOptions = new JsonSerializerOptions
            {
                MaxDepth = 1,
            };

            {
                var     reader = new Utf8JsonReader(utf8, readerOptions);
                int[][] result = JsonSerializer.Deserialize <int[][]>(ref reader);
                Assert.Equal(1, result.Length);
            }

            {
                var     reader = new Utf8JsonReader(utf8, readerOptions);
                int[][] result = JsonSerializer.Deserialize <int[][]>(ref reader, serializerOptions);
                Assert.Equal(1, result.Length);
            }
        }
Beispiel #13
0
        public static void DefaultJsonReaderState()
        {
            JsonReaderState state = default;

            Assert.Equal(0, state.BytesConsumed);
            Assert.Equal(0, state.MaxDepth);

            var expectedOption = new JsonReaderOptions
            {
                CommentHandling = JsonCommentHandling.Disallow
            };

            Assert.Equal(expectedOption, state.Options);
        }
Beispiel #14
0
        private static object ReadInternal(string json, Type returnType, JsonSerializerOptions options = null)
        {
            if (options == null)
            {
                options = s_defaultSettings;
            }

            // todo: use an array pool here for smaller requests to avoid the alloc. Also doc the API that UTF8 is preferred for perf.
            byte[] jsonBytes = s_utf8Encoding.GetBytes(json);
            var    state     = new JsonReaderState(options: options.ReaderOptions);
            var    reader    = new Utf8JsonReader(jsonBytes, isFinalBlock: true, state);

            return(Read(reader, returnType, options));
        }
Beispiel #15
0
        public static bool TryReadPlaylistItem(
            ref ReadOnlySequence <byte> sequence,
            ref JsonReaderState state,
            [NotNullWhen(true)] out string?videoId)
        {
            videoId = default;
            var reader = new Utf8JsonReader(sequence, true, state);

            int savedDepth = 0;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    if (reader.ValueTextEquals(PlaylistVideoRendererProperty))
                    {
                        savedDepth = reader.CurrentDepth;
                    }
                    else if (reader.ValueTextEquals(VideoIdPropertyName))
                    {
                        if (!TryGetString(ref reader, out videoId))
                        {
                            return(false);
                        }

                        while (reader.CurrentDepth > savedDepth)
                        {
                            if (!reader.Read())
                            {
                                return(false);
                            }
                        }

                        sequence = sequence.Slice(reader.Position);
                        state    = reader.CurrentState;
                        return(true);
                    }
                    else if (reader.ValueTextEquals(SidebarPropertyName))
                    {
                        if (!reader.TrySkip())
                        {
                            return(false);
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #16
0
        public static void PrintJson(string str)
        {
            ReadOnlySpan <byte> dataUtf8 = Encoding.ASCII.GetBytes(str).AsSpan();
            var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default);

            while (json.Read())
            {
                JsonTokenType       tokenType = json.TokenType;
                ReadOnlySpan <byte> valueSpan = json.ValueSpan;
                switch (tokenType)
                {
                case JsonTokenType.StartObject:
                case JsonTokenType.EndObject:
                    break;

                case JsonTokenType.StartArray:
                case JsonTokenType.EndArray:
                    break;

                case JsonTokenType.PropertyName:
                    break;

                case JsonTokenType.String:
                    Console.WriteLine($"STRING: {json.GetString()}");
                    break;

                case JsonTokenType.Number:
                    if (!json.TryGetInt32(out int valueInteger))
                    {
                        throw new FormatException();
                    }
                    break;

                case JsonTokenType.True:
                case JsonTokenType.False:
                    Console.WriteLine($"BOOL: {json.GetBoolean()}");
                    break;

                case JsonTokenType.Null:
                    break;

                default:
                    throw new ArgumentException();
                }
            }

            dataUtf8 = dataUtf8.Slice((int)json.BytesConsumed);
            JsonReaderState state = json.CurrentState;
        }
Beispiel #17
0
        public static void Utf8JsonReaderLoop(ReadOnlySpan <byte> dataUtf8)
        {
            var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default);

            while (json.Read())
            {
                JsonTokenType       tokenType = json.TokenType;
                ReadOnlySpan <byte> valueSpan = json.ValueSpan;
                switch (tokenType)
                {
                case JsonTokenType.StartObject:
                case JsonTokenType.EndObject:
                    break;

                case JsonTokenType.StartArray:
                case JsonTokenType.EndArray:
                    break;

                case JsonTokenType.PropertyName:
                    break;

                case JsonTokenType.String:
                    string valueString = json.GetString();
                    break;

                case JsonTokenType.Number:
                    if (!json.TryGetInt32(out int valueInteger))
                    {
                        throw new FormatException();
                    }
                    break;

                case JsonTokenType.True:
                case JsonTokenType.False:
                    bool valueBool = json.GetBoolean();
                    break;

                case JsonTokenType.Null:
                    break;

                default:
                    throw new ArgumentException();
                }
            }

            dataUtf8 = dataUtf8.Slice((int)json.BytesConsumed);
            JsonReaderState state = json.CurrentState;
        }
Beispiel #18
0
        private static void ReadCore(
            ref JsonReaderState readerState,
            bool isFinalBlock,
            Span <byte> buffer,
            JsonSerializerOptions options,
            ref ReadStack state)
        {
            var reader = new Utf8JsonReader(buffer, isFinalBlock, readerState);

            ReadCore(
                options,
                ref reader,
                ref state);

            readerState = reader.CurrentState;
        }
Beispiel #19
0
        public static void EnableComments()
        {
            string json = "3";

            var options = new JsonReaderOptions
            {
                CommentHandling = JsonCommentHandling.Allow,
            };

            byte[] utf8 = Encoding.UTF8.GetBytes(json);

            AssertExtensions.Throws <ArgumentException>(
                "reader",
                () =>
            {
                var state  = new JsonReaderState(options);
                var reader = new Utf8JsonReader(utf8, isFinalBlock: false, state);
                JsonSerializer.Deserialize(ref reader, typeof(int));
            });

            AssertExtensions.Throws <ArgumentException>(
                "reader",
                () =>
            {
                var state  = new JsonReaderState(options);
                var reader = new Utf8JsonReader(utf8, isFinalBlock: true, state);
                JsonSerializer.Deserialize(ref reader, typeof(int));
            });

            AssertExtensions.Throws <ArgumentException>(
                "reader",
                () =>
            {
                var state  = new JsonReaderState(options);
                var reader = new Utf8JsonReader(utf8, isFinalBlock: false, state);
                JsonSerializer.Deserialize <int>(ref reader);
            });

            AssertExtensions.Throws <ArgumentException>(
                "reader",
                () =>
            {
                var state  = new JsonReaderState(options);
                var reader = new Utf8JsonReader(utf8, isFinalBlock: true, state);
                JsonSerializer.Deserialize <int>(ref reader);
            });
        }
        private static void ReadCore(
            ref JsonReaderState readerState,
            bool isFinalBlock,
            byte[] buffer,
            int bytesToRead,
            JsonSerializerOptions options,
            ref ReadStack state)
        {
            Utf8JsonReader reader = new Utf8JsonReader(buffer.AsSpan(0, bytesToRead), isFinalBlock, readerState);

            ReadCore(
                options,
                ref reader,
                ref state);

            readerState = reader.CurrentState;
        }
        private static object ParseCore(ReadOnlySpan <byte> utf8Json, Type returnType, JsonSerializerOptions options)
        {
            options ??= JsonSerializerOptions.s_defaultOptions;

            var    readerState = new JsonReaderState(options.GetReaderOptions());
            var    reader      = new Utf8JsonReader(utf8Json, isFinalBlock: true, readerState);
            object result      = ReadCore(returnType, options, ref reader);

            readerState = reader.CurrentState;
            if (readerState.BytesConsumed != utf8Json.Length)
            {
                throw new JsonReaderException(SR.Format(SR.DeserializeDataRemaining,
                                                        utf8Json.Length, utf8Json.Length - readerState.BytesConsumed), readerState);
            }

            return(result);
        }
 public override bool Read()
 {
     if (ReaderState.Next != null)
     {
         ReaderState = ReaderState.Next;
         this.SetToken(ReaderState.Token, ReaderState.Value);
         return(true);
     }
     else
     {
         var result = InnerReader.Read();
         this.SetToken(InnerReader.TokenType, InnerReader.Value);
         ReaderState.Next = new JsonReaderState(this.TokenType, this.Value);
         ReaderState      = ReaderState.Next;
         return(result);
     }
 }
Beispiel #23
0
        private static object ParseCore(ReadOnlySpan <byte> utf8Json, Type returnType, JsonSerializerOptions options)
        {
            if (options == null)
            {
                options = JsonSerializerOptions.s_defaultOptions;
            }

            var    readerState = new JsonReaderState(options.GetReaderOptions());
            var    reader      = new Utf8JsonReader(utf8Json, isFinalBlock: true, readerState);
            object result      = ReadCore(returnType, options, ref reader);

            if (reader.BytesConsumed != utf8Json.Length)
            {
                ThrowHelper.ThrowJsonException_DeserializeDataRemaining(utf8Json.Length, utf8Json.Length - reader.BytesConsumed);
            }

            return(result);
        }
        private static object ParseCore(string json, Type returnType, JsonSerializerOptions options = null)
        {
            options ??= JsonSerializerOptions.s_defaultOptions;

            // todo: use an array pool here for smaller requests to avoid the alloc?
            byte[] jsonBytes   = JsonReaderHelper.s_utf8Encoding.GetBytes(json);
            var    readerState = new JsonReaderState(options.GetReaderOptions());
            var    reader      = new Utf8JsonReader(jsonBytes, isFinalBlock: true, readerState);
            object result      = ReadCore(returnType, options, ref reader);

            readerState = reader.CurrentState;
            if (readerState.BytesConsumed != jsonBytes.Length)
            {
                throw new JsonReaderException(SR.Format(SR.DeserializeDataRemaining,
                                                        jsonBytes.Length, jsonBytes.Length - readerState.BytesConsumed), readerState);
            }

            return(result);
        }
Beispiel #25
0
        private static async Task <T> ReadAsync <T>(PipeReader utf8Reader, Type returnType, JsonSerializerOptions options = null, CancellationToken cancellationToken = default)
        {
            if (options == null)
            {
                options = s_defaultSettings;
            }

            ReadObjectState current   = default;
            JsonClassInfo   classInfo = options.GetOrAddClass(returnType);

            current.ClassInfo = classInfo;
            if (classInfo.ClassType != ClassType.Object)
            {
                current.PropertyInfo = classInfo.GetPolicyProperty();
            }

            var readerState = new JsonReaderState(options: options.ReaderOptions);
            List <ReadObjectState> previous = null;
            int arrayIndex = 0;

            ReadResult result;

            do
            {
                result = await utf8Reader.ReadAsync(cancellationToken).ConfigureAwait(false);

                ReadOnlySequence <byte> buffer = result.Buffer;

                Read(
                    ref readerState,
                    returnType,
                    result.IsCompleted,
                    buffer,
                    options,
                    ref current,
                    ref previous,
                    ref arrayIndex);

                utf8Reader.AdvanceTo(buffer.GetPosition(readerState.BytesConsumed), buffer.End);
            } while (!result.IsCompleted);

            return((T)current.ReturnValue);
        }
Beispiel #26
0
        internal static bool DoSingleValueReadWithReadAhead(ref Utf8JsonReader reader, ref ReadStack state)
        {
            // When we're reading ahead we always have to save the state as we don't know if the next token
            // is an opening object or an array brace.
            JsonReaderState initialReaderState         = reader.CurrentState;
            long            initialReaderBytesConsumed = reader.BytesConsumed;

            if (!reader.Read())
            {
                return(false);
            }

            // Perform the actual read-ahead.
            JsonTokenType tokenType = reader.TokenType;

            if (tokenType == JsonTokenType.StartObject || tokenType == JsonTokenType.StartArray)
            {
                // Attempt to skip to make sure we have all the data we need.
                bool complete = reader.TrySkip();

                // We need to restore the state in all cases as we need to be positioned back before
                // the current token to either attempt to skip again or to actually read the value.

                reader = new Utf8JsonReader(reader.OriginalSpan.Slice(checked ((int)initialReaderBytesConsumed)),
                                            isFinalBlock: reader.IsFinalBlock,
                                            state: initialReaderState);

                Debug.Assert(reader.BytesConsumed == 0);
                state.BytesConsumed += initialReaderBytesConsumed;

                if (!complete)
                {
                    // Couldn't read to the end of the object, exit out to get more data in the buffer.
                    return(false);
                }

                // Success, requeue the reader to the start token.
                reader.ReadWithVerify();
                Debug.Assert(tokenType == reader.TokenType);
            }

            return(true);
        }
Beispiel #27
0
        private static void TestReadingSingleValueJson(ReadOnlySequence <byte> sequence, string expectedString)
        {
            foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
            {
                var state = new JsonReaderState(options: new JsonReaderOptions {
                    CommentHandling = commentHandling
                });
                var json = new Utf8JsonReader(sequence, false, state);

                while (json.Read())
                {
                    // Check if the TokenType is a primitive "value", i.e. String, Number, True, False, and Null
                    Assert.True(json.TokenType >= JsonTokenType.String && json.TokenType <= JsonTokenType.Null);
                    Assert.Equal(expectedString, Encoding.UTF8.GetString(json.HasValueSequence ? json.ValueSequence.ToArray() : json.ValueSpan.ToArray()));
                }

                Assert.Equal(json.BytesConsumed, json.CurrentState.BytesConsumed);
            }
        }
        public static void TestingGetComment(string jsonData, string expected)
        {
            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonData);
            var    state    = new JsonReaderState(options: new JsonReaderOptions {
                CommentHandling = JsonCommentHandling.Allow
            });
            var reader = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state);

            Assert.True(reader.Read());
            Assert.Equal(JsonTokenType.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonTokenType.Comment, reader.TokenType);
            Assert.Equal(expected, reader.GetComment());

            Assert.True(reader.Read());
            Assert.Equal(JsonTokenType.EndObject, reader.TokenType);

            Assert.False(reader.Read());
        }
        public static void SkipSingleLineCommentMultiSpanTest(string expected)
        {
            string jsonData = "{" + expected + "}";

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonData);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1);

            for (int i = 0; i < jsonData.Length; i++)
            {
                var state = new JsonReaderState(options: new JsonReaderOptions {
                    CommentHandling = JsonCommentHandling.Skip
                });

                var json = new Utf8JsonReader(sequence.Slice(0, i), isFinalBlock: false, state);
                VerifyReadLoop(ref json, null);

                json = new Utf8JsonReader(sequence.Slice(state.BytesConsumed), isFinalBlock: true, state);
                VerifyReadLoop(ref json, null);
            }
        }
        private static void ReadCore(
            ref JsonReaderState readerState,
            Type returnType,
            bool isFinalBlock,
            ReadOnlySequence <byte> buffer,
            JsonSerializerOptions options,
            ref ReadObjectState current,
            ref List <ReadObjectState> previous,
            ref int arrayIndex)
        {
            Utf8JsonReader reader = new Utf8JsonReader(buffer, isFinalBlock, readerState);

            ReadCore(
                options,
                ref reader,
                ref current,
                ref previous,
                ref arrayIndex);

            readerState = reader.CurrentState;
        }
Beispiel #31
0
        private static void ReadCore(
            ref JsonReaderState readerState,
            bool isFinalBlock,
            byte[] buffer,
            int bytesToRead,
            JsonSerializerOptions options,
            ref ReadObjectState current,
            ref List <ReadObjectState> previous,
            ref int arrayIndex)
        {
            Utf8JsonReader reader = new Utf8JsonReader(new ReadOnlySpan <byte>(buffer, 0, bytesToRead), isFinalBlock, readerState);

            ReadCore(
                options,
                ref reader,
                ref current,
                ref previous,
                ref arrayIndex);

            readerState = reader.CurrentState;
        }
Beispiel #32
0
        public static void TestingGetStringInvalidUTF16(string jsonString)
        {
            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
            {
                var state = new JsonReaderState(options: new JsonReaderOptions {
                    CommentHandling = commentHandling
                });
                var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state);

                Assert.True(json.Read());
                Assert.Equal(JsonTokenType.String, json.TokenType);
                try
                {
                    string val = json.GetString();
                    Assert.True(false, "Expected InvalidOperationException when trying to get string value for invalid UTF-16 JSON text.");
                }
                catch (InvalidOperationException) { }
            }
        }
Beispiel #33
0
        private bool ReadValueOrEndArray()
        {
            switch (buffer.Data[buffer.Offset])
            {
                case ']':
                    state = stack.Pop();
                    token = new TypedToken(JsonTokenType.EndArray);
                    buffer.Forward(false);
                    return true;
            }

            return ReadValue(JsonReaderState.ValueInArray);
        }
Beispiel #34
0
        private bool ReadValue(JsonReaderState nextState)
        {
            switch (buffer.Data[buffer.Offset])
            {
                case '{':
                    return ReadStartObject(nextState);

                case '[':
                    return ReadStartArray(nextState);

                case '"':
                    return ReadTextOrProperty(JsonTokenType.Text, nextState);

                case 't':
                    return ReadTrue(nextState);

                case 'f':
                    return ReadFalse(nextState);

                case 'n':
                    return ReadNull(nextState);
            }

            return ReadNumber(nextState);
        }
Beispiel #35
0
        private bool ReadTextOrProperty(JsonTokenType textOrProperty, JsonReaderState nextState)
        {
            token = TextParser.Parse(buffer, textOrProperty);
            state = nextState;

            return true;
        }
Beispiel #36
0
        private bool ReadNumber(JsonReaderState nextState)
        {
            token = NumberParser.Parse(buffer);
            state = nextState;

            return true;
        }
Beispiel #37
0
 private bool ReadStartArray(JsonReaderState nextState)
 {
     stack.Push(nextState);
     state = JsonReaderState.BeginArray;
     token = new TypedToken(JsonTokenType.OpenArray);
     buffer.Forward(false);
     return true;
 }
Beispiel #38
0
 private bool ReadEndArray()
 {
     state = stack.Pop();
     token = new TypedToken(JsonTokenType.EndArray);
     buffer.Forward(false);
     return true;
 }
Beispiel #39
0
        private bool ReadFalse(JsonReaderState nextState)
        {
            if (Skip("false"))
            {
                state = nextState;
                token = new TypedToken(JsonTokenType.False);
                return true;
            }

            throw new JsonReaderSyntaxException();
        }
Beispiel #40
0
        private bool ReadNull(JsonReaderState nextState)
        {
            if (Skip("null"))
            {
                state = nextState;
                token = new TypedToken(JsonTokenType.Null);
                return true;
            }

            throw new JsonReaderSyntaxException();
        }