Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(valueTypeToConvert);
}

JsonConverter converter = (JsonConverter)Activator.CreateInstance(
return CreateValueConverter(valueTypeToConvert, valueConverter);
}

public static JsonConverter CreateValueConverter(Type valueTypeToConvert, JsonConverter valueConverter)
{
return (JsonConverter)Activator.CreateInstance(
typeof(JsonValueConverterNullable<>).MakeGenericType(valueTypeToConvert),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: new object[] { valueConverter },
culture: null)!;

return converter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ private JsonConverter GetConverterFromAttribute(JsonConverterAttribute converter
Debug.Assert(converter != null);
if (!converter.CanConvert(typeToConvert))
{
Type? underlyingType = Nullable.GetUnderlyingType(typeToConvert);
if (underlyingType != null)
{
// Allow nullable handling to forward to the underlying type's converter.
return JsonValueConverterNullableFactory.CreateValueConverter(underlyingType, converter);
}

ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, propertyInfo, typeToConvert);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptio
}
}

private class Int32Class
{
[JsonConverter(typeof(Int32Converter))]
public int? MyInt { get; set; }
}

[Fact]
public static void OverrideDefaultConverter()
{
Expand All @@ -52,6 +58,33 @@ public static void OverrideDefaultConverter()
int myInt = JsonSerializer.Deserialize<int>(@"""1""", options);
Assert.Equal(1, myInt);
}

{
int? myInt = JsonSerializer.Deserialize<int?>("null", options);
Assert.False(myInt.HasValue);
}

{
int? myInt = JsonSerializer.Deserialize<int?>("1", options);
Assert.Equal(1, myInt.Value);
}

{
Int32Class myIntClass = JsonSerializer.Deserialize<Int32Class>(@"{""MyInt"":null}");
Assert.False(myIntClass.MyInt.HasValue);
}

{
Int32Class myIntClass = JsonSerializer.Deserialize<Int32Class>(@"{""MyInt"":1}");
Assert.True(myIntClass.MyInt.HasValue);
Assert.Equal(1, myIntClass.MyInt.Value);
}

{
Int32Class myIntClass = JsonSerializer.Deserialize<Int32Class>(@"{""MyInt"":""1""}");
Assert.True(myIntClass.MyInt.HasValue);
Assert.Equal(1, myIntClass.MyInt.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,65 @@ public static void ConverterForClassThatCanBeNullDependingOnContent()
obj = JsonSerializer.Deserialize<ClassThatCanBeNullDependingOnContent>(@"{""MyInt"":0}");
Assert.Null(obj);
}

private class JsonTestStructConverter : JsonConverter<TestStruct>
{
public override TestStruct Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new TestStruct
{
InnerValue = reader.GetInt32()
};
}

public override void Write(Utf8JsonWriter writer, TestStruct value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.InnerValue);
}
}

private struct TestStruct
{
public int InnerValue { get; set; }
}

private class TestStructClass
{
[JsonConverter(typeof(JsonTestStructConverter))]
public TestStruct? MyStruct { get; set; }
}

[Fact]
public static void NullableCustomValueType()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonTestStructConverter());

{
TestStruct myStruct = JsonSerializer.Deserialize<TestStruct>("1", options);
Assert.Equal(1, myStruct.InnerValue);
}

{
TestStruct? myStruct = JsonSerializer.Deserialize<TestStruct?>("null", options);
Assert.False(myStruct.HasValue);
}

{
TestStruct? myStruct = JsonSerializer.Deserialize<TestStruct?>("1", options);
Assert.Equal(1, myStruct.Value.InnerValue);
}

{
TestStructClass myStructClass = JsonSerializer.Deserialize<TestStructClass>(@"{""MyStruct"":null}");
Assert.False(myStructClass.MyStruct.HasValue);
}

{
TestStructClass myStructClass = JsonSerializer.Deserialize<TestStructClass>(@"{""MyStruct"":1}");
Assert.True(myStructClass.MyStruct.HasValue);
Assert.Equal(1, myStructClass.MyStruct.Value.InnerValue);
}
}
}
}