Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jul 22, 2024
1 parent e19513f commit e3e4c76
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,31 @@ public async Task JsonIgnoreCondition_LastOneWins()
Assert.Null(obj.MyString);
}

[Fact]
public void JsonIgnoreCondition_WhenWriting()
{
var json = JsonSerializer.Serialize(new JsonIgnoreCondition_WhenReadingWritingTestModel{ Age = 10, Name="Mike" });
Assert.Equal("""{"Age":10}""", json);
}

[Fact]
public void JsonIgnoreCondition_WhenReading()
{
var json = """{"Age":10, "Name":"Mike"}""";
var model = JsonSerializer.Deserialize<JsonIgnoreCondition_WhenReadingWritingTestModel>(json);
Assert.Equal("Mike", model.Name);
Assert.Equal(0, model.Age);
}

public class JsonIgnoreCondition_WhenReadingWritingTestModel
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenReading)]
public int Age { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWriting)]
public string Name { get; set; }
}


[Fact]
public async Task ClassWithComplexObjectsUsingIgnoreWhenWritingDefaultAttribute()
{
Expand Down

0 comments on commit e3e4c76

Please sign in to comment.