Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
Tutorial

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

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Өкіл: 120
Қосылу күні: Feb 2020
Хабарламалар: 12
Рахмет: 75
1 ай бұрын · 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
Өкіл: 103
Қосылу күні: Apr 2018
Хабарламалар: 40
Рахмет: 24
1 ай бұрын · 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
Өкіл: 160
Қосылу күні: Jul 2022
Хабарламалар: 10
Рахмет: 38
1 ай бұрын · Jul 1, 2026 7:49 PM
#3

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