Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json.Nodes.JsonNode no longer supports C# "dynamic" keyword in Preview 7 #25105

Closed
2 of 3 tasks
steveharter opened this issue Jul 13, 2021 · 2 comments · Fixed by #25605
Closed
2 of 3 tasks
Assignees
Labels
binary incompatible Existing binaries may encounter a breaking change in behavior. breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3

Comments

@steveharter
Copy link
Member

steveharter commented Jul 13, 2021

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
@steveharter steveharter added the breaking-change Indicates a .NET Core breaking change label Jul 13, 2021
@steveharter steveharter self-assigned this Jul 13, 2021
@PRMerger7 PRMerger7 added the Pri3 label Jul 13, 2021
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged doc-idea Indicates issues that are suggestions for new topics [org][type][category] labels Jul 13, 2021
@gewarren gewarren added Pri1 High priority, do before Pri2 and Pri3 and removed ⌚ Not Triaged Not triaged Pri3 labels Jul 14, 2021
@gewarren gewarren added the 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release label Jul 23, 2021
@gewarren gewarren added the binary incompatible Existing binaries may encounter a breaking change in behavior. label Jul 28, 2021
@gewarren
Copy link
Contributor

@steveharter Did you assign this to yourself because you're planning to document it? Otherwise I will document it.

@softveda
Copy link

softveda commented Aug 11, 2021

Dynamic is "stale" is an opinion and not a strong argument. As long as the feature exists in C# it should be supported. Some of use write C# to quickly prototype and achieve results and calling arbitrary APIs to test json response is a very common thing. Using dynamic is very valuable here. Code can always be written to better standards and performant but that doesn't mean everyone has to do it in all contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binary incompatible Existing binaries may encounter a breaking change in behavior. breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants