Ignore extension property if null. (dotnet/corefx#40431)
authorCodeBlanch <mblanchard@macrosssoftware.com>
Wed, 21 Aug 2019 22:09:46 +0000 (15:09 -0700)
committerSteve Harter <steveharter@users.noreply.github.com>
Wed, 21 Aug 2019 22:09:46 +0000 (17:09 -0500)
Commit migrated from https://github.com/dotnet/corefx/commit/41cd99d051102be4ed83f4f9105ae9e73aa48b7c

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleDictionary.cs
src/libraries/System.Text.Json/tests/Serialization/ExtensionDataTests.cs

index 75e9e64..6dc9427 100644 (file)
@@ -26,8 +26,9 @@ namespace System.Text.Json
                 if (enumerable == null)
                 {
                     // If applicable, we only want to ignore object properties.
-                    if (state.Current.JsonClassInfo.ClassType != ClassType.Object ||
-                        !state.Current.JsonPropertyInfo.IgnoreNullValues)
+                    if (state.Current.ExtensionDataStatus != ExtensionDataWriteStatus.Writing &&
+                        (state.Current.JsonClassInfo.ClassType != ClassType.Object ||
+                        !state.Current.JsonPropertyInfo.IgnoreNullValues))
                     {
                         // Write a null object or enumerable.
                         state.Current.WriteObjectOrArrayStart(ClassType.Dictionary, writer, writeNull: true);
index cd847c5..72c0a26 100644 (file)
@@ -54,6 +54,14 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
+        public static void ExtensionPropertyIgnoredWhenNull()
+        {
+            string expected = @"{}";
+            string actual = JsonSerializer.Serialize(new ClassWithExtensionPropertyAsObject());
+            Assert.Equal(expected, actual);
+        }
+
+        [Fact]
         public static void ExtensionPropertyAlreadyInstantiated()
         {
             Assert.NotNull(new ClassWithExtensionPropertyAlreadyInstantiated().MyOverflow);