Adds support for nullable structs (#203)
This commit is contained in:
parent
259fdb5ae8
commit
c4e7f47ada
3 changed files with 38 additions and 0 deletions
|
|
@ -0,0 +1,19 @@
|
|||
namespace System.Text.Json.Serialization
|
||||
{
|
||||
public class NullableStructSerializerFactory : JsonConverterFactory
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
if (!typeToConvert.IsGenericType || typeToConvert.GetGenericTypeDefinition() != typeof(Nullable<>))
|
||||
return false;
|
||||
|
||||
var structType = typeToConvert.GenericTypeArguments[0];
|
||||
return !structType.IsPrimitive && structType.Namespace?.StartsWith(nameof(System)) != true && !structType.IsEnum;
|
||||
}
|
||||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
(JsonConverter)Activator.CreateInstance(
|
||||
typeof(NullableStructSerializer<>).MakeGenericType(typeToConvert.GenericTypeArguments[0])
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue