Skip to content

Deserializing Version edge case in JSON #118201

@lilinus

Description

@lilinus

Description

Deserializing Version does not allow leading nor trailing whitespace bug. But there are some wierd behaviours since the "components" of the version are parsed with NumberStyles.Integer:

  • + plus sign is allowed for second/third/fourth components.
  • Arbitrary amount of whitespace is allowed between the components and the . dot-separateor.

Reproduction Steps

using System.Text.Json;
JsonSerializer.Deserialize<Version>($"\"+1.1\""); // Throws 
JsonSerializer.Deserialize<Version>($"\"1.+1\""); // Success
JsonSerializer.Deserialize<Version>($"\" 1.1\""); // Throws
JsonSerializer.Deserialize<Version>($"\"1 .1\""); // Success
JsonSerializer.Deserialize<Version>($"\"1. 1\""); // Success
JsonSerializer.Deserialize<Version>($"\"1.1 \""); // Throws 
JsonSerializer.Deserialize<Version>($"\"1 . +1\""); // Success
JsonSerializer.Deserialize<Version>($"\"1.{new string(' ', 255)}1\""); // Success
JsonSerializer.Deserialize<Version>($"\"1.{new string(' ', 256)}1\""); // Throws - string is too long (>258 chars)

Expected behavior

Not sure, but given the other JSON converters are a bit "strict" perhaps all of the above should throw?

Actual behavior

See repro steps

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions