Skip to content

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

Closed

Description

@steveharter

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

Activity

  1. added
    breaking-change
    on Jul 13, 2021
  2. self-assigned this
    on Jul 13, 2021
  3. added and removed
    ⌚ Not Triaged
    on Jul 14, 2021
  4. added
    🏁 Release: .NET 6
    binary incompatible
    on Jul 23, 2021
  5. gewarren commented on Jul 30, 2021

    @gewarren
    Collaborator

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

  6. softveda commented on Aug 11, 2021

    @softveda

    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

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 6binary incompatiblebreaking-change

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions