[Breaking change]: System.Text.Json no longer hardcodes polymorphism for root-level object
types
#30758
Closed
1 of 2 tasks
Labels
binary incompatible
Existing binaries may encounter a breaking change in behavior.
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 7
Work items for the .NET 7 release
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
Description
Using default configuration, System.Text.Json will serialize values of type
object
using polymorphism. This becomes less consistent once users register a custom converter forobject
, as witnessed in the following example:In short, System.Text.Json has historically been hardcoding polymorphism for root-level
object
values but not using polymorphism for nestedobject
values. Starting with .NET 7 RC1, this behavior has been changed so that custom converters never use polymorphism.Version
.NET 7 RC 1
Previous behavior
Using the custom object converter defined above, the code
Will serialize as 0, since the serializer will use polymorphism and ignore the custom converter.
New behavior
Using the custom object converter defined above, the code
Will serialize as 42, since the serializer will always consult the custom converter and not use polymorphism.
Type of breaking change
Reason for change
Inconsistency on serialization contracts for a given type, depending on whether it is being serialized as a root-level value or a nested value.
Recommended action
Users can get back polymorphism for root-level values by invoking one of the untyped serialization methods:
Feature area
Core .NET libraries
Affected APIs
No response
The text was updated successfully, but these errors were encountered: