-
Notifications
You must be signed in to change notification settings - Fork 5
Add NativeAOT support for .NET 8+ via JSON source generation #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
…frameworks Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
|
@copilot 项目构建存在一些问题
|
…T sample Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
已在提交 6d7ba7b 中解决:
当前状态:Core 项目可无警告构建,但 Rest 项目仍有 IL 警告,这些警告来自自定义转换器和泛型序列化调用。建议采用渐进式方法,在后续 PR 中继续改进。 |
|
@copilot 你可以编写一个扩展方法,从 JsonSerializerOptions 上获取指定类型的 JsonTypeInfo,返回强类型 JsonTypeInfo,不存在时抛出异常,存在时则返回给调用此此扩展方法的位置,传递给序列化/反序列化方法。 |
…erialization Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
已在提交 f6cccd7 中实现: 创建了
已更新所有主要序列化/反序列化位置使用 JsonTypeInfo:
结果:Core 项目构建无警告,Rest 项目主要 API 路径已使用源生成,剩余约 120 个 IL 警告来自处理多态类型的自定义转换器(符合预期)。 |
|
@copilot 你只在 Rest 项目中添加了 JsonSerializerContext,WebSocket 和 Webhook 项目没有添加,请为这两个项目添加项目内的 JsonSerializerContext,并在这两个项目内修正所传递的 JsonTypeInfo |
…nTypeInfo usage Co-authored-by: gehongyan <21241496+gehongyan@users.noreply.github.com>
已在提交 f11b8c7 中完成: 为 WebSocket 和 Webhook 项目添加了各自的 JsonSerializerContext:
构建结果:WebSocket 和 Webhook 项目均以 0 警告、0 错误成功构建。 |
Adds NativeAOT compatibility for .NET 8 and later target frameworks by implementing JSON source generation for API serialization across all projects.
Changes
JsonSerializerContextclasses for compile-time serialization metadata generation:KookJsonSerializerContext(Rest): 160+ API model typesKookWebSocketJsonSerializerContext(WebSocket): 37 Gateway event model typesKookWebhookJsonSerializerContext(Webhook): 2 Webhook-specific model typesTypeInfoPropertyName = "ApiFriendState"forKook.API.Rest.FriendStateJsonSerializerOptionsExtensionsclass with:GetTypedTypeInfo<T>(): Returns strongly-typedJsonTypeInfo<T>from the serializer contextGetTypeInfo(Type): Returns non-genericJsonTypeInfofor runtime typesJsonSerializerOptionsinstantiations to initialize from the source-generated contexts while preserving custom convertersJsonTypeInfo:KookRestApiClient.SerializeJson()andDeserializeJsonAsync<T>(),MessageHelpermethods,ChannelHelpermethods,CardJsonExtensionpublic API methodsKookSocketApiClient,KookSocketClient,NullableChannelConverterKookWebhookApiClientfor encrypted frame processingIsTrimmableandIsAotCompatiblefor .NET 8+ targets via conditional MSBuild propertiesKook.Net.CommandsasIsAotCompatible=falseandIsTrimmable=falsedue to reflection dependenciessamples/Kook.Net.Samples.NativeAOTdemonstrating:Enum.GetValues(typeof(T))with genericEnum.GetValues<T>()for .NET 8+ (conditional compilation for older targets)[RequiresUnreferencedCode]and[RequiresDynamicCode]attributes (NET5.0+)Example
Limitations
NotImplementedEmbed.Resolve<T>()andNotImplementedPokeResource.Resolve<T>()marked as non-AOT compatible (fallback deserialization for unknown types)Testing
The included NativeAOT sample project can be tested with:
Results
JsonTypeInfo, ~120 IL warnings remain in custom converters (expected and acceptable for polymorphic type handling)JsonTypeInfoJsonTypeInfoOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.