Beispiel #1
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);
                }
            }
        }
Beispiel #2
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);
                }
            }
        }