如何使用 System.Text.Json 将所有“Nullable<T>”值类型的空字符串反序列化为空值?

Dab*_*bas 2 c# .net-core system.text.json

在 .Net Core 3.1 和使用System.Text.Json库中,我遇到了 Newtonsoft 库中没有出现的问题。

如果我在 JSON 中为某些类型(后端类型)DateTime?或 的属性发送一个空字符串int?,它会返回 400 状态代码,并带有一条错误消息,表示值无法反序列化。但是,对于 Newtonsoft,空字符串会自动解释为任何值的空值。Nullable<T>.

一个最小的例子是:

var json = "\"\"";

Assert.AreEqual(null, Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime?>(json)); // Passes
Assert.AreEqual(null, System.Text.Json.JsonSerializer.Deserialize<DateTime?>(json));   // Throws System.Text.Json.JsonException: The JSON value could not be converted to System.Nullable`1[System.DateTime].
Run Code Online (Sandbox Code Playgroud)

有没有办法让System.Text.Json行为以同样的方式?演示在这里

dbc*_*dbc 5

您可以使用工厂转换器模式来创建一个JsonConverterFactory导致空字符串被解释null为所有Nullable<T>类型值的。

以下工厂完成这项工作:

public class NullableConverterFactory : JsonConverterFactory
{
    static readonly byte [] Empty = Array.Empty<byte>();

    public override bool CanConvert(Type typeToConvert) => Nullable.GetUnderlyingType(typeToConvert) != null;

    public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => 
        (JsonConverter)Activator.CreateInstance(
            typeof(NullableConverter<>).MakeGenericType(
                new Type[] { Nullable.GetUnderlyingType(type) }),
            BindingFlags.Instance | BindingFlags.Public,
            binder: null,
            args: new object[] { options },
            culture: null);

    class NullableConverter<T> : JsonConverter<T?> where T : struct
    {
        // DO NOT CACHE the return of (JsonConverter<T>)options.GetConverter(typeof(T)) as DoubleConverter.Read() and DoubleConverter.Write()
        // DO NOT WORK for nondefault values of JsonSerializerOptions.NumberHandling which was introduced in .NET 5
        public NullableConverter(JsonSerializerOptions options) {} 

        public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                if (reader.ValueTextEquals(Empty))
                    return null;
            }
            return JsonSerializer.Deserialize<T>(ref reader, options);
        }           

        public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) =>
            JsonSerializer.Serialize(writer, value.Value, options);
    }
}
Run Code Online (Sandbox Code Playgroud)

该工厂应添加到JsonSerializerOptions.Converters您的框架集合中。

笔记:

演示小提琴在这里