.NET 10: System.Text.Json Improvements
Introduction
System.Text.Json continues to evolve in .NET 10 with meaningful improvements focused on correctness and performance.
Let’s look at two areas that matter in real-world applications: duplicate property handling and PipeReader integration.
Duplicate Property Rejection
JSON technically allows duplicate properties. Historically, System.Text.Json would silently keep the last occurrence.
Exemple:
{
"id": 1,
"id": 2
}
Before .NET 10, this would deserialize without error, resulting in:
id = 2;
This behavior could hide bugs or introduce subtle security risks. With .NET 10, you can explicitly reject duplicate properties. Now, a JsonException is thrown when duplicates are detected, giving you stricter input validation and more predictable behavior:
Pretty useful right?
PipeReader Integration
High-performance applications often use System.IO.Pipelines. However, prior to .NET 10, System.Text.Json did not provide a direct overload accepting aPipeReader.
Developers typically had to bridge the pipeline to a Stream:
These approaches worked but:
- Added extra complexity
- Introduced bridging layers
- Increased the risk of subtle buffering bugs
.NET 10 simplifies this dramatically by allowing direct deserialization from a PipeReader:
- No stream adapter.
- No manual reader loop.
- No extra plumbing.
This results in:
- Cleaner code
- Better performance characteristics
- More natural integration with ASP.NET Core and custom servers
Conclusion
.NET 10 strengthens System.Text.Json in two key ways:
- Duplicate JSON properties can now be rejected explicitly
- PipeReader integration is direct and far simpler
Small API additions ->big impact for correctness and high-performance workloads.