| | | 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.Text; |
| | | 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 TimeSpanConverter : JsonPrimitiveConverter<TimeSpan> |
| | | 12 | | { |
| | | 13 | | private const int MinimumTimeSpanFormatLength = 1; // d |
| | | 14 | | private const int MaximumTimeSpanFormatLength = 26; // -dddddddd.hh:mm:ss.fffffff |
| | | 15 | | private const int MaximumEscapedTimeSpanFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTi |
| | | 16 | | |
| | | 17 | | public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 634 | 18 | | { |
| | 634 | 19 | | if (reader.TokenType != JsonTokenType.String) |
| | 356 | 20 | | { |
| | 356 | 21 | | ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); |
| | | 22 | | } |
| | | 23 | | |
| | 278 | 24 | | return ReadCore(ref reader); |
| | 22 | 25 | | } |
| | | 26 | | |
| | | 27 | | internal override TimeSpan ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerO |
| | 0 | 28 | | { |
| | 0 | 29 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 30 | | return ReadCore(ref reader); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static TimeSpan ReadCore(ref Utf8JsonReader reader) |
| | 278 | 34 | | { |
| | 278 | 35 | | Debug.Assert(reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName); |
| | | 36 | | |
| | 278 | 37 | | if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumTimeSpanFormatLength, MaximumEscapedTimeSpanF |
| | 120 | 38 | | { |
| | 120 | 39 | | ThrowHelper.ThrowFormatException(DataType.TimeSpan); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | scoped ReadOnlySpan<byte> source; |
| | 158 | 43 | | if (!reader.HasValueSequence && !reader.ValueIsEscaped) |
| | 106 | 44 | | { |
| | 106 | 45 | | source = reader.ValueSpan; |
| | 106 | 46 | | } |
| | | 47 | | else |
| | 52 | 48 | | { |
| | 52 | 49 | | Span<byte> stackSpan = stackalloc byte[MaximumEscapedTimeSpanFormatLength]; |
| | 52 | 50 | | int bytesWritten = reader.CopyString(stackSpan); |
| | 12 | 51 | | source = stackSpan.Slice(0, bytesWritten); |
| | 12 | 52 | | } |
| | | 53 | | |
| | 118 | 54 | | byte firstChar = source[0]; |
| | 118 | 55 | | if (!JsonHelpers.IsDigit(firstChar) && firstChar != '-') |
| | 72 | 56 | | { |
| | | 57 | | // Note: Utf8Parser.TryParse allows for leading whitespace so we |
| | | 58 | | // need to exclude that case here. |
| | 72 | 59 | | ThrowHelper.ThrowFormatException(DataType.TimeSpan); |
| | | 60 | | } |
| | | 61 | | |
| | 46 | 62 | | bool result = Utf8Parser.TryParse(source, out TimeSpan tmpValue, out int bytesConsumed, 'c'); |
| | | 63 | | |
| | | 64 | | // Note: Utf8Parser.TryParse will return true for invalid input so |
| | | 65 | | // long as it starts with an integer. Example: "2021-06-18" or |
| | | 66 | | // "1$$$$$$$$$$". We need to check bytesConsumed to know if the |
| | | 67 | | // entire source was actually valid. |
| | | 68 | | |
| | 46 | 69 | | if (!result || source.Length != bytesConsumed) |
| | 24 | 70 | | { |
| | 24 | 71 | | ThrowHelper.ThrowFormatException(DataType.TimeSpan); |
| | | 72 | | } |
| | | 73 | | |
| | 22 | 74 | | return tmpValue; |
| | 22 | 75 | | } |
| | | 76 | | |
| | | 77 | | public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) |
| | 0 | 78 | | { |
| | 0 | 79 | | Span<byte> output = stackalloc byte[MaximumTimeSpanFormatLength]; |
| | | 80 | | |
| | 0 | 81 | | bool result = Utf8Formatter.TryFormat(value, output, out int bytesWritten, 'c'); |
| | 0 | 82 | | Debug.Assert(result); |
| | | 83 | | |
| | 0 | 84 | | writer.WriteStringValue(output.Slice(0, bytesWritten)); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions opti |
| | 0 | 88 | | { |
| | 0 | 89 | | Span<byte> output = stackalloc byte[MaximumTimeSpanFormatLength]; |
| | | 90 | | |
| | 0 | 91 | | bool result = Utf8Formatter.TryFormat(value, output, out int bytesWritten, 'c'); |
| | 0 | 92 | | Debug.Assert(result); |
| | | 93 | | |
| | 0 | 94 | | writer.WritePropertyName(output.Slice(0, bytesWritten)); |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() |
| | 0 | 98 | | { |
| | 0 | 99 | | Type = JsonSchemaType.String, |
| | 0 | 100 | | Comment = "Represents a System.TimeSpan value.", |
| | 0 | 101 | | Pattern = @"^-?(\d+\.)?\d{2}:\d{2}:\d{2}(\.\d{1,7})?$" |
| | 0 | 102 | | }; |
| | | 103 | | } |
| | | 104 | | } |