Description
System.Text.Json.Nodes.JsonNode no longer supports C# "dynamic" keyword.
Version introduced
.NET 6 Preview 7 (breaks functionality introduced in .NET 6 Preview 4)
Old behavior
The "dynamic" keyword could be used to get and set properties on the new JsonObject
class such as:
dynamic obj = JsonNode.Parse("{\"A\":42}");
int i = (int)obj.A;
New behavior
The property name must be specified as a string, and "dynamic" not used:
JsonNode obj = JsonNode.Parse("{\"A\":42}");
int i = (int)obj["A"];
Category
- Binary compatibility (code must be recompiled to use the newer API version)
- Source compatibility (successfully recompiling against the newer API version requires source changes)
Reason for change
As discussed in dotnet/runtime#53195, the "dynamic" feature in C# is considered somewhat stale and adding a dependency to a new API (JsonNode
and derived classes) is not considered a good practice.
If a newer, more modern version of "dynamic" is introduced, this decision will be re-opened.
Recommended action
Use the string-based property name.
If "dynamic" is necessary, the previous workaround mentioned at dotnet/runtime#42097 is still valid. This will be verified and updated as necessary for .NET 6.0.
Feature area
- System.Text.Json
Affected APIs
All System.Text.Json.Nodes.JsonNode.Parse()
APIs are affected if the return value is assigned to a C# variable of type dynamic
variable.
In addition, if JsonSerializerOptions.UnknownTypeHandling
== UnknownTypeHandling.JsonNode
then all System.Text.Json.JsonSerializer.Deserialize*
APIs are affected if they use dynamic
or object
for the type parameter and the return value is assigned to a C# variable of type dynamic
.
Issue metadata
- Issue type: breaking-change