Streamlining the Obfuscation Pipeline
One of the most significant advantages of the discriminator-based design is the reduction in complexity for build pipelines. In legacy systems, developers often had to implement "keep rules" for their Data Transfer Objects (DTOs), essentially telling the obfuscator to leave certain type names untouched so that serialization wouldn’t break. This often meant surrendering the very parts of the hierarchy that were most in need of protection.
With System.Text.Json, these exclusions become unnecessary. Because the hierarchy’s registration relies on tokens and literals, a modern obfuscator like Nebula.NET can rename the entire polymorphic hierarchy without any special configuration. The types can be renamed, their control flows flattened, and their strings encrypted, all while maintaining a stable serialization contract.
Furthermore, the use of .NET Source Generators adds another layer of robustness. By using a [JsonSerializable] context, the serialization logic—including the discriminators and property names—is baked into the generated code at compile-time. This happens before the obfuscator even runs. The generated constants for "circle" and "rect" are frozen, ensuring that no amount of post-processing can alter the output format.
Verification and Cross-Build Stability
The ultimate test of any serialization strategy is its performance in a production-like environment. For organizations managing persisted data, it is not enough to prove that a single build can round-trip its own data. The real requirement is cross-build stability: the ability to serialize data with one version of the application and deserialize it with a subsequent, obfuscated version.
Industry best practices now suggest incorporating specific integration tests that run against the "protected" or obfuscated artifacts. By capturing a JSON string from an older build and attempting to deserialize it using the current obfuscated assembly, developers can catch silent, data-corrupting regressions before they reach production. If the test successfully yields the correct concrete type after a rename, the developer can be confident that the document remains safe.
The shift toward explicit discriminators represents a broader principle in software architecture: type identity on the wire should be a value owned by the developer, never a byproduct of the compiler’s internal naming scheme. By adhering to this standard, .NET developers can leverage the full power of obfuscation and modern serialization without the fear of rendering their historical archives unreadable. As the ecosystem continues to evolve, the reliance on CLR-bound identities is likely to become a relic of the past, replaced by the more intentional and secure patterns found in modern .NET frameworks.
