diff --git a/docs/core/compatibility/7.0.md b/docs/core/compatibility/7.0.md index dce701b09f38d..bb584117d0ebf 100644 --- a/docs/core/compatibility/7.0.md +++ b/docs/core/compatibility/7.0.md @@ -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 | diff --git a/docs/core/compatibility/serialization/7.0/deserialize-version-with-whitespace.md b/docs/core/compatibility/serialization/7.0/deserialize-version-with-whitespace.md new file mode 100644 index 0000000000000..d04de204bc3ae --- /dev/null +++ b/docs/core/compatibility/serialization/7.0/deserialize-version-with-whitespace.md @@ -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 + + now throws an exception while deserializing types that have leading or trailing whitespace. + +## Previous behavior + +Prior to .NET 7, deserializing types that have leading or trailing whitespace was permitted. + +## New behavior + +Started in .NET 7, throws a when deserializing 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 converter. This resulted in the implementation being made to align with the behavior for other primitive types supported by , for example, and , which also disallow leading and trailing spaces. + +## Recommended action + +To get the old behavior back, add a custom converter for the type that permits whitespace: + +```csharp +internal sealed class VersionConverter : JsonConverter +{ + 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 + +- +- diff --git a/docs/core/compatibility/toc.yml b/docs/core/compatibility/toc.yml index d432d94a4a690..480262466ce42 100644 --- a/docs/core/compatibility/toc.yml +++ b/docs/core/compatibility/toc.yml @@ -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 @@ -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