Join our Newsletter — 33% off our NHI Course
Home› Glossary› Foundations & NHI Taxonomy› System.Text.Json Source Generator
Foundations & NHI Taxonomy

System.Text.Json Source Generator

← Back to Glossary
By NHI Mgmt Group Updated November 25, 2026 Domain: Foundations & NHI Taxonomy

A System.Text.Json source generator is a compile-time tool that creates JSON serialisation and deserialisation code for .NET types. It reduces reflection at runtime by emitting type-specific metadata and converters, which can improve performance and help applications work in trimmed or restricted runtime environments.

What the generator does

System.Text.Json source generation moves JSON serialisation work from runtime to compile time. Instead of discovering .NET types through reflection on the fly, the generator emits type-specific metadata and converters so the application can serialise and deserialise known shapes more directly.

This changes the shape of the implementation, not the JSON format itself. The same payloads can be processed, but the generated code gives the runtime less work to do and often makes behaviour more predictable in environments where reflection is limited, trimmed, or intentionally restricted.

Why it matters for performance and deployment

The main benefit is reduced runtime overhead. Reflection-based JSON handling can add cost at startup and during repeated serialisation paths, while generated code can remove much of that lookup work. That is most valuable in APIs, services, and high-throughput applications where JSON is a hot path.

Source generation also matters when applications are trimmed or published for constrained runtimes. In those environments, code that depends on late-bound type discovery can become fragile if metadata is removed or unavailable. Generated serializers help preserve functionality by compiling the required type information in advance.

That does not make source generation a universal replacement for reflection. It works best when the application knows its serialisation models ahead of time, and it trades some flexibility for stronger compile-time shape knowledge.

How it changes serialisation behaviour

A source generator typically produces code for a defined set of types, properties, and converters, which means the resulting serialisation path is more explicit. That can improve compatibility with AOT-style builds and reduce surprises caused by runtime inspection of private or dynamically discovered members.

It also encourages clearer contracts. Because the generator must understand the types up front, teams are pushed to declare the models they intend to exchange rather than relying on loosely governed runtime behaviour. For many .NET services, that becomes a design advantage as much as a performance one.

The practical constraint is coverage. If a type shape is not included in the generator context, or if the application depends on highly dynamic JSON handling, reflection-based fallback or alternative configuration may still be needed. The engineering question is therefore not only whether generation is faster, but whether the serialisation surface is stable enough to benefit from it.

Common trade-offs and failure modes

The biggest trade-off is between flexibility and predictability. Generated serializers are fast and trim-friendly, but they can be less forgiving when models change frequently or when code relies on late-bound polymorphism, custom converters, or dynamic document patterns.

Misconfiguration usually shows up as missing metadata, incomplete deserialisation support, or unexpected differences between development and published builds. In practice, that means the generator should be treated as part of the build contract, not just as an optimisation switch.

It is also worth remembering that generated JSON code improves runtime efficiency, but it does not by itself validate business logic, authorisation, or trust boundaries. The generated serializer should be viewed as an implementation mechanism for moving data safely and efficiently, not as a security control on its own.

Where to use it in a .NET design

Source generation is usually the strongest fit when the serialised models are well-defined, the application needs predictable runtime behaviour, or publish-time trimming would otherwise threaten reflection-based code paths. It is especially useful in service back ends, libraries, and deployment models that prioritise small binaries and lower startup cost.

For teams evaluating whether to adopt it, the key question is whether the serialisation model is sufficiently explicit to justify compile-time generation. When that is true, the approach can simplify runtime dependencies and make JSON handling more robust across constrained environments. For broader .NET JSON guidance, see Secure Deserialisation Guide, which covers how to keep deserialisation strict and fail-closed.

When the application already uses generated code, published build settings should be checked carefully so the generated context remains aligned with the deployed types. That is the point where the feature delivers its value, not just at compile time but throughout the application lifecycle.

Related platform guidance from Microsoft is available in the official System.Text.Json source generation documentation, which explains the supported patterns and build-time configuration.

Risk and Threat Considerations

The main risk is not that source generation creates a new attack surface, but that teams may assume it removes the need to think about deserialisation safety. If generated contexts are too permissive, or if fallback paths remain active, the application can still accept unexpected shapes or process data in ways the developer did not intend.

Failure mechanism: Problems arise when the generated contract does not match the deployed model, when trimming removes required metadata, or when deserialisation logic remains broader than the application’s trust assumptions.

Impact: The result can be broken functionality, silent data loss, inconsistent runtime behaviour across environments, or, in poorly constrained designs, unsafe object handling that should have been excluded by the contract.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS, NIST SP 800-53 Rev 5 and CIS Controls v8 set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
OWASP ASVSV1 — Encoding and SanitizationGenerated deserialisation still depends on safe handling of incoming structured data.
Recommendation — Validate JSON inputs and parsing assumptions before deserialising application data.
NIST SP 800-53 Rev 5SA-11 — Developer Testing and EvaluationSource-generated serializers should be verified against build and runtime conditions.
Recommendation — Test generated serialization paths under the same trimming and deployment settings you will ship.
ISO/IEC 27001:2022A.8.25 — Secure development life cycleCompile-time serializer generation is a development practice that should be governed in the SDLC.
Recommendation — Include generated serialization code in secure development reviews and release checks.
CIS Controls v8CIS-16 — Application Software SecuritySerializer generation affects application code quality and safe handling of data in software.
Recommendation — Review generated serialization behaviour as part of application security testing and change control.

Practitioner Guidance

Why practitioners should care: Treat source generation as a build-time contract, not just a performance tweak. The generator is most valuable when the serialisation model is stable enough to make explicit metadata an asset rather than a maintenance burden.

What to watch for: Pay attention to model drift, polymorphic edge cases, and build or publish settings that change which types are available at runtime. Those are the places where a fast serializer can still fail in production if the generated context no longer matches the deployed application.

Practitioner takeaway: Use source generation when you can enumerate the serialisation surface clearly, and verify the deployed build behaves the same way as the build you tested.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org