-
Notifications
You must be signed in to change notification settings - Fork 5.4k
System.Text.Json uses inconsistent polymorphism semantics with custom object converters #72681
Copy link
Copy link
Closed
Labels
area-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.Issue or PR that represents a breaking API or functional change over a previous release.enhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additionsneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnetBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Milestone
Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.Issue or PR that represents a breaking API or functional change over a previous release.enhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additionsneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnetBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Type
Fields
Give feedbackNo fields configured for issues without a type.
System.Text.Json hardcodes polymorphic serialization when serializing root-level values of type
object, regardless of whether the registeredJsonConverter<object>supports polymorphism. While this is consistent with the semantics of the built-in converter forobject, it can result in inconsistent serialization contracts when used in conjunction with custom converters, depending on whether the value is serialized at the root level or as a nested node:This issue proposes that we change the root-level behavior for custom
objectconverters so that it no longer defaults to polymorphism (unless a polymorphic converter is being used). This will make serialization contracts consistent, regardless of whether the instance is serialized as a root-level value or as a nested value.This change might be a breaking change for users that rely on the current behavior. As a workaround, users can get back the existing polymorphic behavior by calling into one of the untyped
JsonSerializerAPIs, explicitly passing the runtime type of the value as theinputTypeparameter:Related to #54436 and #72187.