Skip to content

Commit ec0a3f7

Browse files
tdykstragewarren
andauthored
Clarify scope of breaking change (#22012)
Co-authored-by: Genevieve Warren <[email protected]>
1 parent c9ddd2c commit ec0a3f7

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

docs/core/compatibility/serialization/5.0/deserializing-json-into-char-requires-single-character.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
---
2-
title: "Breaking change: Deserialize requires single-character string"
3-
description: Learn about the breaking change in .NET 5.0 where JsonSerializer.Deserialize requires a single-character string.
4-
ms.date: 10/18/2020
2+
title: "Breaking change: Deserialize char requires single-character string"
3+
description: Learn about the breaking change in .NET 5.0 where System.Text.Json requires a single-char string in the JSON when deserializing to a char target.
4+
ms.date: 12/15/2020
55
---
6-
# JsonSerializer.Deserialize requires single-character string
6+
# System.Text.Json requires single-char string to deserialize a char
77

8-
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.
8+
To successfully deserialize a <xref:System.Char> using <xref:System.Text.Json>, the JSON string must contain a single character.
99

1010
## Change description
1111

12-
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.
13-
14-
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.
12+
In previous .NET versions, a multi-`char` string in the JSON is successfully deserialized to a `char` property or field. Only the first `char` of the string is used, as in the following example:
1513

1614
```csharp
17-
// .NET Core 3.0 and 3.1: Returns the first character 'a'.
18-
// .NET 5.0 and later: Throws JsonException because payload has more than one character.
19-
JsonSerializer.Deserialize<char>("\"abc\"");
15+
// .NET Core 3.0 and 3.1: Returns the first char 'a'.
16+
// .NET 5.0 and later: Throws JsonException because payload has more than one char.
17+
char deserializedChar = JsonSerializer.Deserialize<char>("\"abc\"");
18+
```
2019

20+
In .NET 5.0 and later, anything other than a single-`char` string causes a <xref:System.Text.Json.JsonException> to be thrown when the deserialization target is a `char`. The following example string is successfully deserialized in all .NET versions:
21+
22+
```csharp
2123
// Correct usage.
22-
JsonSerializer.Deserialize<char>("\"a\"");
24+
char deserializedChar = JsonSerializer.Deserialize<char>("\"a\"");
2325
```
2426

2527
## Version introduced
@@ -28,21 +30,21 @@ JsonSerializer.Deserialize<char>("\"a\"");
2830

2931
## Reason for change
3032

31-
<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.
33+
Parsing for deserialization should only succeed when the provided payload is valid for the target type. For a `char` type, the only valid payload is a single-`char` string.
3234

3335
## Recommended action
3436

35-
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.
37+
When you deserialize JSON into a `char` target, make sure the string consists of a single `char`.
3638

3739
## Affected APIs
3840

39-
- <xref:System.Text.Json.JsonSerializer.Deserialize%60%601(System.String,System.Text.Json.JsonSerializerOptions)?displayProperty=fullName>
41+
- <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=fullName>
4042

4143
<!--
4244
4345
### Affected APIs
4446
45-
- `M:System.Text.Json.JsonSerializer.Deserialize``1(System.String,System.Text.Json.JsonSerializerOptions)`
47+
- `Overload:System.Text.Json.JsonSerializer.Deserialize`
4648
4749
### Category
4850

0 commit comments

Comments
 (0)