From 7d46b3008a5a4002fd12a13b2f94a83a02975e76 Mon Sep 17 00:00:00 2001 From: gnovack <50467879+gnovack@users.noreply.github.com> Date: Thu, 16 May 2019 19:35:08 -0400 Subject: [PATCH] Fixed deserialization of null arrays for issue dotnet/corefx#37606 (dotnet/corefx#37616) * Fixed deserialization of null root arrays. * Used reader.CurrentDepth to catch root level nulls. Commit migrated from https://github.com/dotnet/corefx/commit/026fa6a6889ab1c9c8f14f55d414d2940fc59805 --- .../JsonSerializer.Read.HandleNull.cs | 4 +-- .../tests/Serialization/Null.ReadTests.cs | 38 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs index 80abfc4..7f7ecb1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs @@ -16,8 +16,8 @@ namespace System.Text.Json.Serialization return false; } - // If we don't have a valid property, that means we read "null" for a root object so just return. - if (state.Current.JsonPropertyInfo == null) + // If null is read at the top level and the type is nullable, then the root is "null" so just return. + if (reader.CurrentDepth == 0 && (state.Current.JsonPropertyInfo == null || state.Current.JsonPropertyInfo.CanBeNull)) { Debug.Assert(state.IsLastFrame); Debug.Assert(state.Current.ReturnValue == null); diff --git a/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs index ce12143..34f6cae 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs @@ -2,6 +2,7 @@ // 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.Collections.Generic; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -17,7 +18,7 @@ namespace System.Text.Json.Serialization.Tests [Fact] public static void RootObjectIsNull() - { + { { TestClassWithNull obj = JsonSerializer.Parse("null"); Assert.Null(obj); @@ -27,6 +28,41 @@ namespace System.Text.Json.Serialization.Tests object obj = JsonSerializer.Parse("null"); Assert.Null(obj); } + + { + string obj = JsonSerializer.Parse("null"); + Assert.Null(obj); + } + + { + IEnumerable obj = JsonSerializer.Parse>("null"); + Assert.Null(obj); + } + + { + Dictionary obj = JsonSerializer.Parse>("null"); + Assert.Null(obj); + } + + } + + [Fact] + public static void RootArrayIsNull() + { + { + int[] obj = JsonSerializer.Parse("null"); + Assert.Null(obj); + } + + { + object[] obj = JsonSerializer.Parse("null"); + Assert.Null(obj); + } + + { + TestClassWithNull[] obj = JsonSerializer.Parse("null"); + Assert.Null(obj); + } } [Fact] -- 2.7.4