前言
几乎所有.NET序列化程序的实现基础都是反射。下列代码是Newtonsoft.Json的实现:
- protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
- {
- JsonProperty property = new JsonProperty();
- property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
- property.DeclaringType = member.DeclaringType;
- property.ValueProvider = CreateMemberValueProvider(member);
- property.AttributeProvider = new ReflectionAttributeProvider(member);
- ......
- }
反射为某些场景提供了强大的功能,但相对于直接编码,在运行性能上较差,例如Newtonsoft.Json就用缓存进行了优化:
- public virtual JsonContract ResolveContract(Type type)
- {
- ValidationUtils.ArgumentNotNull(type, nameof(type));
- return _contractCache.Get(type);
- }
而在.NET 6中,为System.Text.Json提供了Source Generator,可以在编译时就生成序列化源代码。
Demo
使用方法非常简单。
只需实现一个继承自JsonSerializerContext的类,并声明JsonSerializable,指定序列化的类型:
- [JsonSerializable(typeof(WeatherForecast))]
- internal partial class WeatherForecastContext : JsonSerializerContext
- {
- }
然后,就可以将自动生成的WeatherForecastContext.Default.WeatherForecast对象作为参数用于序列化:
- var str = JsonSerializer.Serialize(new WeatherForecast
- {
- TemperatureC = Random.Shared.Next(-20, 55),
- Summary = Summaries[Random.Shared.Next(Summaries.Length)]
- }, WeatherForecastContext.Default.WeatherForecast);
- var obj = JsonSerializer.Deserialize(str, WeatherForecastContext.Default.WeatherForecast);
单步跟踪,可以看到生成的序列化代码如下,
- private static void WeatherForecastSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::WebApplication1.WeatherForecast? value)
- {
- if (value == null)
- {
- writer.WriteNullValue();
- return;
- }
- writer.WriteStartObject();
- writer.WriteNumber(PropName_TemperatureC, value.TemperatureC);
- writer.WriteNumber(PropName_TemperatureF, value.TemperatureF);
- writer.WriteString(PropName_Summary, value.Summary);
- writer.WriteEndObject();
- }
另外,还可以使用JsonSourceGenerationOptionsAttribute对生成的序列化代码进行一定调整,比如属性名大小写:
- [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
- [JsonSerializable(typeof(WeatherForecast))]
- internal partial class WeatherForecastContext : JsonSerializerContext
- {
- }
结论
在编译时生成源代码可为.NET应用程序带来许多好处,包括提高性能。官方提供的测试结果表明提高了接近40%,有兴趣的朋友可以验证一下: