From 02eaf52d17d82560195ce53e6165f0fb03a44d59 Mon Sep 17 00:00:00 2001 From: gnovack Date: Sat, 11 May 2019 13:11:34 -0400 Subject: [PATCH 1/2] Fixed deserialization of null root arrays. --- .../JsonSerializer.Read.HandleArray.cs | 4 ++-- .../tests/Serialization/Null.ReadTests.cs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs index a19e02f4f2b8..d6773866a9fe 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs +++ b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs @@ -170,7 +170,7 @@ internal static void ApplyObjectToEnumerable( { state.Current.TempEnumerableValues.Add(value); } - else + else if (state.Current.ReturnValue != null) { ((IList)state.Current.ReturnValue).Add(value); } @@ -225,7 +225,7 @@ internal static void ApplyValueToEnumerable( { ((IList)state.Current.TempEnumerableValues).Add(value); } - else + else if (state.Current.ReturnValue != null) { ((IList)state.Current.ReturnValue).Add(value); } diff --git a/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs b/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs index c61dab68fb67..1f483d12decd 100644 --- a/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs +++ b/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs @@ -29,6 +29,25 @@ public static void RootObjectIsNull() } } + [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] public static void DefaultIgnoreNullValuesOnRead() { From f4b22b953bff6bc0c000fe9f1f4ea5975d2abc35 Mon Sep 17 00:00:00 2001 From: gnovack Date: Tue, 14 May 2019 22:05:05 -0400 Subject: [PATCH 2/2] Used reader.CurrentDepth to catch root level nulls. --- .../JsonSerializer.Read.HandleArray.cs | 4 ++-- .../JsonSerializer.Read.HandleNull.cs | 4 ++-- .../tests/Serialization/Null.ReadTests.cs | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs index d6773866a9fe..a19e02f4f2b8 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs +++ b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs @@ -170,7 +170,7 @@ internal static void ApplyObjectToEnumerable( { state.Current.TempEnumerableValues.Add(value); } - else if (state.Current.ReturnValue != null) + else { ((IList)state.Current.ReturnValue).Add(value); } @@ -225,7 +225,7 @@ internal static void ApplyValueToEnumerable( { ((IList)state.Current.TempEnumerableValues).Add(value); } - else if (state.Current.ReturnValue != null) + else { ((IList)state.Current.ReturnValue).Add(value); } diff --git a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs index 80abfc4a631c..7f7ecb1ef8b0 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs +++ b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleNull.cs @@ -16,8 +16,8 @@ private static bool HandleNull(ref Utf8JsonReader reader, ref ReadStack state, J 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/System.Text.Json/tests/Serialization/Null.ReadTests.cs b/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs index 1f483d12decd..73b87ad2e640 100644 --- a/src/System.Text.Json/tests/Serialization/Null.ReadTests.cs +++ b/src/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 @@ public static void ClassWithNullProperty() [Fact] public static void RootObjectIsNull() - { + { { TestClassWithNull obj = JsonSerializer.Parse("null"); Assert.Null(obj); @@ -27,6 +28,22 @@ public static void RootObjectIsNull() 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]