| | | 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.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Text.Json.Nodes; |
| | | 7 | | using System.Text.Json.Schema; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | internal sealed class VersionConverter : JsonPrimitiveConverter<Version?> |
| | | 12 | | { |
| | | 13 | | #if NET |
| | | 14 | | private const int MaximumFormattedVersionLength = 43; // 2147483647.2147483647.2147483647.2147483647 |
| | | 15 | | #endif |
| | | 16 | | |
| | | 17 | | public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 0 | 18 | | { |
| | 0 | 19 | | if (reader.TokenType is JsonTokenType.Null) |
| | 0 | 20 | | { |
| | 0 | 21 | | return null; |
| | | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | if (reader.TokenType != JsonTokenType.String) |
| | 0 | 25 | | { |
| | 0 | 26 | | ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | return ReadCore(ref reader); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private static unsafe Version ReadCore(ref Utf8JsonReader reader) |
| | 0 | 33 | | { |
| | 0 | 34 | | Debug.Assert(reader.TokenType is JsonTokenType.PropertyName or JsonTokenType.String); |
| | | 35 | | |
| | | 36 | | #if NET |
| | 0 | 37 | | int bufferLength = reader.ValueLength; |
| | 0 | 38 | | byte[]? rentedBuffer = null; |
| | | 39 | | try |
| | 0 | 40 | | { |
| | 0 | 41 | | Span<byte> utf8Buffer = bufferLength <= JsonConstants.StackallocByteThreshold |
| | 0 | 42 | | ? stackalloc byte[JsonConstants.StackallocByteThreshold] |
| | 0 | 43 | | : (rentedBuffer = ArrayPool<byte>.Shared.Rent(bufferLength)); |
| | | 44 | | |
| | 0 | 45 | | int bytesWritten = reader.CopyString(utf8Buffer); |
| | 0 | 46 | | ReadOnlySpan<byte> utf8Source = utf8Buffer.Slice(0, bytesWritten); |
| | | 47 | | |
| | | 48 | | // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters |
| | | 49 | | // we need to make sure that our input doesn't have them, |
| | | 50 | | // and if it has - we need to throw, to match behaviour of other converters |
| | | 51 | | // since Version.TryParse allows them and silently parses input to Version |
| | 0 | 52 | | if (utf8Source.IsEmpty || IsWhiteSpaceByte(utf8Source[0]) || IsWhiteSpaceByte(utf8Source[^1])) |
| | 0 | 53 | | { |
| | 0 | 54 | | ThrowHelper.ThrowFormatException(DataType.Version); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | if (Version.TryParse(utf8Source, out Version? result)) |
| | 0 | 58 | | { |
| | 0 | 59 | | return result!; |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | finally |
| | 0 | 63 | | { |
| | 0 | 64 | | if (rentedBuffer is not null) |
| | 0 | 65 | | { |
| | 0 | 66 | | ArrayPool<byte>.Shared.Return(rentedBuffer); |
| | 0 | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | #else |
| | | 70 | | string? versionString = reader.GetString(); |
| | | 71 | | if (string.IsNullOrEmpty(versionString) || char.IsWhiteSpace(versionString[0]) || char.IsWhiteSpace(versionS |
| | | 72 | | { |
| | | 73 | | // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters |
| | | 74 | | // we need to make sure that our input doesn't have them, |
| | | 75 | | // and if it has - we need to throw, to match behaviour of other converters |
| | | 76 | | // since Version.TryParse allows them and silently parses input to Version |
| | | 77 | | ThrowHelper.ThrowFormatException(DataType.Version); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | if (Version.TryParse(versionString, out Version? result)) |
| | | 81 | | { |
| | | 82 | | return result; |
| | | 83 | | } |
| | | 84 | | #endif |
| | 0 | 85 | | ThrowHelper.ThrowJsonException(); |
| | | 86 | | return null; |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | #if NET |
| | | 90 | | private static bool IsWhiteSpaceByte(byte b) |
| | 0 | 91 | | { |
| | | 92 | | // Check for ASCII whitespace characters: space (0x20), tab (0x09), newline (0x0A), |
| | | 93 | | // carriage return (0x0D), form feed (0x0C), and vertical tab (0x0B) |
| | | 94 | | // For non-ASCII bytes, convert to char and check with char.IsWhiteSpace |
| | 0 | 95 | | return b <= 127 ? char.IsWhiteSpace((char)b) : false; |
| | 0 | 96 | | } |
| | | 97 | | #endif |
| | | 98 | | |
| | | 99 | | public override unsafe void Write(Utf8JsonWriter writer, Version? value, JsonSerializerOptions options) |
| | 0 | 100 | | { |
| | 0 | 101 | | if (value is null) |
| | 0 | 102 | | { |
| | 0 | 103 | | writer.WriteNullValue(); |
| | 0 | 104 | | return; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | #if NET |
| | 0 | 108 | | Span<byte> span = stackalloc byte[MaximumFormattedVersionLength]; |
| | 0 | 109 | | bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); |
| | 0 | 110 | | Debug.Assert(formattedSuccessfully); |
| | 0 | 111 | | writer.WriteStringValue(span.Slice(0, charsWritten)); |
| | | 112 | | #else |
| | | 113 | | writer.WriteStringValue(value.ToString()); |
| | | 114 | | #endif |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | internal override Version ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOp |
| | 0 | 118 | | { |
| | 0 | 119 | | return ReadCore(ref reader); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal override unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, Version value, JsonSerializerOption |
| | 0 | 123 | | { |
| | 0 | 124 | | ArgumentNullException.ThrowIfNull(value); |
| | | 125 | | |
| | | 126 | | #if NET |
| | 0 | 127 | | Span<byte> span = stackalloc byte[MaximumFormattedVersionLength]; |
| | 0 | 128 | | bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); |
| | 0 | 129 | | Debug.Assert(formattedSuccessfully); |
| | 0 | 130 | | writer.WritePropertyName(span.Slice(0, charsWritten)); |
| | | 131 | | #else |
| | | 132 | | writer.WritePropertyName(value.ToString()); |
| | | 133 | | #endif |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => |
| | 0 | 137 | | new() |
| | 0 | 138 | | { |
| | 0 | 139 | | Type = JsonSchemaType.String, |
| | 0 | 140 | | Comment = "Represents a version string.", |
| | 0 | 141 | | Pattern = @"^\d+(\.\d+){1,3}$", |
| | 0 | 142 | | }; |
| | | 143 | | |
| | 0 | 144 | | internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |