<PackageReference Include="System.Text.Json" Version="7.0.4" />

VersionConverter

namespace System.Text.Json.Serialization.Converters { internal sealed class VersionConverter : JsonConverter<Version> { public unsafe override Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.String) ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, 3, 258)) ThrowHelper.ThrowFormatException(DataType.TimeSpan); Span<char> span = new Span<char>(stackalloc byte[516], 258); Span<char> destination = span; int length = reader.CopyString(destination); ReadOnlySpan<char> input = destination.Slice(0, length); if (!char.IsDigit(input[0]) || !char.IsDigit(input[input.Length - 1])) ThrowHelper.ThrowFormatException(DataType.Version); if (Version.TryParse(input, out Version result)) return result; ThrowHelper.ThrowJsonException(null); return null; } public unsafe override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options) { Span<char> span = new Span<char>(stackalloc byte[86], 43); Span<char> destination = span; int charsWritten; bool flag = value.TryFormat(destination, out charsWritten); writer.WriteStringValue(destination.Slice(0, charsWritten)); } } }