diff --git a/src/Dahomey.Json.Tests/JsonIgnoreTests.cs b/src/Dahomey.Json.Tests/JsonIgnoreTests.cs index 7e97839..497ac48 100644 --- a/src/Dahomey.Json.Tests/JsonIgnoreTests.cs +++ b/src/Dahomey.Json.Tests/JsonIgnoreTests.cs @@ -68,6 +68,12 @@ public class MyClass [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public object WhenWritingNull { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public int? WhenWritingNullableNull { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public int? WhenWritingNullableValue { get; set; } = 123; } [Fact] @@ -83,7 +89,7 @@ public void TestWritingCondition() WhenWritingNull = null, }; - const string expected = @"{""Never"":0}"; + const string expected = @"{""Never"":0,""WhenWritingNullableValue"":123}"; string actual = JsonSerializer.Serialize(myClass, options); Assert.Equal(expected, actual); diff --git a/src/Dahomey.Json/Serialization/Converters/Mappings/DefaultObjectMappingConvention.cs b/src/Dahomey.Json/Serialization/Converters/Mappings/DefaultObjectMappingConvention.cs index 5775094..415a230 100644 --- a/src/Dahomey.Json/Serialization/Converters/Mappings/DefaultObjectMappingConvention.cs +++ b/src/Dahomey.Json/Serialization/Converters/Mappings/DefaultObjectMappingConvention.cs @@ -206,7 +206,8 @@ private void ProcessDefaultValue(MemberInfo memberInfo, MemberMapping memb if (jsonIgnoreAttribute != null) { if (jsonIgnoreAttribute.Condition == JsonIgnoreCondition.WhenWritingDefault - || jsonIgnoreAttribute.Condition == JsonIgnoreCondition.WhenWritingNull && memberMapping.MemberType.IsClass) + || jsonIgnoreAttribute.Condition == JsonIgnoreCondition.WhenWritingNull + && (memberMapping.MemberType.IsClass || Nullable.GetUnderlyingType(memberMapping.MemberType) != null)) { memberMapping.SetIngoreIfDefault(true); }