Skip to content

Commit d5a359e

Browse files
authored
Breaking changes for JsonSerializer.Deserialize (#20956)
1 parent 059c330 commit d5a359e

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

docs/core/compatibility/3.1-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,13 @@ If you're migrating from version 3.1 of .NET Core, ASP.NET Core, or EF Core to v
383383

384384
## Serialization
385385

386+
- [JsonSerializer.Deserialize requires single-character string](#jsonserializerdeserialize-requires-single-character-string)
386387
- [BinaryFormatter.Deserialize rewraps some exceptions in SerializationException](#binaryformatterdeserialize-rewraps-some-exceptions-in-serializationexception)
387388

389+
[!INCLUDE [deserializing-json-into-char-requires-single-character](../../../includes/core-changes/serialization/5.0/deserializing-json-into-char-requires-single-character.md)]
390+
391+
***
392+
388393
[!INCLUDE [binaryformatter-deserialize-rewraps-exceptions](../../../includes/core-changes/serialization/5.0/binaryformatter-deserialize-rewraps-exceptions.md)]
389394

390395
***

docs/core/compatibility/serialization.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ The following breaking changes are documented on this page:
99

1010
| Breaking change | Introduced version |
1111
| - | - |
12+
| [JsonSerializer.Deserialize requires single-character string](#jsonserializerdeserialize-requires-single-character-string) | 5.0 |
1213
| [BinaryFormatter.Deserialize rewraps some exceptions in SerializationException](#binaryformatterdeserialize-rewraps-some-exceptions-in-serializationexception) | 5.0 |
1314

14-
## .NET Core 5.0
15+
## .NET 5.0
16+
17+
[!INCLUDE [deserializing-json-into-char-requires-single-character](../../../includes/core-changes/serialization/5.0/deserializing-json-into-char-requires-single-character.md)]
18+
19+
***
1520

1621
[!INCLUDE [binaryformatter-deserialize-rewraps-exceptions](../../../includes/core-changes/serialization/5.0/binaryformatter-deserialize-rewraps-exceptions.md)]
1722

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
### JsonSerializer.Deserialize requires single-character string
2+
3+
When the type parameter is <xref:System.Char>, the string argument to <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=nameWithType> must contain a single character for deserialization to succeed.
4+
5+
#### Change description
6+
7+
In previous .NET versions, if you pass a multi-character string to <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=nameWithType> and the type parameter is <xref:System.Char>, the deserialization succeeds and only the first character is deserialized.
8+
9+
In .NET 5.0 and later, when the type parameter is <xref:System.Char>, passing anything other than a single-character string causes a <xref:System.Text.Json.JsonException> to be thrown.
10+
11+
```csharp
12+
// .NET Core 3.0 and 3.1: Returns the first character 'a'.
13+
// .NET 5.0 and later: Throws JsonException because payload has more than one character.
14+
JsonSerializer.Deserialize<char>("\"abc\"");
15+
16+
// Correct usage.
17+
JsonSerializer.Deserialize<char>("\"a\"");
18+
```
19+
20+
#### Version introduced
21+
22+
5.0
23+
24+
#### Reason for change
25+
26+
<xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=nameWithType> parses text that represents a single JSON value into an instance of the type specified by the generic type parameter. Parsing should only succeed when the provided payload is valid for the specified generic type parameter. For a <xref:System.Char> value type, a valid payload is a single character string.
27+
28+
#### Recommended action
29+
30+
When parsing a string into a <xref:System.Char> type using <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=nameWithType>, make sure the string consists of a single character.
31+
32+
#### Category
33+
34+
Serialization
35+
36+
#### Affected APIs
37+
38+
- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=fullName>
39+
40+
<!--
41+
42+
#### Affected APIs
43+
44+
- `M:System.Text.Json.JsonSerializer.Deserialize``1(System.String,System.Text.Json.JsonSerializerOptions)`
45+
46+
-->

0 commit comments

Comments
 (0)