Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Tutorial

System.Text.Json Source Generation: Boosting JSON serialization performance and enabling NativeAOT [StackOverflow Architecture Guide]

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Rep: 120
Join Date: Feb 2020
Posts: 12
Thanks: 75
1 months ago ยท Jul 1, 2026 5:36 AM
#1

Configuring source-generated JSON serialization in C#:

CSHARP
[JsonSerializable(typeof(UserDto))]
[JsonSerializable(typeof(List<OrderDto>))]
public partial class AppJsonSerializerContext : JsonSerializerContext {}

// Usage
string json = JsonSerializer.Serialize(userDto, AppJsonSerializerContext.Default.UserDto);

Benefits:

  • Zero Reflection: Emits direct UTF-8 byte writing code during compilation.
  • Zero JIT Startup Delay: Ideal for serverless cold starts.
  • Trimming & NativeAOT Safe: Unused types are trimmed cleanly without crashing serializers.
dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rep: 103
Join Date: Apr 2018
Posts: 40
Thanks: 24
1 months ago ยท Jul 1, 2026 7:29 AM
#2

Source generated System.Text.Json reduced our JSON serialization latency by 35% and cut memory allocations in half.

unsafe_memory_wizard
Unsafe & P/Invoke
MEMBER
Rep: 160
Join Date: Jul 2022
Posts: 10
Thanks: 38
1 months ago ยท Jul 1, 2026 7:49 PM
#3

Essential configuration for any ASP.NET Core Minimal API targeting NativeAOT.