Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review feedback
  • Loading branch information
layomia committed May 14, 2020
commit 4f734727f0ffa68387b3b843ac4d0dadada19cfd
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
namespace System.Text.Json.Serialization
{
/// <summary>
/// When specified on <see cref="JsonSerializerOptions.DefaultIgnoreCondition"/>, controls whether properties with default values are ignored during serialization.
/// When specified on <see cref="JsonIgnoreAttribute.Condition"/>, controls whether a property is ignored during serialization and deserialization.
/// When specified on <see cref="JsonSerializerOptions.DefaultIgnoreCondition"/>,
/// <see cref="WhenWritingDefault"/> specifies that properties with default values are ignored during serialization.
/// When specified on <see cref="JsonIgnoreAttribute.Condition"/>, controls whether
/// a property is ignored during serialization and deserialization.
/// </summary>
public enum JsonIgnoreCondition
{
/// <summary>
/// Property will always be serialized and deserialized.
/// Property is never ignored during serialization or deserialization.
/// </summary>
Never = 0,
/// <summary>
/// Property will never be serialized or deserialized.
/// Property is always ignored during serialization and deserialization.
/// </summary>
Always = 1,
/// <summary>
/// Property will not be serialized if it is the default value.
/// If the value is the default, the property is ignored during serialization.
/// </summary>
WhenWritingDefault = 2,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,40 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp

public override bool HandleNull => true;
}

[Fact]
public static void SetterCalledWhenConverterReturnsNull()
{
var options = new JsonSerializerOptions
{
IgnoreNullValues = true,
Converters = { new UriToNullConverter() }
};

// Baseline - null values ignored, converter is not called.
string json = @"{""MyUri"":null}";

ClassWithInitializedUri obj = JsonSerializer.Deserialize<ClassWithInitializedUri>(json, options);
Assert.Equal(new Uri("https://microsoft.com"), obj.MyUri);

// Baseline - setter is called if payload is not null and converter returns null.
json = @"{""MyUri"":""https://default""}";
obj = JsonSerializer.Deserialize<ClassWithInitializedUri>(json, options);
Assert.Null(obj.MyUri);
}

private class ClassWithInitializedUri
{
public Uri MyUri { get; set; } = new Uri("https://microsoft.com");
}

public class UriToNullConverter : JsonConverter<Uri>
{
public override Uri Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => null;

public override void Write(Utf8JsonWriter writer, Uri value, JsonSerializerOptions options) => throw new NotImplementedException();

public override bool HandleNull => true;
}
}
}