Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaël Catanzariti committed Oct 18, 2020
2 parents 32676e8 + 7bdc761 commit 54863c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Dahomey.Json.Tests/JsonIgnoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ private void ProcessDefaultValue<T>(MemberInfo memberInfo, MemberMapping<T> 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);
}
Expand Down

0 comments on commit 54863c2

Please sign in to comment.