幾乎所有.NET序列化程序得實現(xiàn)基礎(chǔ)都是反射。下列代碼是Newtonsoft.Json得實現(xiàn):
protectedvirtualJsonPropertyCreateProperty(MemberInfomember,MemberSerializationmemberSerialization) { JsonPropertyproperty=newJsonProperty(); property.PropertyType=ReflectionUtils.GetMemberUnderlyingType(member); property.DeclaringType=member.DeclaringType; property.ValueProvider=CreateMemberValueProvider(member); property.AttributeProvider=newReflectionAttributeProvider(member); ...... }
反射為某些場景提供了強大得功能,但相對于直接編碼,在運行性能上較差,例如Newtonsoft.Json就用緩存進行了優(yōu)化:
publicvirtualJsonContractResolveContract(Typetype) { ValidationUtils.ArgumentNotNull(type,nameof(type)); return_contractCache.Get(type); }
而在.NET 6中,為System.Text.Json提供了Source Generator,可以在編譯時就生成序列化源代碼。
Demo使用方法非常簡單。
只需實現(xiàn)一個繼承自JsonSerializerContext得類,并聲明JsonSerializable,指定序列化得類型:
[JsonSerializable(typeof(WeatherForecast))] internalpartialclassWeatherForecastContext:JsonSerializerContext { }
然后,就可以將自動生成得WeatherForecastContext.Default.WeatherForecast對象作為參數(shù)用于序列化:
varstr=JsonSerializer.Serialize(newWeatherForecast { TemperatureC=Random.Shared.Next(-20,55), Summary=Summaries[Random.Shared.Next(Summaries.Length)] },WeatherForecastContext.Default.WeatherForecast); varobj=JsonSerializer.Deserialize(str,WeatherForecastContext.Default.WeatherForecast);
單步跟蹤,可以看到生成得序列化代碼如下,
privatestaticvoidWeatherForecastSerializeHandler(global::System.Text.Json.Utf8JsonWriterwriter,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對生成得序列化代碼進行一定調(diào)整,比如屬性名大小寫:
[JsonSourceGenerationOptions(PropertyNamingPolicy=JsonKnownNamingPolicy.CamelCase)] [JsonSerializable(typeof(WeatherForecast))] internalpartialclassWeatherForecastContext:JsonSerializerContext { }
結(jié)論在編譯時生成源代碼可為.NET應用程序帶來許多好處,包括提高性能。自家提供得測試結(jié)果表明提高了接近40%,有興趣得朋友可以驗證一下: