| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | using System.Text.Json.Schema; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | internal sealed class VersionConverter : JsonPrimitiveConverter<Version?> |
| | | 11 | | { |
| | | 12 | | #if NET |
| | | 13 | | private const int MinimumVersionLength = 3; // 0.0 |
| | | 14 | | |
| | | 15 | | private const int MaximumVersionLength = 43; // 2147483647.2147483647.2147483647.2147483647 |
| | | 16 | | |
| | | 17 | | private const int MaximumEscapedVersionLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumVersionLe |
| | | 18 | | #endif |
| | | 19 | | |
| | | 20 | | public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 634 | 21 | | { |
| | 634 | 22 | | if (reader.TokenType is JsonTokenType.Null) |
| | 0 | 23 | | { |
| | 0 | 24 | | return null; |
| | | 25 | | } |
| | | 26 | | |
| | 634 | 27 | | if (reader.TokenType != JsonTokenType.String) |
| | 356 | 28 | | { |
| | 356 | 29 | | ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); |
| | | 30 | | } |
| | | 31 | | |
| | 278 | 32 | | return ReadCore(ref reader); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static Version ReadCore(ref Utf8JsonReader reader) |
| | 278 | 36 | | { |
| | 278 | 37 | | Debug.Assert(reader.TokenType is JsonTokenType.PropertyName or JsonTokenType.String); |
| | | 38 | | |
| | | 39 | | #if NET |
| | 278 | 40 | | if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumVersionLength, MaximumEscapedVersionLength)) |
| | 194 | 41 | | { |
| | 194 | 42 | | ThrowHelper.ThrowFormatException(DataType.Version); |
| | | 43 | | } |
| | | 44 | | |
| | 84 | 45 | | Span<char> charBuffer = stackalloc char[MaximumEscapedVersionLength]; |
| | 84 | 46 | | int bytesWritten = reader.CopyString(charBuffer); |
| | 20 | 47 | | ReadOnlySpan<char> source = charBuffer.Slice(0, bytesWritten); |
| | | 48 | | |
| | 20 | 49 | | if (!char.IsDigit(source[0]) || !char.IsDigit(source[^1])) |
| | 14 | 50 | | { |
| | | 51 | | // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters |
| | | 52 | | // we need to make sure that our input doesn't have them, |
| | | 53 | | // and if it has - we need to throw, to match behaviour of other converters |
| | | 54 | | // since Version.TryParse allows them and silently parses input to Version |
| | 14 | 55 | | ThrowHelper.ThrowFormatException(DataType.Version); |
| | | 56 | | } |
| | | 57 | | |
| | 6 | 58 | | if (Version.TryParse(source, out Version? result)) |
| | 0 | 59 | | { |
| | 0 | 60 | | return result; |
| | | 61 | | } |
| | | 62 | | #else |
| | | 63 | | string? versionString = reader.GetString(); |
| | | 64 | | if (!string.IsNullOrEmpty(versionString) && (!char.IsDigit(versionString[0]) || !char.IsDigit(versionString[ |
| | | 65 | | { |
| | | 66 | | // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters |
| | | 67 | | // we need to make sure that our input doesn't have them, |
| | | 68 | | // and if it has - we need to throw, to match behaviour of other converters |
| | | 69 | | // since Version.TryParse allows them and silently parses input to Version |
| | | 70 | | ThrowHelper.ThrowFormatException(DataType.Version); |
| | | 71 | | } |
| | | 72 | | if (Version.TryParse(versionString, out Version? result)) |
| | | 73 | | { |
| | | 74 | | return result; |
| | | 75 | | } |
| | | 76 | | #endif |
| | 6 | 77 | | ThrowHelper.ThrowJsonException(); |
| | | 78 | | return null; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public override void Write(Utf8JsonWriter writer, Version? value, JsonSerializerOptions options) |
| | 0 | 82 | | { |
| | 0 | 83 | | if (value is null) |
| | 0 | 84 | | { |
| | 0 | 85 | | writer.WriteNullValue(); |
| | 0 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | #if NET |
| | 0 | 90 | | Span<byte> span = stackalloc byte[MaximumVersionLength]; |
| | 0 | 91 | | bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); |
| | 0 | 92 | | Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength); |
| | 0 | 93 | | writer.WriteStringValue(span.Slice(0, charsWritten)); |
| | | 94 | | #else |
| | | 95 | | writer.WriteStringValue(value.ToString()); |
| | | 96 | | #endif |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | internal override Version ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOp |
| | 0 | 100 | | { |
| | 0 | 101 | | return ReadCore(ref reader); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Version value, JsonSerializerOptions optio |
| | 0 | 105 | | { |
| | 0 | 106 | | ArgumentNullException.ThrowIfNull(value); |
| | | 107 | | |
| | | 108 | | #if NET |
| | 0 | 109 | | Span<byte> span = stackalloc byte[MaximumVersionLength]; |
| | 0 | 110 | | bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); |
| | 0 | 111 | | Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength); |
| | 0 | 112 | | writer.WritePropertyName(span.Slice(0, charsWritten)); |
| | | 113 | | #else |
| | | 114 | | writer.WritePropertyName(value.ToString()); |
| | | 115 | | #endif |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => |
| | 0 | 119 | | new() |
| | 0 | 120 | | { |
| | 0 | 121 | | Type = JsonSchemaType.String, |
| | 0 | 122 | | Comment = "Represents a version string.", |
| | 0 | 123 | | Pattern = @"^\d+(\.\d+){1,3}$", |
| | 0 | 124 | | }; |
| | | 125 | | } |
| | | 126 | | } |