コード例 #1
0
        public void WritePropertyOutsideObject(bool skipValidation)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_options))
            {
                JsonElement root    = doc.RootElement;
                var         options = new JsonWriterOptions
                {
                    SkipValidation = skipValidation,
                };

                const string CharLabel = "char";
                byte[]       byteUtf8  = Encoding.UTF8.GetBytes("byte");
                using var writer = new Utf8JsonWriter(buffer, options);

                if (skipValidation)
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        writer.WritePropertyName(CharLabel);
                        val.WriteTo(writer);
                        writer.WritePropertyName(CharLabel.AsSpan());
                        val.WriteTo(writer);
                        writer.WritePropertyName(byteUtf8);
                        val.WriteTo(writer);
                        writer.WritePropertyName(JsonEncodedText.Encode(CharLabel));
                        val.WriteTo(writer);
                    }

                    writer.Flush();

                    JsonTestHelper.AssertContents(
                        "\"char\":null,\"char\":null,\"byte\":null,\"char\":null," +
                        "\"char\":false,\"char\":false,\"byte\":false,\"char\":false," +
                        "\"char\":true,\"char\":true,\"byte\":true,\"char\":true," +
                        "\"char\":\"hi\",\"char\":\"hi\",\"byte\":\"hi\",\"char\":\"hi\"," +
                        "\"char\":5,\"char\":5,\"byte\":5,\"char\":5," +
                        "\"char\":{},\"char\":{},\"byte\":{},\"char\":{}," +
                        "\"char\":[],\"char\":[],\"byte\":[],\"char\":[]",
                        buffer);
                }
                else
                {
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(CharLabel));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(CharLabel.AsSpan()));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(byteUtf8));
                    Assert.Throws <InvalidOperationException>(() => writer.WritePropertyName(JsonEncodedText.Encode(CharLabel)));

                    writer.Flush();

                    JsonTestHelper.AssertContents("", buffer);
                }
            }
        }
コード例 #2
0
        public static void ReadJsonTokenWithExtraValueMultiSegment(string jsonString)
        {
            byte[] utf8 = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8, 1);

            foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
            {
                TestReadTokenWithExtra(sequence, commentHandling, isFinalBlock: false);
                TestReadTokenWithExtra(sequence, commentHandling, isFinalBlock: true);
            }
        }
コード例 #3
0
        public static void TestJsonReaderUtf8SegmentSizeOne(bool compactData, TestCaseType type, string jsonString)
        {
            // Remove all formatting/indendation
            if (compactData)
            {
                using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(jsonString)))
                {
                    jsonReader.FloatParseHandling = FloatParseHandling.Decimal;
                    JToken jtoken       = JToken.ReadFrom(jsonReader);
                    var    stringWriter = new StringWriter();
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter))
                    {
                        jtoken.WriteTo(jsonWriter);
                        jsonString = stringWriter.ToString();
                    }
                }
            }

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            Stream     stream      = new MemoryStream(dataUtf8);
            TextReader reader      = new StreamReader(stream, Encoding.UTF8, false, 1024, true);
            string     expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader);

            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1);

            // Skipping really large JSON since slicing them (O(n^2)) is too slow.
            if (type == TestCaseType.Json40KB || type == TestCaseType.Json400KB || type == TestCaseType.ProjectLockJson)
            {
                var    utf8JsonReader    = new Utf8JsonReader(sequence, isFinalBlock: true, default);
                byte[] resultSequence    = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader);
                string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length);
                Assert.Equal(expectedStr, actualStrSequence);
                return;
            }

            for (int j = 0; j < dataUtf8.Length; j++)
            {
                var    utf8JsonReader    = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default);
                byte[] resultSequence    = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader);
                string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length);

                long consumed = utf8JsonReader.BytesConsumed;
                Assert.Equal(consumed, utf8JsonReader.CurrentState.BytesConsumed);
                utf8JsonReader     = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, utf8JsonReader.CurrentState);
                resultSequence     = JsonTestHelper.ReaderLoop(dataUtf8.Length, out length, ref utf8JsonReader);
                actualStrSequence += Encoding.UTF8.GetString(resultSequence, 0, length);
                string message = $"Expected consumed: {dataUtf8.Length - consumed}, Actual consumed: {utf8JsonReader.BytesConsumed}, Index: {j}";
                Assert.Equal(utf8JsonReader.BytesConsumed, utf8JsonReader.CurrentState.BytesConsumed);
                Assert.True(dataUtf8.Length - consumed == utf8JsonReader.BytesConsumed, message);
                Assert.Equal(expectedStr, actualStrSequence);
            }
        }
コード例 #4
0
        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);
コード例 #5
0
        public static void TestingStringsInvalidConversionToGuid(string testString)
        {
            byte[] dataUtf8 = Encoding.UTF8.GetBytes($"\"{testString}\"");
            var    json     = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default);

            Assert.True(json.Read(), "Read string value");
            Assert.Equal(JsonTokenType.String, json.TokenType);

            Assert.False(json.TryGetGuid(out Guid actual));
            Assert.Equal(Guid.Empty, actual);

            JsonTestHelper.AssertThrows <FormatException>(json, (jsonReader) => jsonReader.GetGuid());
        }
コード例 #6
0
        private static void WriteComplexValue(
            bool indented,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            byte[] bufferOutput;
            var    options = new JsonWriterOptions
            {
                Indented = indented,
            };

            using (JsonDocument doc = JsonDocument.Parse($" [  {jsonIn}  ]", s_options))
            {
                JsonElement target = doc.RootElement[0];
                using var writer = new Utf8JsonWriter(buffer, options);

                target.WriteTo(writer);
                writer.Flush();

                if (indented && s_replaceNewlines)
                {
                    JsonTestHelper.AssertContents(
                        expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                        buffer);
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);

                bufferOutput = buffer.WrittenSpan.ToArray();
            }

            // After reading the output and writing it again, it should be byte-for-byte identical.
            {
                string bufferString = Encoding.UTF8.GetString(bufferOutput);
                buffer.Clear();

                using (JsonDocument doc2 = JsonDocument.Parse(bufferString, s_options))
                {
                    using (var writer = new Utf8JsonWriter(buffer, options))
                    {
                        doc2.RootElement.WriteTo(writer);
                    }
                }

                Assert.True(buffer.WrittenSpan.SequenceEqual(bufferOutput));
            }
        }
コード例 #7
0
        public static void AllowCommentStackMismatchMultiSegment(string jsonString, string expectedWithoutComments, string expectedWithComments)
        {
            byte[] data = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(data, 1);

            TestReadingJsonWithComments(sequence, expectedWithoutComments, expectedWithComments);

            var firstSegment = new BufferSegment <byte>(ReadOnlyMemory <byte> .Empty);
            ReadOnlyMemory <byte> secondMem     = data;
            BufferSegment <byte>  secondSegment = firstSegment.Append(secondMem);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, secondSegment, secondMem.Length);

            TestReadingJsonWithComments(sequence, expectedWithoutComments, expectedWithComments);
        }
コード例 #8
0
        public static void SingleJsonValueMultiSegment(string jsonString, string expectedString)
        {
            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1);

            TestReadingSingleValueJson(sequence, expectedString);

            var firstSegment = new BufferSegment <byte>(ReadOnlyMemory <byte> .Empty);
            ReadOnlyMemory <byte> secondMem     = dataUtf8;
            BufferSegment <byte>  secondSegment = firstSegment.Append(secondMem);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, secondSegment, secondMem.Length);

            TestReadingSingleValueJson(sequence, expectedString);
        }
コード例 #9
0
        public static void TextEqualsEscapedCharAtTheLastSegment()
        {
            string jsonString   = "\"aaaaaa\\u0061\"";
            string lookupString = "aaaaaaa";

            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.True(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(utf8Data);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.True(found);
        }
コード例 #10
0
        public static void WriteWithRelaxedEscaper(bool indented, string jsonIn, string jsonOut, bool matchesRelaxedEscaping)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = JsonDocument.Parse($" [  {jsonIn}  ]", s_options))
            {
                JsonElement target = doc.RootElement[0];

                {
                    var options = new JsonWriterOptions
                    {
                        Indented = indented,
                    };

                    using var writer = new Utf8JsonWriter(buffer, options);

                    target.WriteTo(writer);
                    writer.Flush();

                    if (matchesRelaxedEscaping)
                    {
                        JsonTestHelper.AssertContents(jsonOut, buffer);
                    }
                    else
                    {
                        JsonTestHelper.AssertContentsNotEqual(jsonOut, buffer);
                    }
                }

                buffer.Clear();

                {
                    var options = new JsonWriterOptions
                    {
                        Indented = indented,
                        Encoder  = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                    };

                    using var writer = new Utf8JsonWriter(buffer, options);

                    target.WriteTo(writer);
                    writer.Flush();

                    JsonTestHelper.AssertContents(jsonOut, buffer);
                }
            }
        }
コード例 #11
0
        public static void TestTextEqualsTooLargeToMatch(string jsonString)
        {
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            var lookupString = new string('a', 13);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8Data, 1);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);
        }
コード例 #12
0
        public static void ReadJsonTokenWithExtraValueAndCommentsAppendedMultiSegment(string jsonString)
        {
            jsonString = "  /* comment */  /* comment */  " + jsonString;
            byte[] utf8 = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8, 1);

            foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
            {
                if (commentHandling == JsonCommentHandling.Disallow)
                {
                    continue;
                }

                TestReadTokenWithExtra(sequence, commentHandling, isFinalBlock: false, commentsAppended: true);
                TestReadTokenWithExtra(sequence, commentHandling, isFinalBlock: true, commentsAppended: true);
            }
        }
コード例 #13
0
        public void WriteValueSurrogatesEscapeString()
        {
            string unicodeString = "\uD800\uDC00\uD803\uDE6D \uD834\uDD1E\uDBFF\uDFFF";
            string expectedStr   = "\"\\uD800\\uDC00\\uD803\\uDE6D \\uD834\\uDD1E\\uDBFF\\uDFFF\"";
            string json          = $"\"{unicodeString}\"";
            var    buffer        = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = PrepareDocument(json))
            {
                using (var writer = new Utf8JsonWriter(buffer))
                {
                    WriteSingleValue(doc, writer);
                }

                JsonTestHelper.AssertContents(expectedStr, buffer);
            }
        }
コード例 #14
0
        public void WriteWithRelaxedEscaper(bool indented, string jsonIn, string jsonOut, bool matchesRelaxedEscaping)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = PrepareDocument(jsonIn))
            {
                {
                    var options = new JsonWriterOptions
                    {
                        Indented = indented,
                    };

                    using (var writer = new Utf8JsonWriter(buffer, options))
                    {
                        WriteSingleValue(doc, writer);
                    }

                    if (matchesRelaxedEscaping)
                    {
                        JsonTestHelper.AssertContents(jsonOut, buffer);
                    }
                    else
                    {
                        JsonTestHelper.AssertContentsNotEqual(jsonOut, buffer);
                    }
                }

                buffer.Clear();

                {
                    var options = new JsonWriterOptions
                    {
                        Indented = indented,
                        Encoder  = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                    };

                    using (var writer = new Utf8JsonWriter(buffer, options))
                    {
                        WriteSingleValue(doc, writer);
                    }

                    JsonTestHelper.AssertContents(jsonOut, buffer);
                }
            }
        }
コード例 #15
0
        public static void TestTextEqualsValue(string jsonString, string lookUpString, bool expectedFound)
        {
            byte[] lookup   = Encoding.UTF8.GetBytes(lookUpString);
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);
            bool   found    = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(lookup) &&
                        json.ValueTextEquals(lookUpString) &&
                        json.ValueTextEquals(lookUpString.AsSpan()))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.Equal(expectedFound, found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8Data, 1);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(lookup) &&
                        json.ValueTextEquals(lookUpString) &&
                        json.ValueTextEquals(lookUpString.AsSpan()))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.Equal(expectedFound, found);
        }
コード例 #16
0
        public static void TextMismatchSameLength(string jsonString, string lookupString)
        {
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(utf8Data);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);
        }
コード例 #17
0
        public static void TestTextEqualsTooSmallToMatch(string jsonString)
        {
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(new byte[] { (byte)'a' }) ||
                        json.ValueTextEquals(new char[] { 'a' }) ||
                        json.ValueTextEquals("a"))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8Data, 1);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(new byte[] { (byte)'a' }) ||
                        json.ValueTextEquals(new char[] { 'a' }) ||
                        json.ValueTextEquals("a"))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);
        }
コード例 #18
0
        public static void WriteValueInsideObject(bool skipValidation)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (var doc = JsonDocument.Parse("[ null, false, true, \"hi\", 5, {}, [] ]", s_readerOptions))
            {
                JsonElement root    = doc.RootElement;
                var         options = new JsonWriterOptions
                {
                    SkipValidation = skipValidation,
                };

                var writer = new Utf8JsonWriter(buffer, options);
                writer.WriteStartObject();

                if (skipValidation)
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        val.WriteAsValue(writer);
                    }

                    writer.WriteEndObject();
                    writer.Flush();

                    AssertContents(
                        "{null,false,true,\"hi\",5,{},[]}",
                        buffer);
                }
                else
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        JsonTestHelper.AssertThrows <InvalidOperationException>(
                            ref writer,
                            (ref Utf8JsonWriter w) => val.WriteAsValue(w));
                    }

                    writer.WriteEndObject();
                    writer.Flush();

                    AssertContents("{}", buffer);
                }
            }
        }
コード例 #19
0
        private void WriteComplexValue(
            bool indented,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            byte[] bufferOutput;

            var options = new JsonWriterOptions
            {
                Indented = indented
            };

            using (JsonDocument doc = PrepareDocument(jsonIn))
            {
                using (var writer = new Utf8JsonWriter(buffer, options))
                {
                    WriteSingleValue(doc, writer);
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);

                bufferOutput = buffer.WrittenSpan.ToArray();
            }

            // After reading the output and writing it again, it should be byte-for-byte identical.
            {
                string bufferString = Encoding.UTF8.GetString(bufferOutput);
                buffer.Clear();

                using (JsonDocument doc2 = PrepareDocument(bufferString))
                {
                    using (var writer = new Utf8JsonWriter(buffer, options))
                    {
                        WriteSingleValue(doc2, writer);
                    }
                }

                Assert.True(buffer.WrittenSpan.SequenceEqual(bufferOutput));
            }
        }
コード例 #20
0
        private void WriteSimpleValue(bool indented, string jsonIn, string jsonOut = null)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = PrepareDocument(jsonIn))
            {
                var options = new JsonWriterOptions
                {
                    Indented = indented,
                };

                using (var writer = new Utf8JsonWriter(buffer, options))
                {
                    WriteSingleValue(doc, writer);
                }

                JsonTestHelper.AssertContents(jsonOut ?? jsonIn, buffer);
            }
        }
コード例 #21
0
        public static void WriteValueSurrogatesEscapeString()
        {
            string unicodeString = "\uD800\uDC00\uD803\uDE6D \uD834\uDD1E\uDBFF\uDFFF";
            string json          = $"[\"{unicodeString}\"]";
            var    buffer        = new ArrayBufferWriter <byte>(1024);
            string expectedStr   = GetEscapedExpectedString(unicodeString, StringEscapeHandling.EscapeNonAscii);

            using (JsonDocument doc = JsonDocument.Parse(json, s_options))
            {
                JsonElement target = doc.RootElement[0];

                using (var writer = new Utf8JsonWriter(buffer))
                {
                    target.WriteTo(writer);
                    writer.Flush();
                }
                JsonTestHelper.AssertContents(expectedStr, buffer);
            }
        }
コード例 #22
0
        public static void EmptyJsonWithinSequenceIsInvalid()
        {
            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(new byte[0], 1);
            var json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);

            try
            {
                while (json.Read())
                {
                    ;
                }
                Assert.True(false, "Expected JsonReaderException was not thrown with single-segment data.");
            }
            catch (JsonReaderException ex)
            {
                Assert.Equal(0, ex.LineNumber);
                Assert.Equal(0, ex.BytePositionInLine);
            }
        }
コード例 #23
0
        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);
            }
        }
コード例 #24
0
        private static void WriteSimpleValue(bool indented, string jsonIn, string jsonOut = null)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = JsonDocument.Parse($" [  {jsonIn}  ]", s_options))
            {
                JsonElement target = doc.RootElement[0];

                var options = new JsonWriterOptions
                {
                    Indented = indented,
                };

                using (var writer = new Utf8JsonWriter(buffer, options))
                {
                    target.WriteTo(writer);
                    writer.Flush();
                }
                JsonTestHelper.AssertContents(jsonOut ?? jsonIn, buffer);
            }
        }
コード例 #25
0
        public static void TestPartialJsonReaderSlicesMultiSegment(bool compactData, TestCaseType type, string jsonString)
        {
            // Remove all formatting/indendation
            if (compactData)
            {
                jsonString = JsonTestHelper.GetCompactString(jsonString);
            }

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
            ReadOnlyMemory <byte> dataMemory = dataUtf8;

            List <ReadOnlySequence <byte> > sequences = JsonTestHelper.GetSequences(dataMemory);

            for (int i = 0; i < sequences.Count; i++)
            {
                ReadOnlySequence <byte> sequence = sequences[i];
                for (int j = 0; j < dataUtf8.Length; j++)
                {
                    var json = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default);
                    while (json.Read())
                    {
                        ;
                    }

                    long            consumed      = json.BytesConsumed;
                    JsonReaderState jsonState     = json.CurrentState;
                    byte[]          consumedArray = sequence.Slice(0, consumed).ToArray();
                    Assert.Equal(consumedArray, sequence.Slice(0, json.Position).ToArray());
                    Assert.True(json.Position.Equals(jsonState.Position));
                    json = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, jsonState);
                    while (json.Read())
                    {
                        ;
                    }
                    Assert.Equal(dataUtf8.Length - consumed, json.BytesConsumed);
                    Assert.Equal(json.BytesConsumed, json.CurrentState.BytesConsumed);
                }
            }
        }
コード例 #26
0
        private void WriteComplexValue(
            bool indented,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            var buffer = new ArrayBufferWriter <byte>(1024);

            using (JsonDocument doc = PrepareDocument(jsonIn))
            {
                var options = new JsonWriterOptions
                {
                    Indented = indented
                };

                using (var writer = new Utf8JsonWriter(buffer, options))
                {
                    WriteSingleValue(doc, writer);
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
            }
        }
コード例 #27
0
        private static void ReadFullySegmentSizeOne(bool compactData, TestCaseType type, string jsonString)
        {
            // Remove all formatting/indendation
            if (compactData)
            {
                jsonString = JsonTestHelper.GetCompactString(jsonString);
            }

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            Stream     stream      = new MemoryStream(dataUtf8);
            TextReader reader      = new StreamReader(stream, Encoding.UTF8, false, 1024, true);
            string     expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader);

            ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1);

            var utf8JsonReader = new Utf8JsonReader(sequence, isFinalBlock: true, default);

            byte[] resultSequence    = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader);
            string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length);

            Assert.Equal(expectedStr, actualStrSequence);
        }
コード例 #28
0
        private static void SpanSequenceStatesAreEqual(byte[] dataUtf8)
        {
            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(dataUtf8);

            var jsonSpan     = new Utf8JsonReader(dataUtf8, isFinalBlock: true, default);
            var jsonSequence = new Utf8JsonReader(sequence, isFinalBlock: true, default);

            while (true)
            {
                bool spanResult     = jsonSpan.Read();
                bool sequenceResult = jsonSequence.Read();

                Assert.Equal(spanResult, sequenceResult);
                Assert.Equal(jsonSpan.CurrentDepth, jsonSequence.CurrentDepth);
                Assert.Equal(jsonSpan.BytesConsumed, jsonSequence.BytesConsumed);
                Assert.Equal(jsonSpan.TokenType, jsonSequence.TokenType);

                if (!spanResult)
                {
                    break;
                }
            }
        }
コード例 #29
0
        private static void WritePropertyValue(
            bool indented,
            string propertyName,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            var    buffer = new ArrayBufferWriter <byte>(1024);
            string temp   = $" [  {jsonIn}  ]";

            using (JsonDocument doc = JsonDocument.Parse(temp, s_options))
            {
                JsonElement target = doc.RootElement[0];

                var options = new JsonWriterOptions
                {
                    Indented = indented,
                };

                using var writer = new Utf8JsonWriter(buffer, options);

                writer.WriteStartObject();
                writer.WritePropertyName(propertyName);
                target.WriteTo(writer);
                writer.WriteEndObject();
                writer.Flush();

                if (indented && s_replaceNewlines)
                {
                    JsonTestHelper.AssertContents(
                        expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                        buffer);
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
            }
        }
コード例 #30
0
        public static void TestTextEqualsLargeMismatch()
        {
            var jsonChars = new char[320];  // Some value larger than 256 (stack threshold)

            jsonChars.AsSpan().Fill('a');
            ReadOnlySpan <char> escapedA = new char[6] {
                '\\', 'u', '0', '0', '6', '1'
            };

            byte[] originalLookup = Encoding.UTF8.GetBytes(jsonChars);

            char[] originalLookupChars = new char[jsonChars.Length];
            Array.Copy(jsonChars, originalLookupChars, jsonChars.Length);

            for (int i = 1; i < jsonChars.Length - 6; i++)
            {
                jsonChars.AsSpan().Fill('a');
                escapedA.CopyTo(jsonChars.AsSpan(i));
                string jsonString = "\"" + new string(jsonChars) + "\"";
                byte[] utf8Data   = Encoding.UTF8.GetBytes(jsonString);

                for (int j = 0; j < 3; j++)
                {
                    Span <byte> lookup = new byte[originalLookup.Length];
                    originalLookup.CopyTo(lookup);
                    lookup = lookup.Slice(0, lookup.Length - escapedA.Length + 1);    // remove extra characters that were replaced by escaped bytes

                    Span <char> lookupChars = new char[originalLookupChars.Length];
                    originalLookupChars.CopyTo(lookupChars);
                    lookupChars = lookupChars.Slice(0, lookupChars.Length - escapedA.Length + 1);    // remove extra characters that were replaced by escaped bytes

                    switch (j)
                    {
                    case 0:
                        lookup[i]      = (byte)'b';
                        lookupChars[i] = 'b';
                        break;

                    case 1:
                        lookup[i + 1]      = (byte)'b';
                        lookupChars[i + 1] = 'b';
                        break;

                    case 2:
                        lookup[i - 1]      = (byte)'b';
                        lookupChars[i - 1] = 'b';
                        break;
                    }

                    bool found = false;

                    var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);
                    while (json.Read())
                    {
                        if (json.TokenType == JsonTokenType.String)
                        {
                            if (json.ValueTextEquals(lookup) ||
                                json.ValueTextEquals(lookupChars) ||
                                json.ValueTextEquals(new string(lookupChars.ToArray())))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    Assert.False(found, $"Json String: {jsonString}");

                    ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(utf8Data, 1);
                    found = false;

                    json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
                    while (json.Read())
                    {
                        if (json.TokenType == JsonTokenType.String)
                        {
                            if (json.ValueTextEquals(lookup) ||
                                json.ValueTextEquals(lookupChars) ||
                                json.ValueTextEquals(new string(lookupChars.ToArray())))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    Assert.False(found);
                }
            }
        }