Skip to content

Version deserialization breaking change #27809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/core/compatibility/7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ If you're migrating an app to .NET 7, the breaking changes listed here might aff
| Title | Binary compatible | Source compatible | Introduced |
| - | - | - | - |
| [Validate CompressionLevel for BrotliStream](core-libraries/7.0/compressionlevel-validation.md) | ❌ | ✔️ | Preview 1 |

## Serialization

| Title | Binary compatible | Source compatible | Introduced |
| - | - | - | - |
| [Deserialize Version type with leading or trailing whitespace](serialization/7.0/deserialize-version-with-whitespace.md) | ❌ | ✔️ | Preview 1 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: "Breaking change: Deserialize Version with leading/trailing whitespace"
description: Learn about the .NET 7 breaking change for deserializing Version types with leading or trailing whitespace.
ms.date: 01/11/2022
---
# Deserialize Version type with leading or trailing whitespace

<xref:System.Text.Json.JsonSerializer> now throws an exception while deserializing <xref:System.Version> types that have leading or trailing whitespace.

## Previous behavior

Prior to .NET 7, deserializing <xref:System.Version> types that have leading or trailing whitespace was permitted.

## New behavior

Started in .NET 7, <xref:System.Text.Json.JsonSerializer> throws a <xref:System.FormatException> when deserializing <xref:System.Version> types that have leading or trailing whitespace.

## Version introduced

.NET 7 Preview 1

## Type of breaking change

This change can affect [binary compatibility](../../categories.md#binary-compatibility).

## Reason for change

.NET has optimized the implementation of the underlying <xref:System.Version> converter. This resulted in the implementation being made to align with the behavior for other primitive types supported by <xref:System.Text.Json?displayProperty=fullName>, for example, <xref:System.DateTime> and <xref:System.Guid>, which also disallow leading and trailing spaces.

## Recommended action

To get the old behavior back, add a custom converter for the <xref:System.Version> type that permits whitespace:

```csharp
internal sealed class VersionConverter : JsonConverter<Version>
{
public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? versionString = reader.GetString();
if (Version.TryParse(versionString, out Version? result))
{
return result;
}

ThrowHelper.ThrowJsonException();
return null;
}

public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
```

## Affected APIs

- <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=fullName>
- <xref:System.Text.Json.JsonSerializer.DeserializeAsync%2A?displayProperty=fullName>
8 changes: 8 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ items:
items:
- name: Default serialization format for TimeSpan
href: core-libraries/7.0/compressionlevel-validation.md
- name: Serialization
items:
- name: Deserialize Version type with leading or trailing whitespace
href: serialization/7.0/deserialize-version-with-whitespace.md
- name: .NET 6
items:
- name: Overview
Expand Down Expand Up @@ -901,6 +905,10 @@ items:
href: core-libraries/5.0/utf-7-code-paths-obsolete.md
- name: Serialization
items:
- name: .NET 7
items:
- name: Deserialize Version type with leading or trailing whitespace
href: serialization/7.0/deserialize-version-with-whitespace.md
- name: .NET 6
items:
- name: Default serialization format for TimeSpan
Expand Down