Use relaxed JavascriptEncoder in S.T.JSON specific tests to improve coverage. (dotnet...
authorAhson Khan <ahson_ahmedk@yahoo.com>
Mon, 29 Jul 2019 04:17:28 +0000 (21:17 -0700)
committerGitHub <noreply@github.com>
Mon, 29 Jul 2019 04:17:28 +0000 (21:17 -0700)
* Use relaxed JavascriptEncoder in S.T.JSON specific tests to improve
coverage.

* Add control characters as well as part of the test data.

* Add JsonElement.WriteTo test and make sure all Utf8JsonWriter in tests
are disposed.

* Fix spacing/formatting.

* Fix call to AssertEquals since it moved to JsonTestHelper.

Commit migrated from https://github.com/dotnet/corefx/commit/74d26be704117139e9ef081c9e287740fde5d002

src/libraries/System.Text.Json/tests/DebuggerTests.cs
src/libraries/System.Text.Json/tests/JsonDocumentTests.cs
src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs
src/libraries/System.Text.Json/tests/JsonPropertyTests.cs
src/libraries/System.Text.Json/tests/JsonTestHelper.cs
src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs
src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs

index 13a50ca..2100e75 100644 (file)
@@ -30,7 +30,7 @@ namespace System.Text.Json.Tests
         public void DefaultUtf8JsonWriter()
         {
             // Validating that we don't throw on new object
-            Utf8JsonWriter writer = new Utf8JsonWriter(new MemoryStream());
+            using var writer = new Utf8JsonWriter(new MemoryStream());
             GetDebuggerDisplayProperty(writer);
         }
 
index fa15907..be76211 100644 (file)
@@ -1769,19 +1769,19 @@ namespace System.Text.Json.Tests
 
                 Assert.Throws<ObjectDisposedException>(() =>
                 {
-                    Utf8JsonWriter writer = new Utf8JsonWriter(buffer);
+                    using var writer = new Utf8JsonWriter(buffer);
                     root.WriteTo(writer);
                 });
 
                 Assert.Throws<ObjectDisposedException>(() =>
                 {
-                    Utf8JsonWriter writer = new Utf8JsonWriter(buffer);
+                    using var writer = new Utf8JsonWriter(buffer);
                     doc.WriteTo(writer);
                 });
 
                 Assert.Throws<ObjectDisposedException>(() =>
                 {
-                    Utf8JsonWriter writer = new Utf8JsonWriter(buffer);
+                    using var writer = new Utf8JsonWriter(buffer);
                     property.WriteTo(writer);
                 });
             }
@@ -1831,7 +1831,7 @@ namespace System.Text.Json.Tests
             Assert.Throws<InvalidOperationException>(() =>
             {
                 var buffer = new ArrayBufferWriter<byte>(1024);
-                Utf8JsonWriter writer = new Utf8JsonWriter(buffer);                
+                using var writer = new Utf8JsonWriter(buffer);
                 root.WriteTo(writer);
             });
         }
@@ -3693,7 +3693,7 @@ namespace System.Text.Json.Tests
             var expectedNonIndentedJson = $"[{OneQuarticGoogol}]";
             using (JsonDocument doc = JsonDocument.Parse($"[ {OneQuarticGoogol} ]"))
             {
-                var writer = new Utf8JsonWriter(buffer, default);
+                using var writer = new Utf8JsonWriter(buffer, default);
                 doc.WriteTo(writer);
                 writer.Flush();
 
index 9dedb7d..9a0b468 100644 (file)
@@ -6,6 +6,7 @@ using Xunit;
 using System.Buffers;
 using Newtonsoft.Json;
 using System.IO;
+using System.Text.Encodings.Web;
 
 namespace System.Text.Json.Tests
 {
@@ -42,7 +43,7 @@ namespace System.Text.Json.Tests
         }
 
         [Theory]
-        [InlineData("12E-3", false)]    
+        [InlineData("12E-3", false)]
         [InlineData("1e6", false)]
         [InlineData("1e6", true)]
         [InlineData("1e+6", false)]
@@ -168,7 +169,7 @@ namespace System.Text.Json.Tests
                 JsonElement rootElement = document.RootElement;
 
                 Assert.Equal(overwriteJson, rootElement.GetRawText());
-                
+
                 AssertExtensions.Throws<ArgumentException>(
                     "utf8FormattedNumber",
                     () => rootElement.WriteTo(writer));
@@ -543,7 +544,7 @@ null,
                     target.WriteTo(writer);
                     writer.Flush();
                 }
-                AssertContents(expectedStr, buffer);
+                JsonTestHelper.AssertContents(expectedStr, buffer);
             }
         }
 
@@ -967,7 +968,7 @@ null,
             var buffer = new ArrayBufferWriter<byte>(jsonIn.Length);
             using (JsonDocument doc = JsonDocument.Parse(jsonIn, optionsCopy))
             {
-                var writer = new Utf8JsonWriter(buffer);
+                using var writer = new Utf8JsonWriter(buffer);
                 doc.RootElement.WriteTo(writer);
                 writer.Flush();
 
@@ -995,7 +996,7 @@ null,
 
                 const string CharLabel = "char";
                 byte[] byteUtf8 = Encoding.UTF8.GetBytes("byte");
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 if (skipValidation)
                 {
@@ -1013,7 +1014,7 @@ null,
 
                     writer.Flush();
 
-                    AssertContents(
+                    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," +
@@ -1035,7 +1036,7 @@ null,
 
                     writer.Flush();
 
-                    AssertContents("", buffer);
+                    JsonTestHelper.AssertContents("", buffer);
                 }
             }
         }
@@ -1054,7 +1055,7 @@ null,
                     SkipValidation = skipValidation,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
                 writer.WriteStartObject();
 
                 if (skipValidation)
@@ -1067,7 +1068,7 @@ null,
                     writer.WriteEndObject();
                     writer.Flush();
 
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         "{null,false,true,\"hi\",5,{},[]}",
                         buffer);
                 }
@@ -1081,7 +1082,61 @@ null,
                     writer.WriteEndObject();
                     writer.Flush();
 
-                    AssertContents("{}", buffer);
+                    JsonTestHelper.AssertContents("{}", buffer);
+                }
+            }
+        }
+
+        [Theory]
+        [InlineData(false, "\"message\"", "\"message\"", true)]
+        [InlineData(true, "\"message\"", "\"message\"", true)]
+        [InlineData(false, "\">><++>>>\\\">>\\\\>>&>>>\u6f22\u5B57>>>\"", "\">><++>>>\\\">>\\\\>>&>>>\u6f22\u5B57>>>\"", false)]
+        [InlineData(true, "\">><++>>>\\\">>\\\\>>&>>>\u6f22\u5B57>>>\"", "\">><++>>>\\\">>\\\\>>&>>>\u6f22\u5B57>>>\"", false)]
+        [InlineData(false, "\"mess\\r\\nage\\u0008\\u0001!\"", "\"mess\\r\\nage\\b\\u0001!\"", true)]
+        [InlineData(true, "\"mess\\r\\nage\\u0008\\u0001!\"", "\"mess\\r\\nage\\b\\u0001!\"", true)]
+        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);
                 }
             }
         }
@@ -1103,7 +1158,7 @@ null,
                     target.WriteTo(writer);
                     writer.Flush();
                 }
-                AssertContents(jsonOut ?? jsonIn, buffer);
+                JsonTestHelper.AssertContents(jsonOut ?? jsonIn, buffer);
             }
         }
 
@@ -1123,19 +1178,19 @@ null,
                     Indented = indented,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 target.WriteTo(writer);
                 writer.Flush();
 
                 if (indented && s_replaceNewlines)
                 {
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                         buffer);
                 }
 
-                AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
             }
         }
 
@@ -1193,7 +1248,7 @@ null,
                     Indented = indented,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 writer.WriteStartObject();
                 writer.WritePropertyName(propertyName);
@@ -1203,12 +1258,12 @@ null,
 
                 if (indented && s_replaceNewlines)
                 {
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                         buffer);
                 }
 
-                AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
             }
         }
 
@@ -1229,7 +1284,7 @@ null,
                     Indented = indented,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 writer.WriteStartObject();
                 writer.WritePropertyName(propertyName);
@@ -1239,12 +1294,12 @@ null,
 
                 if (indented && s_replaceNewlines)
                 {
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                         buffer);
                 }
 
-                AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
             }
         }
 
@@ -1265,7 +1320,7 @@ null,
                     Indented = indented,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 writer.WriteStartObject();
                 writer.WritePropertyName(propertyName);
@@ -1275,12 +1330,12 @@ null,
 
                 if (indented && s_replaceNewlines)
                 {
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                         buffer);
                 }
 
-                AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
             }
         }
 
@@ -1301,7 +1356,7 @@ null,
                     Indented = indented,
                 };
 
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
 
                 writer.WriteStartObject();
                 writer.WritePropertyName(propertyName);
@@ -1311,26 +1366,14 @@ null,
 
                 if (indented && s_replaceNewlines)
                 {
-                    AssertContents(
+                    JsonTestHelper.AssertContents(
                         expectedIndent.Replace(CompiledNewline, Environment.NewLine),
                         buffer);
                 }
 
-                AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
+                JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer);
             }
         }
 
-        private static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
-        {
-            string value = Encoding.UTF8.GetString(
-                    buffer.WrittenSpan
-#if netfx
-                        .ToArray()
-#endif
-                    );
-
-            // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
-            Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
-        }
     }
 }
index 93055a1..2625578 100644 (file)
@@ -36,7 +36,7 @@ namespace System.Text.Json.Tests
                 {
                     SkipValidation = skipValidation,
                 };
-                var writer = new Utf8JsonWriter(buffer, options);
+                using var writer = new Utf8JsonWriter(buffer, options);
                 if (skipValidation)
                 {
                     foreach (JsonProperty property in root.EnumerateObject())
@@ -67,7 +67,7 @@ namespace System.Text.Json.Tests
             var buffer = new ArrayBufferWriter<byte>(1024);
             using (JsonDocument doc = JsonDocument.Parse("{\"First\":1, \"Number\":1e400}"))
             {
-                var writer = new Utf8JsonWriter(buffer);
+                using var writer = new Utf8JsonWriter(buffer);
                 writer.WriteStartObject();
                 foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
                 {
index 2c06356..bf0b185 100644 (file)
@@ -673,6 +673,32 @@ namespace System.Text.Json
             }
         }
 
+        public static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
+        {
+            string value = Encoding.UTF8.GetString(
+                    buffer.WrittenSpan
+#if netfx
+                        .ToArray()
+#endif
+                    );
+
+            // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
+            Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
+        }
+
+        public static void AssertContentsNotEqual(string expectedValue, ArrayBufferWriter<byte> buffer)
+        {
+            string value = Encoding.UTF8.GetString(
+                    buffer.WrittenSpan
+#if netfx
+                        .ToArray()
+#endif
+                    );
+
+            // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
+            Assert.NotEqual(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
+        }
+
         public delegate void AssertThrowsActionUtf8JsonReader(Utf8JsonReader json);
 
         // Cannot use standard Assert.Throws() when testing Utf8JsonReader - ref structs and closures don't get along.
index 297e7bb..8cd5393 100644 (file)
@@ -2,6 +2,8 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Text.Encodings.Web;
+using Newtonsoft.Json;
 using Xunit;
 
 namespace System.Text.Json.Serialization.Tests
@@ -9,6 +11,18 @@ namespace System.Text.Json.Serialization.Tests
     public static partial class ValueTests
     {
         [Fact]
+        public static void WriteStringWithRelaxedEscaper()
+        {
+            string inputString = ">><++>>>\">>\\>>&>>>\u6f22\u5B57>>>"; // Non-ASCII text should remain unescaped. \u6f22 = æ±‰, \u5B57 = å­—
+
+            string actual = JsonSerializer.Serialize(inputString, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
+            string expected = "\">><++>>>\\\">>\\\\>>&>>>\u6f22\u5B57>>>\"";
+            Assert.Equal(JsonConvert.SerializeObject(inputString), actual);
+            Assert.Equal(expected, actual);
+            Assert.NotEqual(expected, JsonSerializer.Serialize(inputString));
+        }
+
+        [Fact]
         public static void WritePrimitives()
         {
             {
index 2e290ed..17d0fd0 100644 (file)
@@ -64,24 +64,28 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var stream = new MemoryStream();
-            var writer = new Utf8JsonWriter(stream, options);
-            Assert.Equal(0, writer.BytesCommitted);
-            Assert.Equal(0, writer.BytesPending);
-            Assert.Equal(0, writer.CurrentDepth);
-            Assert.Null(writer.Options.Encoder);
-            Assert.Equal(formatted, writer.Options.Indented);
-            Assert.Equal(skipValidation, writer.Options.SkipValidation);
-            Assert.Equal(0, stream.Position);
+            using (var writer = new Utf8JsonWriter(stream, options))
+            {
+                Assert.Equal(0, writer.BytesCommitted);
+                Assert.Equal(0, writer.BytesPending);
+                Assert.Equal(0, writer.CurrentDepth);
+                Assert.Null(writer.Options.Encoder);
+                Assert.Equal(formatted, writer.Options.Indented);
+                Assert.Equal(skipValidation, writer.Options.SkipValidation);
+                Assert.Equal(0, stream.Position);
+            }
 
             var output = new FixedSizedBufferWriter(0);
-            writer = new Utf8JsonWriter(output, options);
-            Assert.Equal(0, writer.BytesCommitted);
-            Assert.Equal(0, writer.BytesPending);
-            Assert.Equal(0, writer.CurrentDepth);
-            Assert.Null(writer.Options.Encoder);
-            Assert.Equal(formatted, writer.Options.Indented);
-            Assert.Equal(skipValidation, writer.Options.SkipValidation);
-            Assert.Equal(0, output.FormattedCount);
+            using (var writer = new Utf8JsonWriter(output, options))
+            {
+                Assert.Equal(0, writer.BytesCommitted);
+                Assert.Equal(0, writer.BytesPending);
+                Assert.Equal(0, writer.CurrentDepth);
+                Assert.Null(writer.Options.Encoder);
+                Assert.Equal(formatted, writer.Options.Indented);
+                Assert.Equal(skipValidation, writer.Options.SkipValidation);
+                Assert.Equal(0, output.FormattedCount);
+            }
         }
 
         [Theory]
@@ -94,7 +98,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteNumberValue(1);
             writeToStream.Flush();
 
@@ -114,7 +118,7 @@ namespace System.Text.Json.Tests
             Assert.Equal(previousWritten, stream.Position);
 
             var output = new FixedSizedBufferWriter(256);
-            var writeToIBW = new Utf8JsonWriter(output, options);
+            using var writeToIBW = new Utf8JsonWriter(output, options);
             writeToIBW.WriteNumberValue(1);
             writeToIBW.Flush();
 
@@ -144,7 +148,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteNumberValue(1);
             writeToStream.Flush();
 
@@ -170,7 +174,7 @@ namespace System.Text.Json.Tests
             Assert.Equal("11", Encoding.UTF8.GetString(stream.ToArray()));
 
             var output = new FixedSizedBufferWriter(257);
-            var writeToIBW = new Utf8JsonWriter(output, options);
+            using var writeToIBW = new Utf8JsonWriter(output, options);
             writeToIBW.WriteNumberValue(1);
             writeToIBW.Flush();
 
@@ -206,7 +210,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteNumberValue(1);
             writeToStream.Flush();
 
@@ -237,7 +241,7 @@ namespace System.Text.Json.Tests
             Assert.True(output.FormattedCount != 0);
 
             output = new FixedSizedBufferWriter(256);
-            var writeToIBW = new Utf8JsonWriter(output, options);
+            using var writeToIBW = new Utf8JsonWriter(output, options);
             writeToIBW.WriteNumberValue(1);
             writeToIBW.Flush();
 
@@ -278,7 +282,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
 
             Assert.Throws<ArgumentNullException>(() => writeToStream.Reset((Stream)null));
             Assert.Throws<ArgumentNullException>(() => writeToStream.Reset((IBufferWriter<byte>)null));
@@ -288,7 +292,7 @@ namespace System.Text.Json.Tests
             Assert.Throws<ArgumentException>(() => writeToStream.Reset(stream));
 
             var output = new FixedSizedBufferWriter(256);
-            var writeToIBW = new Utf8JsonWriter(output, options);
+            using var writeToIBW = new Utf8JsonWriter(output, options);
 
             Assert.Throws<ArgumentNullException>(() => writeToIBW.Reset((Stream)null));
             Assert.Throws<ArgumentNullException>(() => writeToIBW.Reset((IBufferWriter<byte>)null));
@@ -306,13 +310,13 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(0);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.Flush();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(0, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.Flush();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(0, stream.Position);
@@ -328,13 +332,13 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(0);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             await jsonUtf8.FlushAsync();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(0, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             await writeToStream.FlushAsync();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(0, stream.Position);
@@ -350,7 +354,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             jsonUtf8.WriteEndObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
@@ -366,7 +370,7 @@ namespace System.Text.Json.Tests
             Assert.Equal(2, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             writeToStream.WriteEndObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
@@ -392,7 +396,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             jsonUtf8.WriteEndObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
@@ -408,7 +412,7 @@ namespace System.Text.Json.Tests
             Assert.Equal(2, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             writeToStream.WriteEndObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
@@ -434,7 +438,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             jsonUtf8.WriteEndObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
@@ -444,7 +448,7 @@ namespace System.Text.Json.Tests
             Assert.Equal(2, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             writeToStream.WriteEndObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
@@ -464,7 +468,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             jsonUtf8.WriteEndObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
@@ -474,7 +478,7 @@ namespace System.Text.Json.Tests
             Assert.Equal(2, output.FormattedCount);
 
             var stream = new MemoryStream();
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             writeToStream.WriteEndObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
@@ -494,7 +498,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(1, jsonUtf8.BytesPending);
@@ -512,7 +516,7 @@ namespace System.Text.Json.Tests
             var stream = new MemoryStream();
             Assert.Throws<ObjectDisposedException>(() => jsonUtf8.Reset(stream));
 
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
             Assert.Equal(1, writeToStream.BytesPending);
@@ -540,7 +544,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new FixedSizedBufferWriter(256);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             Assert.Equal(0, jsonUtf8.BytesCommitted);
             Assert.Equal(1, jsonUtf8.BytesPending);
@@ -558,7 +562,7 @@ namespace System.Text.Json.Tests
             var stream = new MemoryStream();
             Assert.Throws<ObjectDisposedException>(() => jsonUtf8.Reset(stream));
 
-            var writeToStream = new Utf8JsonWriter(stream, options);
+            using var writeToStream = new Utf8JsonWriter(stream, options);
             writeToStream.WriteStartObject();
             Assert.Equal(0, writeToStream.BytesCommitted);
             Assert.Equal(1, writeToStream.BytesPending);
@@ -586,7 +590,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new InvalidBufferWriter();
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumberValue((ulong)12345678901));
         }
@@ -665,20 +669,24 @@ namespace System.Text.Json.Tests
 
             byte[] utf8String = Encoding.UTF8.GetBytes(new string('a', 215));
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
             Guid guid = Guid.NewGuid();
-
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(guid));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.Flush();
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(guid));
+            }
 
             sizeTooSmall += formatted ? 9 : 1;
             output = new FixedSizedBufferWriter(sizeTooSmall);
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
-            jsonUtf8.WriteStringValue(guid);
-            jsonUtf8.Flush();
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.WriteStringValue(guid);
+                jsonUtf8.Flush();
+            }
             string actualStr = Encoding.UTF8.GetString(output.Formatted);
 
             if (!formatted)
@@ -702,20 +710,24 @@ namespace System.Text.Json.Tests
 
             byte[] utf8String = Encoding.UTF8.GetBytes(new string('a', 232));
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
             var date = new DateTime(2019, 1, 1);
-
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(date));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.Flush();
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(date));
+            }
 
             sizeTooSmall += formatted ? 23 : 15;
             output = new FixedSizedBufferWriter(sizeTooSmall);
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
-            jsonUtf8.WriteStringValue(date);
-            jsonUtf8.Flush();
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.WriteStringValue(date);
+                jsonUtf8.Flush();
+            }
             string actualStr = Encoding.UTF8.GetString(output.Formatted);
 
             if (!formatted)
@@ -739,20 +751,24 @@ namespace System.Text.Json.Tests
 
             byte[] utf8String = Encoding.UTF8.GetBytes(new string('a', 226));
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
             DateTimeOffset date = new DateTime(2019, 1, 1);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
-
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(date));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.Flush();
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue(date));
+            }
 
             sizeTooSmall += formatted ? 23 : 15;
             output = new FixedSizedBufferWriter(sizeTooSmall);
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStringValue(utf8String);
-            jsonUtf8.WriteStringValue(date);
-            jsonUtf8.Flush();
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStringValue(utf8String);
+                jsonUtf8.WriteStringValue(date);
+                jsonUtf8.Flush();
+            }
             string actualStr = Encoding.UTF8.GetString(output.Formatted);
 
             if (!formatted)
@@ -777,7 +793,7 @@ namespace System.Text.Json.Tests
                 var output = new FixedSizedBufferWriter(256);
                 decimal value = JsonTestHelper.NextDecimal(random, 78E14, -78E14);
 
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteNumberValue(value);
 
                 jsonUtf8.Flush();
@@ -791,7 +807,7 @@ namespace System.Text.Json.Tests
             {
                 var output = new FixedSizedBufferWriter(256);
                 decimal value = JsonTestHelper.NextDecimal(random, 1_000_000, -1_000_000);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteNumberValue(value);
 
                 jsonUtf8.Flush();
@@ -804,7 +820,7 @@ namespace System.Text.Json.Tests
             {
                 var output = new FixedSizedBufferWriter(256);
                 decimal value = 9999999999999999999999999999m;
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteNumberValue(value);
 
                 jsonUtf8.Flush();
@@ -817,7 +833,7 @@ namespace System.Text.Json.Tests
             {
                 var output = new FixedSizedBufferWriter(256);
                 decimal value = -9999999999999999999999999999m;
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteNumberValue(value);
 
                 jsonUtf8.Flush();
@@ -828,26 +844,30 @@ namespace System.Text.Json.Tests
             }
 
             {
-
                 int sizeTooSmall = 256;
                 var output = new FixedSizedBufferWriter(sizeTooSmall);
 
                 byte[] utf8String = Encoding.UTF8.GetBytes(new string('a', 222));
 
                 decimal value = -0.9999999999999999999999999999m;
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
-                jsonUtf8.WriteStartArray();
-                jsonUtf8.WriteStringValue(utf8String);
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumberValue(value));
+                using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+                {
+                    jsonUtf8.WriteStartArray();
+                    jsonUtf8.WriteStringValue(utf8String);
+                    jsonUtf8.Flush();
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumberValue(value));
+                }
 
                 sizeTooSmall += formatted ? 9 : 1;
                 output = new FixedSizedBufferWriter(sizeTooSmall);
-                jsonUtf8 = new Utf8JsonWriter(output, options);
-                jsonUtf8.WriteStartArray();
-                jsonUtf8.WriteStringValue(utf8String);
-                jsonUtf8.WriteNumberValue(value);
+                using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+                {
+                    jsonUtf8.WriteStartArray();
+                    jsonUtf8.WriteStringValue(utf8String);
+                    jsonUtf8.WriteNumberValue(value);
 
-                jsonUtf8.Flush();
+                    jsonUtf8.Flush();
+                }
                 string actualStr = Encoding.UTF8.GetString(output.Formatted);
 
                 if (!formatted)
@@ -1204,355 +1224,415 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndArray();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartArray("property at start");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property at start"));
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray("property at start");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property at start"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartObject("property at start");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("property at start"));
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject("property at start");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("property at start"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartArray("property inside array");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property inside array"));
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray("property inside array");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property inside array"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
                 jsonUtf8.WriteStartObject();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStringValue("key");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue("key"));
+                jsonUtf8.WriteStartObject();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStringValue("key");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStringValue("key"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteString("key", "value");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("key", "value"));
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteString("key", "value");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("key", "value"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteString(JsonEncodedText.Encode("key"), JsonEncodedText.Encode("value"));
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString(JsonEncodedText.Encode("key"), JsonEncodedText.Encode("value")));
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteString(JsonEncodedText.Encode("key"), JsonEncodedText.Encode("value"));
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString(JsonEncodedText.Encode("key"), JsonEncodedText.Encode("value")));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                jsonUtf8.WriteEndArray();
+                jsonUtf8.WriteStartObject();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
-            else
+
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                jsonUtf8.WriteStartObject();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
                 jsonUtf8.WriteStartArray();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray());
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteEndArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteEndArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
-            }
-
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteStartObject("some object");
-            jsonUtf8.WriteEndObject();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndArray();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
-            }
-
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
+                jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteStartObject("some object");
                 jsonUtf8.WriteEndObject();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
-            else
+
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("some object"));
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject("some object");
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("some object"));
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteStartArray("test array");
-            jsonUtf8.WriteEndArray();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WriteStartArray("test array");
                 jsonUtf8.WriteEndArray();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteEndArray();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
+                jsonUtf8.WriteStartArray();
                 jsonUtf8.WriteEndArray();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteEndObject();
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
+                jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteEndObject();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteStartObject("test object");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndArray();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WriteStartObject("test object");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-                jsonUtf8.WritePropertyName(JsonEncodedText.Encode("test name"));
-                jsonUtf8.WritePropertyName("test name".AsSpan());
-                jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes("test name"));
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName(JsonEncodedText.Encode("test name")));
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name".AsSpan()));
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes("test name")));
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                    jsonUtf8.WritePropertyName(JsonEncodedText.Encode("test name"));
+                    jsonUtf8.WritePropertyName("test name".AsSpan());
+                    jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes("test name"));
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName(JsonEncodedText.Encode("test name")));
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name".AsSpan()));
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes("test name")));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteStartArray();
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartArray("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("test name"));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartObject("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("test name"));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteString("another property name", "some value");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("another property name", "some value"));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteString("another property name", "some value");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("another property name", "some value"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteNumber("another property name", 12345);
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumber("another property name", 12345));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteNumber("another property name", 12345);
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumber("another property name", 12345));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteNull("another property name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNull("another property name"));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteNull("another property name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNull("another property name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WritePropertyName("first name");
-            if (skipValidation)
-            {
-                jsonUtf8.WriteBoolean("another property name", true);
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteBoolean("another property name", true));
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WritePropertyName("first name");
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteBoolean("another property name", true);
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteBoolean("another property name", true));
+                }
             }
         }
 
@@ -1566,36 +1646,48 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteEndArray();
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteEndArray();
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteStartObject("some object");
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WriteStartObject("some object");
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartArray();
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteEndObject();
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartArray();
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WriteEndObject();
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            jsonUtf8.WriteStartArray("test array");
-            jsonUtf8.WriteEndArray();
-            Assert.True(jsonUtf8.CurrentDepth != 0);
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                jsonUtf8.WriteStartArray("test array");
+                jsonUtf8.WriteEndArray();
+                Assert.True(jsonUtf8.CurrentDepth != 0);
+            }
         }
 
         [Theory]
@@ -1608,147 +1700,173 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
                 jsonUtf8.WriteNumberValue(12345);
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumberValue(12345));
-            }
-
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartArray();
-            }
-            else
-            {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray());
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteNumberValue(12345);
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteNumberValue(12345));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject());
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartArray("property name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property name"));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteStartObject("property name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("property name"));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartArray("property name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("property name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteString("property name", "value");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("property name", "value"));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteStartObject("property name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartObject("property name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteString(JsonEncodedText.Encode("property name"), JsonEncodedText.Encode("value"));
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString(JsonEncodedText.Encode("property name"), JsonEncodedText.Encode("value")));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteString("property name", "value");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString("property name", "value"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndArray();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteString(JsonEncodedText.Encode("property name"), JsonEncodedText.Encode("value"));
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteString(JsonEncodedText.Encode("property name"), JsonEncodedText.Encode("value")));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WriteEndObject();
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndArray();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndArray());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNumberValue(12345);
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WriteEndObject();
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteEndObject());
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteBooleanValue(true);
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteNumberValue(12345);
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteNullValue();
-            if (skipValidation)
-            {
-                jsonUtf8.WritePropertyName("test name");
-            }
-            else
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteBooleanValue(true);
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStringValue("some string");
-            if (skipValidation)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                jsonUtf8.WritePropertyName("test name");
+                jsonUtf8.WriteNullValue();
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
-            else
+
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                jsonUtf8.WriteStringValue("some string");
+                if (skipValidation)
+                {
+                    jsonUtf8.WritePropertyName("test name");
+                }
+                else
+                {
+                    Assert.Throws<InvalidOperationException>(() => jsonUtf8.WritePropertyName("test name"));
+                }
             }
         }
 
@@ -1762,47 +1880,71 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.NegativeInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.NegativeInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.PositiveInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.PositiveInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.NaN));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(double.NaN));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.PositiveInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.PositiveInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.NegativeInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.NegativeInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.NaN));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumberValue(float.NaN));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.NegativeInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.NegativeInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.PositiveInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.PositiveInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.NaN));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", double.NaN));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.PositiveInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.PositiveInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.NegativeInfinity));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.NegativeInfinity));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.NaN));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteNumber("name", float.NaN));
+            }
         }
 
         [Theory]
@@ -1813,7 +1955,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = true };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             for (int i = 0; i < 100; i++)
             {
@@ -1835,7 +1977,7 @@ namespace System.Text.Json.Tests
                 sb.Append(Environment.NewLine);
             sb.Append("[]");
 
-            AssertContents(sb.ToString(), output);
+            JsonTestHelper.AssertContents(sb.ToString(), output);
         }
 
         [Theory]
@@ -1872,7 +2014,7 @@ namespace System.Text.Json.Tests
 
             string expectedStr = stringWriter.ToString();
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
             jsonUtf8.WriteStartObject();
             jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes("foo1"));
             jsonUtf8.WriteStringValue("bar1");
@@ -1900,7 +2042,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             for (int i = 0; i < 1000; i++)
             {
@@ -1920,30 +2062,36 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            for (int i = 0; i < 999; i++)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                jsonUtf8.WriteStartObject("name");
+                jsonUtf8.WriteStartObject();
+                for (int i = 0; i < 999; i++)
+                {
+                    jsonUtf8.WriteStartObject("name");
+                }
+                Assert.Equal(1000, jsonUtf8.CurrentDepth);
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("name"));
             }
-            Assert.Equal(1000, jsonUtf8.CurrentDepth);
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray("name"));
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            for (int i = 0; i < 999; i++)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                jsonUtf8.WriteStartObject(Encoding.UTF8.GetBytes("name"));
+                jsonUtf8.WriteStartObject();
+                for (int i = 0; i < 999; i++)
+                {
+                    jsonUtf8.WriteStartObject(Encoding.UTF8.GetBytes("name"));
+                }
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray(Encoding.UTF8.GetBytes("name")));
             }
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray(Encoding.UTF8.GetBytes("name")));
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            for (int i = 0; i < 999; i++)
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
             {
-                jsonUtf8.WriteStartObject(JsonEncodedText.Encode("name"));
+                jsonUtf8.WriteStartObject();
+                for (int i = 0; i < 999; i++)
+                {
+                    jsonUtf8.WriteStartObject(JsonEncodedText.Encode("name"));
+                }
+                Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray(JsonEncodedText.Encode("name")));
             }
-            Assert.Throws<InvalidOperationException>(() => jsonUtf8.WriteStartArray(JsonEncodedText.Encode("name")));
         }
 
         // NOTE: WritingTooLargeProperty test is constrained to run on Windows and MacOSX because it causes
@@ -1978,13 +2126,17 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStartArray(keyChars));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStartArray(keyChars));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStartArray(key));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStartArray(key));
+            }
         }
 
         // NOTE: WritingTooLargePropertyStandalone test is constrained to run on Windows and MacOSX because it causes
@@ -2019,13 +2171,17 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WritePropertyName(keyChars));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WritePropertyName(keyChars));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WritePropertyName(key));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WritePropertyName(key));
+            }
         }
 
         [ConditionalTheory(nameof(IsX64))]
@@ -2052,24 +2208,34 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64StringValue(value));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64StringValue(value));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String("foo", value));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String("foo", value));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String(Encoding.UTF8.GetBytes("foo"), value));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String(Encoding.UTF8.GetBytes("foo"), value));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String("foo".AsSpan(), value));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String("foo".AsSpan(), value));
+            }
 
-            jsonUtf8 = new Utf8JsonWriter(output, options);
-            jsonUtf8.WriteStartObject();
-            Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String(JsonEncodedText.Encode("foo"), value));
+            using (var jsonUtf8 = new Utf8JsonWriter(output, options))
+            {
+                jsonUtf8.WriteStartObject();
+                Assert.Throws<ArgumentException>(() => jsonUtf8.WriteBase64String(JsonEncodedText.Encode("foo"), value));
+            }
         }
 
         [Theory]
@@ -2084,13 +2250,13 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(1024);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteNumberValue(123456789012345);
 
             jsonUtf8.Flush();
 
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -2115,7 +2281,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 32; i++)
             {
                 var output = new ArrayBufferWriter<byte>(32);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -2286,7 +2452,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -2314,7 +2480,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 32; i++)
             {
                 var output = new ArrayBufferWriter<byte>(32);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -2485,7 +2651,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
 
             // Verify that escaping does not change the input strings/spans.
@@ -2507,7 +2673,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(10);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartObject();
 
@@ -2582,7 +2748,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 4; i++)
             {
                 var output = new ArrayBufferWriter<byte>(32);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -2609,7 +2775,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
 
             // Verify that escaping does not change the input strings/spans.
@@ -2629,7 +2795,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(10);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartObject();
 
@@ -2685,7 +2851,7 @@ namespace System.Text.Json.Tests
         {
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(10);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartObject();
 
@@ -2781,7 +2947,7 @@ namespace System.Text.Json.Tests
 
             jsonUtf8.Flush();
 
-            AssertContents("\"++++\"", output);
+            JsonTestHelper.AssertContents("\"++++\"", output);
         }
 
         [Fact]
@@ -2811,7 +2977,7 @@ namespace System.Text.Json.Tests
                 builder.Append("ZGRk");
             }
             builder.Append("ZGQ=\"");
-            AssertContents(builder.ToString(), output);
+            JsonTestHelper.AssertContents(builder.ToString(), output);
         }
 
         [Theory]
@@ -2824,7 +2990,7 @@ namespace System.Text.Json.Tests
             {
                 var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
                 var output = new ArrayBufferWriter<byte>(10);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteEndObject();
@@ -2845,7 +3011,7 @@ namespace System.Text.Json.Tests
             {
                 var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
                 var output = new ArrayBufferWriter<byte>(10);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteEndObject();
@@ -2884,7 +3050,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(32);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartArray();
 
@@ -2911,7 +3077,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndArray();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -2941,7 +3107,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(32);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             string comment = "comment is */ invalid";
 
@@ -2960,7 +3126,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(32);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             string comment = "comment is * / valid";
             jsonUtf8.WriteCommentValue(comment);
@@ -2983,7 +3149,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteCommentValue(invalidUtf8);
 
             string expectedStr = GetCommentExpectedString(prettyPrint: formatted);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         private static string GetCommentExpectedString(bool prettyPrint)
@@ -3031,7 +3197,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartArray();
 
@@ -3054,7 +3220,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndArray();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3094,7 +3260,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 32; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3217,7 +3383,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3252,7 +3418,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 6; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3284,7 +3450,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3315,7 +3481,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 6; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3347,7 +3513,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3369,7 +3535,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 6; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3401,7 +3567,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3441,7 +3607,7 @@ namespace System.Text.Json.Tests
             string WriteProperty(byte[] propertyName, byte[] value)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteString(propertyName, value);
                 jsonUtf8.WriteEndObject();
@@ -3480,7 +3646,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>(1024);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             var validUtf16 = new char[2] { (char)0xD801, (char)0xDC37 }; // 0x10437
             var invalidUtf16 = new char[2] { (char)0xD801, 'a' };
@@ -3547,7 +3713,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 6; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3599,7 +3765,7 @@ namespace System.Text.Json.Tests
         {
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(10);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartObject();
 
@@ -3611,7 +3777,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteEndObject();
             jsonUtf8.Flush();
 
-            AssertContents(GetCustomExpectedString(formatted), output);
+            JsonTestHelper.AssertContents(GetCustomExpectedString(formatted), output);
         }
 
         [Theory]
@@ -3626,7 +3792,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
             var output = new ArrayBufferWriter<byte>(1024);
 
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartArray();
             jsonUtf8.WriteStartObject();
@@ -3634,7 +3800,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteEndArray();
             jsonUtf8.Flush();
 
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -3648,13 +3814,13 @@ namespace System.Text.Json.Tests
                 var options = new JsonWriterOptions { Indented = formatted, SkipValidation = true };
                 var output = new ArrayBufferWriter<byte>(1024);
 
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartArray();
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
 
             {
@@ -3663,13 +3829,13 @@ namespace System.Text.Json.Tests
                 var options = new JsonWriterOptions { Indented = formatted, SkipValidation = true };
                 var output = new ArrayBufferWriter<byte>(1024);
 
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
                 jsonUtf8.WriteEndArray();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3687,7 +3853,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3708,7 +3874,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3737,7 +3903,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3758,7 +3924,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3776,7 +3942,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3797,7 +3963,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3826,7 +3992,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3847,7 +4013,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3866,7 +4032,7 @@ namespace System.Text.Json.Tests
             {
                 var output = new ArrayBufferWriter<byte>(1024);
 
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteStartObject();
 
                 switch (i)
@@ -3886,7 +4052,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3924,7 +4090,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 4; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -3955,7 +4121,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -3981,7 +4147,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 4; i++)
             {
                 var output = new ArrayBufferWriter<byte>(16);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -4015,7 +4181,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4053,7 +4219,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 4; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -4077,7 +4243,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4229,7 +4395,7 @@ namespace System.Text.Json.Tests
             for (int j = 0; j < 3; j++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 ReadOnlySpan<char> keyUtf16 = keyString.AsSpan();
                 ReadOnlySpan<byte> keyUtf8 = Encoding.UTF8.GetBytes(keyString);
@@ -4304,7 +4470,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.Flush();
 
                 // TODO: The output doesn't match what JSON.NET does (different rounding/e-notation).
-                // AssertContents(expectedStr, output);
+                // JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4318,7 +4484,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4333,7 +4499,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4346,7 +4512,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4361,7 +4527,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4374,7 +4540,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4389,7 +4555,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4402,7 +4568,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4417,7 +4583,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4430,7 +4596,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4445,7 +4611,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4458,7 +4624,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4473,7 +4639,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
         }
 
         [Theory]
@@ -4486,7 +4652,7 @@ namespace System.Text.Json.Tests
             var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
 
             var output = new ArrayBufferWriter<byte>();
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             int numberOfElements = 0;
             jsonUtf8.WriteStartArray();
@@ -4501,7 +4667,82 @@ namespace System.Text.Json.Tests
             jsonUtf8.Flush();
 
             string expectedStr = GetNumbersExpectedString(formatted, numberOfElements);
-            AssertContents(expectedStr, output);
+            JsonTestHelper.AssertContents(expectedStr, output);
+        }
+
+        [Theory]
+        [InlineData(true, true, "message", true)]
+        [InlineData(true, false, "message", true)]
+        [InlineData(false, true, "message", true)]
+        [InlineData(false, false, "message", true)]
+        [InlineData(true, true, "mess><age", false)]
+        [InlineData(true, false, "mess><age", false)]
+        [InlineData(false, true, "mess><age", false)]
+        [InlineData(false, false, "mess><age", false)]
+        [InlineData(true, true, ">><++>>>\">>\\>>&>>>\u6f22\u5B57>>>", false)] // \u6f22 = æ±‰, \u5B57 = å­—
+        [InlineData(true, false, ">><++>>>\">>\\>>&>>>\u6f22\u5B57>>>", false)]
+        [InlineData(false, true, ">><++>>>\">>\\>>&>>>\u6f22\u5B57>>>", false)]
+        [InlineData(false, false, ">><++>>>\">>\\>>&>>>\u6f22\u5B57>>>", false)]
+        [InlineData(true, true, "mess\r\nage\u0008\u0001!", true)]
+        [InlineData(true, false, "mess\r\nage\u0008\u0001!", true)]
+        [InlineData(false, true, "mess\r\nage\u0008\u0001!", true)]
+        [InlineData(false, false, "mess\r\nage\u0008\u0001!", true)]
+        public void WriteStringsWithRelaxedEscaping(bool formatted, bool skipValidation, string keyString, bool matchesRelaxedEscaping)
+        {
+            string expectedStr = GetExpectedString_RelaxedEscaping(prettyPrint: formatted, keyString);
+
+            var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
+            WriteStringHelper(options, keyString, expectedStr, shouldMatch: true);
+
+            // Default encoder
+            options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
+            WriteStringHelper(options, keyString, expectedStr, matchesRelaxedEscaping);
+        }
+
+        private static void WriteStringHelper(JsonWriterOptions options, string keyString, string expectedStr, bool shouldMatch)
+        {
+            ReadOnlySpan<char> keyUtf16 = keyString.AsSpan();
+            ReadOnlySpan<byte> keyUtf8 = Encoding.UTF8.GetBytes(keyString);
+
+            for (int i = 0; i < 3; i++)
+            {
+                var output = new ArrayBufferWriter<byte>(1024);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
+
+                jsonUtf8.WriteStartObject();
+
+                switch (i)
+                {
+                    case 0:
+                        jsonUtf8.WriteString(keyString, keyString);
+                        jsonUtf8.WriteStartArray(keyString);
+                        break;
+                    case 1:
+                        jsonUtf8.WriteString(keyUtf16, keyString);
+                        jsonUtf8.WriteStartArray(keyUtf16);
+                        break;
+                    case 2:
+                        jsonUtf8.WriteString(keyUtf8, keyString);
+                        jsonUtf8.WriteStartArray(keyUtf8);
+                        break;
+                }
+
+                jsonUtf8.WriteStringValue(keyString);
+                jsonUtf8.WriteStringValue(keyString);
+                jsonUtf8.WriteEndArray();
+
+                jsonUtf8.WriteEndObject();
+                jsonUtf8.Flush();
+
+                if (shouldMatch)
+                {
+                    JsonTestHelper.AssertContents(expectedStr, output);
+                }
+                else
+                {
+                    JsonTestHelper.AssertContentsNotEqual(expectedStr, output);
+                }
+            }
         }
 
         [Theory]
@@ -4537,7 +4778,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -4567,7 +4808,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4606,7 +4847,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -4636,7 +4877,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4675,7 +4916,7 @@ namespace System.Text.Json.Tests
             for (int i = 0; i < 3; i++)
             {
                 var output = new ArrayBufferWriter<byte>(1024);
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
                 jsonUtf8.WriteStartObject();
 
@@ -4705,7 +4946,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteEndObject();
                 jsonUtf8.Flush();
 
-                AssertContents(expectedStr, output);
+                JsonTestHelper.AssertContents(expectedStr, output);
             }
         }
 
@@ -4742,14 +4983,14 @@ namespace System.Text.Json.Tests
             var output = new ArrayBufferWriter<byte>(1024);
 
             {
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteStartObject();
                 Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(key, DateTime.Now));
                 Assert.Equal(0, output.WrittenCount);
             }
 
             {
-                var jsonUtf8 = new Utf8JsonWriter(output, options);
+                using var jsonUtf8 = new Utf8JsonWriter(output, options);
                 jsonUtf8.WriteStartArray();
                 Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStringValue(value));
                 Assert.Equal(0, output.WrittenCount);
@@ -4833,7 +5074,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteStringValue(DateTime.ParseExact(testStr, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind));
             jsonUtf8.Flush();
 
-            AssertContents($"\"{expectedStr}\"", output);
+            JsonTestHelper.AssertContents($"\"{expectedStr}\"", output);
         }
 
         [Theory]
@@ -4846,7 +5087,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteStringValue(DateTimeOffset.ParseExact(testStr, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind));
             jsonUtf8.Flush();
 
-            AssertContents($"\"{expectedStr}\"", output);
+            JsonTestHelper.AssertContents($"\"{expectedStr}\"", output);
         }
 
         [Fact]
@@ -4860,7 +5101,7 @@ namespace System.Text.Json.Tests
         {
             // Resizing is too slow, even for outerloop tests, so initialize to a large output size up front.
             var output = new ArrayBufferWriter<byte>(noThrow ? 40_000_000 : 1024);
-            var jsonUtf8 = new Utf8JsonWriter(output, options);
+            using var jsonUtf8 = new Utf8JsonWriter(output, options);
 
             jsonUtf8.WriteStartObject();
 
@@ -4907,7 +5148,7 @@ namespace System.Text.Json.Tests
             jsonUtf8.WriteStringValue(text);
             jsonUtf8.Flush();
 
-            AssertContents($"{expectedMessage}", output);
+            JsonTestHelper.AssertContents($"{expectedMessage}", output);
         }
 
         [Theory]
@@ -4934,7 +5175,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteStringValue(text);
                 jsonUtf8.Flush();
 
-                AssertContents(expectedMessage, output);
+                JsonTestHelper.AssertContents(expectedMessage, output);
             }
             {
                 var message = new string('>', stringLength);
@@ -4953,7 +5194,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteStringValue(text);
                 jsonUtf8.Flush();
 
-                AssertContents(expectedMessage, output);
+                JsonTestHelper.AssertContents(expectedMessage, output);
             }
         }
 
@@ -4981,7 +5222,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteStartObject(text);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:{{", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:{{", output);
             }
 
             {
@@ -4991,7 +5232,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteStartArray(text);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:[", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:[", output);
             }
         }
 
@@ -5019,7 +5260,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteBoolean(text, true);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:true", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:true", output);
             }
 
             {
@@ -5029,7 +5270,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteBoolean(text, false);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:false", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:false", output);
             }
 
             {
@@ -5039,7 +5280,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNull(text);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:null", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:null", output);
             }
         }
 
@@ -5068,7 +5309,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5079,7 +5320,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5090,7 +5331,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5101,7 +5342,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5112,7 +5353,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5123,7 +5364,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
 
             {
@@ -5134,7 +5375,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteNumber(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:1", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:1", output);
             }
         }
 
@@ -5163,7 +5404,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteString(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:\"{value.ToString("yyyy-MM-ddTHH:mm:ss")}\"", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:\"{value.ToString("yyyy-MM-ddTHH:mm:ss")}\"", output);
             }
 
             {
@@ -5174,7 +5415,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteString(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:\"{value.ToString("yyyy-MM-ddTHH:mm:ssK")}\"", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:\"{value.ToString("yyyy-MM-ddTHH:mm:ssK")}\"", output);
             }
 
             {
@@ -5185,7 +5426,7 @@ namespace System.Text.Json.Tests
                 jsonUtf8.WriteString(text, value);
                 jsonUtf8.Flush();
 
-                AssertContents($"{{{expectedMessage}:\"{value.ToString()}\"", output);
+                JsonTestHelper.AssertContents($"{{{expectedMessage}:\"{value.ToString()}\"", output);
             }
         }
 
@@ -5526,7 +5767,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents("[/**//**/]", output);
+            JsonTestHelper.AssertContents("[/**//**/]", output);
         }
 
         [Fact]
@@ -5623,7 +5864,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents("{}", output);
+            JsonTestHelper.AssertContents("{}", output);
             output.Clear();
 
             using (var writer = new Utf8JsonWriter(output))
@@ -5639,7 +5880,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents($"{{\"\":{wireValue},\"\":{wireValue}}}", output);
+            JsonTestHelper.AssertContents($"{{\"\":{wireValue},\"\":{wireValue}}}", output);
         }
 
         private static void WriteNullPropertyName_NoValue(
@@ -5664,7 +5905,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents("{}", output);
+            JsonTestHelper.AssertContents("{}", output);
             output.Clear();
 
             using (var writer = new Utf8JsonWriter(output))
@@ -5682,7 +5923,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents($"{{\"\":{wireValue},\"\":{wireValue}}}", output);
+            JsonTestHelper.AssertContents($"{{\"\":{wireValue},\"\":{wireValue}}}", output);
         }
 
         private static void WriteNullValue_InObject(
@@ -5710,7 +5951,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents($"{{{nullValue},{wireValue},{wireValue}}}", output);
+            JsonTestHelper.AssertContents($"{{{nullValue},{wireValue},{wireValue}}}", output);
         }
 
         private static void WriteNullValue_InArray(
@@ -5738,7 +5979,7 @@ namespace System.Text.Json.Tests
                 writer.Flush();
             }
 
-            AssertContents($"[{nullValue},{wireValue},{wireValue}]", output);
+            JsonTestHelper.AssertContents($"[{nullValue},{wireValue},{wireValue}]", output);
         }
 
         private static string GetHelloWorldExpectedString(bool prettyPrint, string propertyName, string value)
@@ -6144,6 +6385,34 @@ namespace System.Text.Json.Tests
             return Encoding.UTF8.GetString(ms.ToArray());
         }
 
+        private static string GetExpectedString_RelaxedEscaping(bool prettyPrint, string keyString)
+        {
+            var ms = new MemoryStream();
+            TextWriter streamWriter = new StreamWriter(ms, new UTF8Encoding(false), 1024, true);
+
+            var json = new JsonTextWriter(streamWriter)
+            {
+                Formatting = prettyPrint ? Formatting.Indented : Formatting.None,
+            };
+
+            json.WriteStartObject();
+
+            json.WritePropertyName(keyString, escape: true);
+            json.WriteValue(keyString);
+
+            json.WritePropertyName(keyString, escape: true);
+            json.WriteStartArray();
+            json.WriteValue(keyString);
+            json.WriteValue(keyString);
+            json.WriteEnd();
+
+            json.WriteEnd();
+
+            json.Flush();
+
+            return Encoding.UTF8.GetString(ms.ToArray());
+        }
+
         private static string GetGuidsExpectedString(bool prettyPrint, string keyString, Guid[] guids, bool escape = false)
         {
             var ms = new MemoryStream();
@@ -6262,19 +6531,6 @@ namespace System.Text.Json.Tests
             return Encoding.UTF8.GetString(ms.ToArray());
         }
 
-        private static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
-        {
-            string value = Encoding.UTF8.GetString(
-                    buffer.WrittenSpan
-#if netfx
-                        .ToArray()
-#endif
-                    );
-
-            // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
-            Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
-        }
-
         public static IEnumerable<object[]> JsonEncodedTextStrings
         {
             get