Esempio n. 1
0
        private void WritePropertyValue(
            bool indented,
            JsonEncodedText propertyName,
            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))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(propertyName);
                    WriteSingleValue(doc, writer);
                    writer.WriteEndObject();
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
            }
        }
Esempio n. 2
0
        private static void WriteComplexValue(
            bool indented,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            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 (indented && s_replaceNewlines)
                {
                    JsonTestHelper.AssertContents(
                        expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                        buffer);
                }

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
            }
        }
Esempio n. 3
0
        private static void WritePropertyValue(
            bool indented,
            JsonEncodedText propertyName,
            string jsonIn,
            string expectedIndent,
            string expectedMinimal)
        {
            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);

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

                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
            }
        }
Esempio n. 4
0
        public static 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
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        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);
                }
            }
        }
        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));
            }
        }
Esempio n. 6
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);
                }
            }
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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);
                }
            }
        }
Esempio n. 9
0
        public void WriteValueInsideObject(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,
                };

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

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

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

                    JsonTestHelper.AssertContents(
                        "{null,false,true,\"hi\",5,{},[]}",
                        buffer);
                }
                else
                {
                    foreach (JsonElement val in root.EnumerateArray())
                    {
                        Assert.Throws <InvalidOperationException>(() => val.WriteTo(writer));
                    }

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

                    JsonTestHelper.AssertContents("{}", buffer);
                }
            }
        }
Esempio n. 10
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));
            }
        }
Esempio n. 11
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);
            }
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
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);
            }
        }
Esempio n. 14
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);
            }
        }