Skip to content

Commit 6000276

Browse files
authored
serialization breaking change (#43883)
1 parent 5a56445 commit 6000276

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/core/compatibility/9.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ If you're migrating an app to .NET 9, the breaking changes listed here might aff
109109
| Title | Type of change | Introduced version |
110110
|-------------------------------------------------------------------------------|-------------------|--------------------|
111111
| [BinaryFormatter always throws](serialization/9.0/binaryformatter-removal.md) | Behavioral change | Preview 6 |
112+
| [Nullable JsonDocument properties deserialize to JsonValueKind.Null](serialization/9.0/jsondocument-props.md) | Behavioral change | Preview 1 |
112113

113114
## Windows Forms
114115

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: "Nullable JsonDocument properties deserialize to JsonValueKind.Null"
3+
description: Learn about the breaking change in serialization in .NET 9 where nullable JsonDocument values deserialize to JsonValueKind.Null instead of null.
4+
ms.date: 12/5/2024
5+
ai-usage: ai-assisted
6+
ms.custom: https://github.com/dotnet/docs/issues/43869
7+
---
8+
9+
# Nullable JsonDocument properties deserialize to JsonValueKind.Null
10+
11+
Starting with .NET 9, deserializing `null` JSON values into <xref:System.Text.Json.JsonDocument> results in non-null documents of type <xref:System.Text.Json.JsonValueKind.Null?displayProperty=nameWithType>.
12+
13+
```csharp
14+
using System.Text.Json;
15+
16+
var doc = JsonSerializer.Deserialize<JsonDocument>("null");
17+
18+
// Returns true in .NET 8 and false in .NET 9.
19+
Console.WriteLine(doc is null);
20+
21+
// Returns false in .NET 8 and true in .NET 9.
22+
Console.WriteLine(doc is { RootElement.ValueKind: JsonValueKind.Null });
23+
```
24+
25+
## Version introduced
26+
27+
.NET 9
28+
29+
## Previous behavior
30+
31+
In .NET 8 and earlier versions, deserializing `null` JSON values into `JsonDocument` gives back `null` results.
32+
33+
```csharp
34+
var doc = JsonSerializer.Deserialize<JsonDocument>("null");
35+
Console.WriteLine(doc is null); // True.
36+
```
37+
38+
## New behavior
39+
40+
Starting in .NET 9, deserializing `null` JSON values into `JsonDocument` gives back non-null instances of `JsonValueKind.Null`.
41+
42+
```csharp
43+
var doc = JsonSerializer.Deserialize<JsonDocument>("null");
44+
Console.WriteLine(doc is null); // False.
45+
Console.WriteLine(doc is { RootElement.ValueKind: JsonValueKind.Null }); // True.
46+
```
47+
48+
## Type of breaking change
49+
50+
This change is a [behavioral change](../../categories.md#behavioral-change).
51+
52+
## Reason for change
53+
54+
This change addresses an inconsistency between root-level JSON nulls and nested nulls in a document. It also makes it consistent with the behavior of the <xref:System.Text.Json.JsonDocument.Parse*?displayProperty=nameWithType> methods. The behavior of returning `null` was considered a bug and updated for alignment with the expected result.
55+
56+
## Recommended action
57+
58+
Update code that consumes deserialized objects containing `JsonDocument` types to expect `JsonValueKind.Null` instead of `null`.
59+
60+
## Affected APIs
61+
62+
* <xref:System.Text.Json.JsonSerializer.Deserialize*?displayProperty=fullName>
63+
* <xref:System.Text.Json.JsonDocument?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ items:
114114
items:
115115
- name: BinaryFormatter always throws
116116
href: serialization/9.0/binaryformatter-removal.md
117+
- name: Nullable JsonDocument properties deserialize to JsonValueKind.Null
118+
href: serialization/9.0/jsondocument-props.md
117119
- name: Windows Forms
118120
items:
119121
- name: BindingSource.SortDescriptions doesn't return null
@@ -1930,6 +1932,8 @@ items:
19301932
items:
19311933
- name: BinaryFormatter always throws
19321934
href: serialization/9.0/binaryformatter-removal.md
1935+
- name: Nullable JsonDocument properties deserialize to JsonValueKind.Null
1936+
href: serialization/9.0/jsondocument-props.md
19331937
- name: .NET 8
19341938
items:
19351939
- name: BinaryFormatter disabled for most projects

0 commit comments

Comments
 (0)