|
| 1 | +--- |
| 2 | +title: "Breaking change: JsonNode no longer supports dynamic type" |
| 3 | +description: Learn about the breaking change in .NET 6 where the C# `dynamic` type can no longer be used to get and set properties on `JsonNode`. |
| 4 | +ms.date: 08/11/2021 |
| 5 | +--- |
| 6 | +# JsonNode no longer supports C# `dynamic` type |
| 7 | + |
| 8 | +In earlier .NET 6 preview versions, you could use the C# [`dynamic` type](../../../../csharp/language-reference/builtin-types/reference-types.md#the-dynamic-type) to get and set properties on the <xref:System.Text.Json.Nodes.JsonNode> class. Starting in Preview 7, you can no longer use `dynamic` for `JsonNode` properties. |
| 9 | + |
| 10 | +## Old behavior |
| 11 | + |
| 12 | +In earlier .NET 6 preview versions, the `dynamic` type could be used to get and set properties on the <xref:System.Text.Json.Nodes.JsonNode> class. For example: |
| 13 | + |
| 14 | +```csharp |
| 15 | +dynamic obj = JsonNode.Parse("{\"A\":42}"); |
| 16 | +int i = (int)obj.A; |
| 17 | +``` |
| 18 | + |
| 19 | +## New behavior |
| 20 | + |
| 21 | +Starting in .NET 6 Preview 7, the property name must be specified as a string in the indexer, and you can't use the `dynamic` type for the return value. For example: |
| 22 | + |
| 23 | +```csharp |
| 24 | +JsonNode obj = JsonNode.Parse("{\"A\":42}"); |
| 25 | +int i = (int)obj["A"]; |
| 26 | +``` |
| 27 | + |
| 28 | +## Change category |
| 29 | + |
| 30 | +This change affects [*binary compatibility*](../../categories.md#binary-compatibility). |
| 31 | + |
| 32 | +## Version introduced |
| 33 | + |
| 34 | +.NET 6 Preview 7 (breaks functionality introduced in .NET 6 Preview 4) |
| 35 | + |
| 36 | +## Reason for change |
| 37 | + |
| 38 | +As discussed in [dotnet/runtime#53195](https://github.com/dotnet/runtime/issues/53195), the `dynamic` feature in C# is considered somewhat stale. Adding a dependency to a new API, that is, <xref:System.Text.Json.Nodes.JsonNode> and its derived classes, is not a good practice. |
| 39 | + |
| 40 | +## Recommended action |
| 41 | + |
| 42 | +Use the string-based property name. |
| 43 | + |
| 44 | +If `dynamic` is necessary, you can use the workaround mentioned at [dotnet/runtime#42097](https://github.com/dotnet/runtime/issues/42097). That workaround will be verified and updated as necessary for .NET 6. |
| 45 | + |
| 46 | +## Affected APIs |
| 47 | + |
| 48 | +When the return value is assigned to a variable of type [`dynamic`](../../../../csharp/language-reference/builtin-types/reference-types.md#the-dynamic-type), the following methods are affected: |
| 49 | + |
| 50 | +- <xref:System.Text.Json.Nodes.JsonNode.Parse%2A?displayProperty=fullName> |
| 51 | + |
| 52 | +When the return value is assigned to a variable of type [`dynamic`](../../../../csharp/language-reference/builtin-types/reference-types.md#the-dynamic-type), the type parameter is `dynamic` or `object`, and `JsonSerializerOptions.UnknownTypeHandling == UnknownTypeHandling.JsonNode`, the following methods are affected: |
| 53 | + |
| 54 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.ReadOnlySpan{System.Byte},System.Text.Json.Serialization.Metadata.JsonTypeInfo{%60%600})?displayProperty=fullName> |
| 55 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.ReadOnlySpan{System.Byte},System.Text.Json.JsonSerializerOptions)?displayProperty=fullName> |
| 56 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.ReadOnlySpan{System.Char},System.Text.Json.Serialization.Metadata.JsonTypeInfo{%60%600})?displayProperty=fullName> |
| 57 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.ReadOnlySpan{System.Char},System.Text.Json.JsonSerializerOptions)?displayProperty=fullName> |
| 58 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=fullName> |
| 59 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.Serialization.Metadata.JsonTypeInfo{%60%600})?displayProperty=fullName> |
| 60 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.Text.Json.Utf8JsonReader@,System.Text.Json.Serialization.Metadata.JsonTypeInfo{%60%600})?displayProperty=fullName> |
| 61 | +- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.Text.Json.Utf8JsonReader@,System.Text.Json.JsonSerializerOptions)?displayProperty=fullName> |
0 commit comments