You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
# System.Text.Json requires single-char string to deserialize a char
7
7
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.
9
9
10
10
## Change description
11
11
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:
15
13
16
14
```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.
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:
<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.
32
34
33
35
## Recommended action
34
36
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`.
0 commit comments