Description
JsonSerializer goes from permitting leading and trailing whitespace when deserializing Version types to throwing FormatException. Breaking change introduced in .NET 7 Preview 1.
Version
Other (please put exact version in description textbox)
Previous behavior
Provided in description.
New behavior
Provided in description.
Type of breaking change
Reason for change
We optimized the implementation of the underlying Version converter. This resulted in the implementation being made to align with the behavior for other primitive types supported by System.Text.Json e.g. DateTime and Guid which also disallow leading and trailing spaces.
Recommended action
To get the old behavior back, add a custom converter for the type which permits whitespace:
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());
}
}
Feature area
Core .NET libraries
Affected APIs
All System.Text.Json.JsonSerializer.Deserialize method overloads.
Description
JsonSerializer goes from permitting leading and trailing whitespace when deserializing Version types to throwing
FormatException. Breaking change introduced in .NET 7 Preview 1.Version
Other (please put exact version in description textbox)
Previous behavior
Provided in description.
New behavior
Provided in description.
Type of breaking change
Reason for change
We optimized the implementation of the underlying
Versionconverter. This resulted in the implementation being made to align with the behavior for other primitive types supported bySystem.Text.Jsone.g.DateTimeandGuidwhich also disallow leading and trailing spaces.Recommended action
To get the old behavior back, add a custom converter for the type which permits whitespace:
Feature area
Core .NET libraries
Affected APIs
All
System.Text.Json.JsonSerializer.Deserializemethod overloads.