当前位置 博文首页 > SpringLeee:使用 C# 9 的records作为强类型ID - JSON序列化

    SpringLeee:使用 C# 9 的records作为强类型ID - JSON序列化

    作者:SpringLeee 时间:2021-01-30 18:05

    在本系列的上一篇文章中,我们注意到强类型ID的实体,序列化为 JSON 的时候报错了,就像这样:

    {
        "id": {
            "value": 1
        },
        "name": "Apple",
        "unitPrice": 0.8
    }
    

    不过想了一下,这样的意外也是在意料之中的,强类型ID是record类型,而不是原始类型,因此将其序列化为一个对象是有意义的,但这显然不是我们想要的……让我们看看如何解决这个问题。

    System.Text.Json

    在最新版本的ASP.NET Core(从3.0)中,默认的JSON序列化程序是System.Text.Json,因此让我首先介绍这种。

    为了将强类型的id序列化为其值而不是对象,我们需要编写一个通用的 JsonConverter:

    public class StronglyTypedIdJsonConverter<TStronglyTypedId, TValue> : JsonConverter<TStronglyTypedId>
        where TStronglyTypedId : StronglyTypedId<TValue>
        where TValue : notnull
    {
        public override TStronglyTypedId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is JsonTokenType.Null)
                return null;
    
            var value = JsonSerializer.Deserialize<TValue>(ref reader, options);
            var factory = StronglyTypedIdHelper.GetFactory<TValue>(typeToConvert);
            return (TStronglyTypedId)factory(value);
        }
    
        public override void Write(Utf8JsonWriter writer, TStronglyTypedId value, JsonSerializerOptions options)
        {
            if (value is null)
                writer.WriteNullValue();
            else
                JsonSerializer.Serialize(writer, value.Value, options);
        }
    }
    

    逻辑很简单,对于直接读取 id.value, 对于反序列化,创建一个强类型id的实例,然后给它赋值。

    然后在启动类中配置:

    services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(
                new StronglyTypedIdJsonConverter<ProductId, int>());
        });
    

    现在拿到了想要的结果:

    {
        "id": 1,
        "name": "Apple",
        "unitPrice": 0.8
    }
    

    真好!不过,还有有一个问题:我们只为添加了一个对于ProductId的转换器,但我不想为每种类型的强类型ID添加另一个转换器!我们想要一个适用于所有强类型id的转换器……,现在可以创建一个转换器工厂(ConverterFactory),就像下边这样:

    public class StronglyTypedIdJsonConverterFactory : JsonConverterFactory
    {
        private static readonly ConcurrentDictionary<Type, JsonConverter> Cache = new();
    
        public override bool CanConvert(Type typeToConvert)
        {
            return StronglyTypedIdHelper.IsStronglyTypedId(typeToConvert);
        }
    
        public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
        {
            return Cache.GetOrAdd(typeToConvert, CreateConverter);
        }
    
        private static JsonConverter CreateConverter(Type typeToConvert)
        {
            if (!StronglyTypedIdHelper.IsStronglyTypedId(typeToConvert, out var valueType))
                throw new InvalidOperationException($"Cannot create converter for '{typeToConvert}'");
    
            var type = typeof(StronglyTypedIdJsonConverter<,>).MakeGenericType(typeToConvert, valueType);
            return (JsonConverter)Activator.CreateInstance(type);
        }
    }
    

    首先我们查看需要转换的类型,检查它是否实际上是强类型的id,然后为该类型创建特定转换器的实例,我们添加了一些缓存,避免每次都进行反射工作。

    现在,我们没有添加特定的JsonConvert,只是添加了一个Factory,然后在启动文件修改,现在,我们的转换器将应用于每个强类型ID

    services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(
                new StronglyTypedIdJsonConverterFactory());
        });
    

    Newtonsoft.Json

    如果您的项目使用的是Newtonsoft.Json进行JSON序列化,那就很简单了。

    当它序列化一个值时,Newtonsoft.Json 查找一个compatible JsonConverter,如果找不到,就查找一个TypeConverter, 如果TypeConverter存在,并且可以将值转换为string,那么它把值序列化为字符串, 因为我们之前定义了 TypeConverter,Newtonsoft.Json查找到了,我得到以下结果:

    {
        "id": "1",
        "name": "Apple",
        "unitPrice": 0.8
    }
    

    几乎是正确的……除了id值不应序列化为字符串,而应序列化为数字,如果id值是GUID或字符串而不是int,那就很好,则需要编写一个自定义转换器。

    它和 System.Text.Json 的转换器非常相似,不同之处在于Newtonsoft.Json没有转换器工厂(ConvertFactory)的概念,相反,我们将编写一个非泛型转换器:

    public class StronglyTypedIdNewtonsoftJsonConverter : JsonConverter
    {
        private static readonly ConcurrentDictionary<Type, JsonConverter> Cache = new();
    
        public override bool CanConvert(Type objectType)
        {
            return StronglyTypedIdHelper.IsStronglyTypedId(objectType);
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var converter = GetConverter(objectType);
            return converter.ReadJson(reader, objectType, existingValue, serializer);
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value is null)
            {
                writer.WriteNull();
            }
            else
            {
                var converter = GetConverter(value.GetType());
                converter.WriteJson(writer, value, serializer);
            }
        }
    
        private static JsonConverter GetConverter(Type objectType)
        {
            return Cache.GetOrAdd(objectType, CreateConverter);
        }
    
        private static JsonConverter CreateConverter(Type objectType)
        {
            if (!StronglyTypedIdHelper.IsStronglyTypedId(objectType, out var valueType))
                throw new InvalidOperationException($"Cannot create converter for '{objectType}'");
    
            var type = typeof(StronglyTypedIdNewtonsoftJsonConverter<,>).MakeGenericType(objectType, valueType);
            return (JsonConverter)Activator.CreateInstance(type);
        }
    }
    
    public class StronglyTypedIdNewtonsoftJsonConverter<TStronglyTypedId, TValue> : JsonConverter<TStronglyTypedId>
        where TStronglyTypedId : StronglyTypedId<TValue>
        where TValue : notnull
    {
        public override TStronglyTypedId ReadJson(JsonReader reader, Type objectType, TStronglyTypedId existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType is JsonToken.Null)
                return null;
    
            var value = serializer.Deserialize<TValue>(reader);
            var factory = StronglyTypedIdHelper.GetFactory<TValue>(objectType);
            return (TStronglyTypedId)factory(value);
        }
    
        public override void WriteJson(JsonWriter writer, TStronglyTypedId value, JsonSerializer serializer)
        {
            if (value is null)
                writer.WriteNull();
            else
                writer.WriteValue(value.Value);
        }
    }
    

    然后在启动文件中这样设置:

    services.AddControllers()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.Converters.Add(
                    new StronglyTypedIdNewtonsoftJsonConverter());
            });
    

    然后,我们得到了预期的结果,输出的结果是这样:

    {
        "id": 1,
        "name": "Apple",
        "unitPrice": 0.8
    }
    

    原文作者: thomas levesque
    原文链接:https://thomaslevesque.com/2020/12/07/csharp-9-records-as-strongly-typed-ids-part-3-json-serialization/

    最后

    欢迎扫码关注我们的公众号 【全球技术精选】,专注国外优秀博客的翻译和开源项目分享,也可以添加QQ群 897216102

    bk
    下一篇:没有了