网站开发的技术意义,珠海公司注册,建筑公司信用分查询官网,ps怎么做网站首页和超链接使用 C# 9 的records作为强类型ID - 路由和查询参数在本系列的上一篇文章中使用 C# 9 的records作为强类型ID - 路由和查询参数#xff0c;我们注意到强类型ID的实体#xff0c;序列化为 JSON 的时候报错了#xff0c;就像这样#xff1a;{id: {value我们注意到强类型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序列化为其值而不是对象我们需要编写一个通用的 JsonConverterpublic class StronglyTypedIdJsonConverterTStronglyTypedId, TValue : JsonConverterTStronglyTypedIdwhere TStronglyTypedId : StronglyTypedIdTValuewhere TValue : notnull
{public override TStronglyTypedId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options){if (reader.TokenType is JsonTokenType.Null)return null;var value JsonSerializer.DeserializeTValue(ref reader, options);var factory StronglyTypedIdHelper.GetFactoryTValue(typeToConvert);return (TStronglyTypedId)factory(value);}public override void Write(Utf8JsonWriter writer, TStronglyTypedId value, JsonSerializerOptions options){if (value is null)writer.WriteNullValue();elseJsonSerializer.Serialize(writer, value.Value, options);}
}
逻辑很简单对于直接读取 id.value, 对于反序列化创建一个强类型id的实例然后给它赋值。然后在启动类中配置:services.AddControllers().AddJsonOptions(options {options.JsonSerializerOptions.Converters.Add(new StronglyTypedIdJsonConverterProductId, int());});
现在拿到了想要的结果{id: 1,name: Apple,unitPrice: 0.8
}
真好不过还有有一个问题我们只为添加了一个对于ProductId的转换器但我不想为每种类型的强类型ID添加另一个转换器我们想要一个适用于所有强类型id的转换器……现在可以创建一个转换器工厂(ConverterFactory),就像下边这样public class StronglyTypedIdJsonConverterFactory : JsonConverterFactory
{private static readonly ConcurrentDictionaryType, 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然后在启动文件修改现在我们的转换器将应用于每个强类型IDservices.AddControllers().AddJsonOptions(options {options.JsonSerializerOptions.Converters.Add(new StronglyTypedIdJsonConverterFactory());});
Newtonsoft.Json如果您的项目使用的是Newtonsoft.Json进行JSON序列化那就很简单了。当它序列化一个值时Newtonsoft.Json 查找一个compatible JsonConverter如果找不到就查找一个TypeConverter, 如果TypeConverter存在并且可以将值转换为string那么它把值序列化为字符串, 因为我们之前定义了 TypeConverterNewtonsoft.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 ConcurrentDictionaryType, 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 StronglyTypedIdNewtonsoftJsonConverterTStronglyTypedId, TValue : JsonConverterTStronglyTypedIdwhere TStronglyTypedId : StronglyTypedIdTValuewhere 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.DeserializeTValue(reader);var factory StronglyTypedIdHelper.GetFactoryTValue(objectType);return (TStronglyTypedId)factory(value);}public override void WriteJson(JsonWriter writer, TStronglyTypedId value, JsonSerializer serializer){if (value is null)writer.WriteNull();elsewriter.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