Skip to main content
InterviewHubPRO
Section Overview
Q21

Question 21 of 50

Part 2: Concurrency, Zero-Allocation & Advanced Language Features (11 - 24)

42%
C# & CLR Internals (50 Qs)•Part 2: Concurrency, Zero-Allocation & Advanced Language Features (11 - 24)

How does System.Text.Json Source Generation work and why is it essential for Native AOT?

senior LevelCode ExamplecsharpConcurrency

In Simple Words: Default JSON serializers inspect object properties at runtime using reflection. With JsonSerializerContext, Roslyn source generators generate specialized serialization and deserialization code at compile time.

csharp
[JsonSerializable(typeof(UserDto))]
public partial class AppJsonSerializerContext : JsonSerializerContext { }

// Usage:
var json = JsonSerializer.Serialize(user, AppJsonSerializerContext.Default.UserDto);

This achieves up to 40% faster serialization and zero runtime reflection, enabling seamless Native AOT compilation.