Skip to content

Commit c5a92f5

Browse files
authored
jsonnode breaking change (#25605)
1 parent 528a0bf commit c5a92f5

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

docs/core/compatibility/6.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
8181
| [GetTargetFrameworkProperties and GetNearestTargetFramework removed from ProjectReference protocol](sdk/6.0/gettargetframeworkproperties-and-getnearesttargetframework-removed.md) | Preview 1 |
8282
| [Implicit namespaces in C# projects](sdk/6.0/implicit-namespaces.md) | Preview 7 |
8383

84+
## Serialization
85+
86+
| Title | Preview introduced |
87+
| - | - |
88+
| [JsonNode no longer supports C# `dynamic` type](serialization/6.0/jsonnode-dynamic-type.md) | Preview 7 |
89+
8490
## Windows Forms
8591

8692
| Title | Preview introduced |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,10 @@ items:
747747
href: core-libraries/5.0/utf-7-code-paths-obsolete.md
748748
- name: Serialization
749749
items:
750+
- name: .NET 6
751+
items:
752+
- name: JsonNode no longer supports C# `dynamic`
753+
href: serialization/6.0/jsonnode-dynamic-type.md
750754
- name: .NET 5
751755
items:
752756
- name: BinaryFormatter.Deserialize rewraps exceptions

0 commit comments

Comments
 (0)