From 0cdd423fbe396275d8faaca0c2fee8b6b6e23674 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Mon, 21 Jun 2021 15:08:27 -0700 Subject: [PATCH] Add code to flush JSON writer after root-level fast-path serialization (#54502) --- .../Text/Json/Serialization/JsonSerializer.Write.Helpers.cs | 1 + .../SerializationLogicTests.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs index 6083612..cd6cd1b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs @@ -43,6 +43,7 @@ namespace System.Text.Json typedInfo.Options._context?.CanUseSerializationLogic == true) { typedInfo.Serialize(writer, value); + writer.Flush(); } else { diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationLogicTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationLogicTests.cs index 53474be..daa3a4c 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationLogicTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationLogicTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.IO; using System.Text.Encodings.Web; using System.Text.Json.Serialization; using Xunit; @@ -111,5 +112,16 @@ namespace System.Text.Json.SourceGeneration.Tests yield return new object[] { new JsonSerializerOptions(s_compatibleOptions) { DefaultIgnoreCondition = JsonIgnoreCondition.Never } }; yield return new object[] { new JsonSerializerOptions(s_compatibleOptions) { IgnoreReadOnlyFields = true } }; } + + [Fact] + public static void WriterIsFlushedAtRootCall() + { + using MemoryStream ms = new(); + using Utf8JsonWriter writer = new(ms); + + JsonSerializer.Serialize(writer, new HighLowTemps(), SerializationContext.Default.HighLowTemps); + Assert.Equal(18, writer.BytesCommitted); + Assert.Equal(0, writer.BytesPending); + } } } -- 2.7.4