NonSerialized on property
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NonSerialized is frequently misunderstood in .NET because developers expect it to hide properties from all serializers. In reality, it was designed for field-based serialization scenarios and does not automatically control property serialization in modern JSON pipelines. Reliable behavior comes from choosing serializer-specific attributes and testing contracts explicitly.
What NonSerialized Actually Targets
The attribute applies to fields that participate in compatible serializers, historically including binary formatter style flows. A property is not the same metadata target, even when an auto-property compiles to a backing field.
Correct field usage:
Placing NonSerialized directly on RuntimeOnlyToken does not produce the intended effect for most serializers.
Use the Right Ignore Attribute for the Active Serializer
Modern applications often use one of these serializers:
System.Text.Json- Newtonsoft.Json
- XML serialization APIs
Each has its own ignore mechanism.
System.Text.Json:
Newtonsoft.Json:
XML serializer example:
Auto-Properties and Backing Field Control
If you truly need field-level control with NonSerialized, use an explicit field and property wrapper. This makes serialization intent visible to maintainers and avoids compiler-generated field ambiguity.
Even then, the behavior depends on serializer selection.
Keep Domain and Transport Models Separate
Trying to hide internal data through many ignore attributes is usually a design smell. A cleaner pattern is separate classes:
- domain entity with full internal state
- transport DTO exposing only contract-safe fields
Mapping cost is small compared with the risk of sensitive field leakage.
Contract Tests Prevent Regressions
Serialization behavior can change during refactors, package upgrades, or source generator changes. Add tests that assert serialized payload contents.
Treat these tests as security and compatibility checks.
Migration Advice for Legacy Code
When modernizing older code:
- inventory every serialization boundary
- identify serializer type per boundary
- replace ambiguous attributes with explicit serializer attributes
- add snapshot tests before and after migration
This approach avoids silent payload changes in production APIs. It also makes future framework upgrades safer because serialization intent is documented in code and in tests.
Common Pitfalls
- Applying
NonSerializedto properties and assuming it works everywhere. - Mixing serializers in one solution without explicit per-serializer annotations.
- Relying on auto-property backing field behavior that is not guaranteed by contract intent.
- Exposing sensitive fields in logs even when serializer output is filtered.
- Skipping regression tests for serialized payload shape.
Summary
NonSerializedis field-focused, not a universal property exclusion tool.- Property serialization should be controlled with serializer-specific attributes.
- Explicit DTO contracts are safer than heavy use of ignore annotations.
- Back serialization decisions with automated payload tests.
- Treat serialization configuration as part of application security design.

