Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -189,7 +189,7 @@ internal bool HasConverter(Type typeToConvert)
return GetConverter(typeToConvert) != null;
}

private JsonConverter GetConverterFromAttribute(JsonConverterAttribute converterAttribute, Type typeToConvert, Type classTypeAttributeIsOn, PropertyInfo? propertyInfo)
private JsonConverter? GetConverterFromAttribute(JsonConverterAttribute converterAttribute, Type typeToConvert, Type classTypeAttributeIsOn, PropertyInfo? propertyInfo)
{
JsonConverter? converter;

Expand Down Expand Up @@ -217,6 +217,8 @@ private JsonConverter GetConverterFromAttribute(JsonConverterAttribute converter
Debug.Assert(converter != null);
if (!converter.CanConvert(typeToConvert))
{
if (Nullable.GetUnderlyingType(typeToConvert) != null)
return null;
ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, propertyInfo, typeToConvert);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,45 @@ public static partial class CustomConverterTests
/// <summary>
/// Allow both string and number values on deserialize.
/// </summary>
private class Int32Converter : JsonConverter<int>
private class Int32Converter : JsonConverterFactory
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override bool CanConvert(Type typeToConvert)
=> typeToConvert == typeof(int);

public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
=> new InternalInt32Converter();

private class InternalInt32Converter : JsonConverter<int>
{
if (reader.TokenType == JsonTokenType.String)
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string stringValue = reader.GetString();
if (int.TryParse(stringValue, out int value))
if (reader.TokenType == JsonTokenType.String)
{
string stringValue = reader.GetString();
if (int.TryParse(stringValue, out int value))
{
return value;
}
}
else if (reader.TokenType == JsonTokenType.Number)
{
return value;
return reader.GetInt32();
}

throw new JsonException();
}
else if (reader.TokenType == JsonTokenType.Number)

public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
return reader.GetInt32();
writer.WriteNumberValue(value);
}

throw new JsonException();
}
}

public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
private class Int32Class
{
[JsonConverter(typeof(Int32Converter))]
public int? MyInt { get; set; }
}

[Fact]
Expand All @@ -52,6 +67,26 @@ 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.Equal(1, myIntClass.MyInt.Value);
}
}
}
}