November 17, 2026

The Evolution of Polymorphic Serialization: Why System.Text.Json Offers a Safer Path for Obfuscated .NET Applications

In the landscape of .NET development, maintaining the integrity of serialized data is often viewed as a routine task. Developers are well-acquainted with the minor inconveniences of property-level renaming breaks—those instances where a JSON key comes out incorrectly after a build, only to be quickly corrected with a [JsonPropertyName] attribute. These issues are generally considered shallow, cosmetic, and easily remediated. However, a much deeper and more catastrophic hazard exists within the realm of polymorphic serialization, where the stakes transition from minor wire-format mismatches to the total loss of historical data readability.

The core of the issue lies in how a system identifies a type during the serialization process. When dealing with polymorphism—such as a list containing various derived types like circles and rectangles stored under a base "shape" reference—the serializer must encode the identity of the concrete type itself. In many legacy configurations, this identity is inextricably linked to the Common Language Runtime (CLR) type name. When code obfuscation is introduced into the pipeline, its primary objective is to destroy that very identity to protect intellectual property. This creates a fundamental conflict: the obfuscator renames the type to an unintelligible string, while the serializer remains dependent on the original name to reconstruct the object.

Recent analysis of modern .NET design reveals that this vulnerability is not an inherent flaw of serialization, but rather a consequence of specific design choices. While legacy tools like Newtonsoft.Json often default to patterns that are fragile under obfuscation, the modern System.Text.Json library provides a framework that is "safe by construction." By moving away from CLR-bound identities and toward developer-owned discriminators, the industry is seeing a shift toward more resilient data persistence strategies.

Two Philosophies of Type Encoding

To understand why some systems fail where others succeed, one must examine the two fundamentally different ways to encode a type on the wire. The legacy approach, exemplified by Newtonsoft’s TypeNameHandling, relies on writing the assembly-qualified name of the CLR type directly into the JSON metadata. For instance, a "Circle" object might be serialized with a metadata property like $type set to "MyApp.Shapes.Circle, MyApp".

On the surface, this approach is highly flexible. It allows the serializer to resolve the string back to a functional Type via the equivalent of a Type.GetType call. However, this convenience welds the wire format to the internal CLR identity. The moment that identity changes—whether through refactoring or obfuscation—the link is severed.

In contrast, the System.Text.Json approach, utilizing the [JsonDerivedType] attribute, decouples the wire format from the internal class name. Instead of relying on the compiler’s name for a class, the developer explicitly chooses a discriminator—a literal string like "circle" or "rect". When the system reads this discriminator, it performs a lookup in a registration table built from attributes to find the corresponding type. In this model, the wire format is welded to a string owned by the developer, not a name owned by the compiler or modified by an obfuscator.

The Impact of Obfuscation on Serialization Integrity

The divergence between these two designs becomes starkly apparent when an obfuscator is introduced into the build process. Obfuscation works by consistently rewriting type names and metadata tokens throughout an assembly. Every reference to a class—whether it be a method call, a field type, or a typeof expression—is updated to point to the new, renamed entity. This transparency allows ordinary code to function perfectly despite the name changes.