[Breaking change]: The System.Text.Json reflection-based deserializer is resolving all property metadata eagerly #37041
Labels
breaking-change
Indicates a .NET Core breaking change
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
📌 seQUESTered
Identifies that an issue has been imported into Quest.
Description
The System.Text.Json reflection-based serializer has been using a lazy loading approach when it comes to resolving property metadata. This has made it possible for POCOs containing unsupported property types to deserialize successfully, provided that the underlying JSON doesn't bind to any of the unsupported properties. This is despite the fact that instances of the same type would fail to serialize.
Starting with .NET 8, the serializer has been changed so that all properties are resolved eagerly in both serialization and deserialization. This has been done to add better support for combining multiple resolvers (cf. dotnet/runtime#71933), which necessitates early analysis of the serialized type graph. A side-effect of this change is that users currently depending on the previous behaviour could start seeing runtime deserialization errors where it previously was working.
Version
.NET 8 Preview 4
Previous behavior
The code
Will work without issue in .NET 7 applications.
New behavior
The same code will throw the following runtime exception in .NET 8:
Which is consistent with the error thrown when attempting to serialize instances of the same type in .NET 7 and the source generator (which produces a compile-time error).
Type of breaking change
Reason for change
Necessitated by new requirements related to fast-path serialization support in combined source generated contexts (cf. dotnet/runtime#71933)
Recommended action
Removing the unsupported property, authoring a custom converter for the unsupported type, or adding the
JsonIgnoreAttribute
like so:Feature area
Serialization
Affected APIs
No response
Associated WorkItem - 159034
The text was updated successfully, but these errors were encountered: