Obfuscating a Polymorphic JSON Contract: Why [JsonDerivedType] Survives Renaming and TypeNameHandling Does Not
Property-level renaming breaks are annoying but shallow: a key comes out wrong, you pin it with [JsonPropertyName] , done. Polymorphic serialization is a deeper hazard, because the thing that goes on the wire is not a property name — it is the identity of the type itself . Obfuscation exists to destroy that identity. So a polymorphic contract is exactly the place where "renaming broke…
Renaming does not impact System.Text.Json's [JsonDerivedType] serialization, whereas Newtonsoft's TypeNameHandling does. With System.Text.Json, the wire format remains unchanged even after type renaming. To obfuscate a type hierarchy without relying on a single name, use opaque, intentional discriminators chosen as a wire vocabulary, rather than deriving them from the identifier. This approach ensures that even after obfuscation, the wire format remains identical and can be deserialized without issue.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.