[3.0 Bug fix]Fix JsonSerializer.WriteAsync (dotnet/corefx#38308)
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Tue, 11 Jun 2019 04:56:58 +0000 (06:56 +0200)
committerAhson Khan <ahkha@microsoft.com>
Tue, 11 Jun 2019 04:56:58 +0000 (21:56 -0700)
* fix serialization

* create tailored test

* cleanup namespace

* address some feedback

* add poco test

* update test

* simplify loop

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

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.cs
src/libraries/System.Text.Json/tests/Serialization/Stream.WriteTests.cs

index 31d5dad..5d7a5dd 100644 (file)
@@ -18,7 +18,6 @@ namespace System.Text.Json
             JsonSerializerOptions options,
             ref WriteStack state)
         {
-            bool continueWriting = true;
             bool finishedSerializing;
             do
             {
@@ -48,16 +47,17 @@ namespace System.Text.Json
                         break;
                 }
 
-                if (flushThreshold >= 0 && writer.BytesPending > flushThreshold)
+                if (finishedSerializing && writer.CurrentDepth == 0)
                 {
-                    return false;
+                    break;
                 }
 
-                if (finishedSerializing && writer.CurrentDepth == 0)
+                // If serialization is not yet end and we surpass beyond flush threshold return false and flush stream.
+                if (flushThreshold >= 0 && writer.BytesPending > flushThreshold)
                 {
-                    continueWriting = false;
+                    return false;
                 }
-            } while (continueWriting);
+            } while (true);
 
             return true;
         }
index 62bbf85..4d277cd 100644 (file)
@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information.
 
 using System.IO;
-using System.Threading;
 using System.Threading.Tasks;
 using Xunit;
 
@@ -60,6 +59,37 @@ namespace System.Text.Json.Serialization.Tests
             }
         }
 
+        [Fact]
+        public static async Task RoundTripLargeJsonViaJsonElementAsync()
+        {
+            // Generating tailored json
+            int i = 0;
+            StringBuilder json = new StringBuilder();
+            json.Append("{");
+            while (true)
+            {
+                if (json.Length >= 14757)
+                {
+                    break;
+                }
+                json.AppendFormat(@"""Key_{0}"":""{0}"",", i);
+                i++;
+            }
+            json.Remove(json.Length - 1, 1).Append("}");
+
+            JsonElement root = JsonSerializer.Parse<JsonElement>(json.ToString());
+            var ms = new MemoryStream();
+            await JsonSerializer.WriteAsync(ms, root, root.GetType());
+        }
+
+        [Fact]
+        public static async Task RoundTripLargeJsonViaPocoAsync()
+        {
+            byte[] array = JsonSerializer.Parse<byte[]>(JsonSerializer.ToString(new byte[11056]));
+            var ms = new MemoryStream();
+            await JsonSerializer.WriteAsync(ms, array, array.GetType());
+        }
+
         private static async Task WriteAsync(TestStream stream)
         {
             JsonSerializerOptions options = new JsonSerializerOptions