| | | 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 TimeOnlyConverter : JsonPrimitiveConverter<TimeOnly> |
| | | 12 | | { |
| | | 13 | | private const int MinimumTimeOnlyFormatLength = 3; // h:m |
| | | 14 | | private const int MaximumTimeOnlyFormatLength = 16; // hh:mm:ss.fffffff |
| | | 15 | | private const int MaximumEscapedTimeOnlyFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTi |
| | | 16 | | |
| | | 17 | | public override TimeOnly 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); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | internal override TimeOnly 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 TimeOnly 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, MinimumTimeOnlyFormatLength, MaximumEscapedTimeOnlyF |
| | 194 | 38 | | { |
| | 194 | 39 | | ThrowHelper.ThrowFormatException(DataType.TimeOnly); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | scoped ReadOnlySpan<byte> source; |
| | 84 | 43 | | if (!reader.HasValueSequence && !reader.ValueIsEscaped) |
| | 54 | 44 | | { |
| | 54 | 45 | | source = reader.ValueSpan; |
| | 54 | 46 | | } |
| | | 47 | | else |
| | 30 | 48 | | { |
| | 30 | 49 | | Span<byte> stackSpan = stackalloc byte[MaximumEscapedTimeOnlyFormatLength]; |
| | 30 | 50 | | int bytesWritten = reader.CopyString(stackSpan); |
| | 8 | 51 | | source = stackSpan.Slice(0, bytesWritten); |
| | 8 | 52 | | } |
| | | 53 | | |
| | 62 | 54 | | byte firstChar = source[0]; |
| | 62 | 55 | | int firstSeparator = source.IndexOfAny((byte)'.', (byte)':'); |
| | 62 | 56 | | if (!JsonHelpers.IsDigit(firstChar) || firstSeparator < 0 || source[firstSeparator] == (byte)'.') |
| | 62 | 57 | | { |
| | | 58 | | // Note: Utf8Parser.TryParse permits leading whitespace, negative values |
| | | 59 | | // and numbers of days so we need to exclude these cases here. |
| | 62 | 60 | | ThrowHelper.ThrowFormatException(DataType.TimeOnly); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | bool result = Utf8Parser.TryParse(source, out TimeSpan timespan, out int bytesConsumed, 'c'); |
| | | 64 | | |
| | | 65 | | // Note: Utf8Parser.TryParse will return true for invalid input so |
| | | 66 | | // long as it starts with an integer. Example: "2021-06-18" or |
| | | 67 | | // "1$$$$$$$$$$". We need to check bytesConsumed to know if the |
| | | 68 | | // entire source was actually valid. |
| | | 69 | | |
| | 0 | 70 | | if (!result || source.Length != bytesConsumed) |
| | 0 | 71 | | { |
| | 0 | 72 | | ThrowHelper.ThrowFormatException(DataType.TimeOnly); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | Debug.Assert(TimeOnly.MinValue.ToTimeSpan() <= timespan && timespan <= TimeOnly.MaxValue.ToTimeSpan()); |
| | 0 | 76 | | return TimeOnly.FromTimeSpan(timespan); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) |
| | 0 | 80 | | { |
| | 0 | 81 | | Span<byte> output = stackalloc byte[MaximumTimeOnlyFormatLength]; |
| | | 82 | | |
| | 0 | 83 | | bool result = Utf8Formatter.TryFormat(value.ToTimeSpan(), output, out int bytesWritten, 'c'); |
| | 0 | 84 | | Debug.Assert(result); |
| | | 85 | | |
| | 0 | 86 | | writer.WriteStringValue(output.Slice(0, bytesWritten)); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions opti |
| | 0 | 90 | | { |
| | 0 | 91 | | Span<byte> output = stackalloc byte[MaximumTimeOnlyFormatLength]; |
| | | 92 | | |
| | 0 | 93 | | bool result = Utf8Formatter.TryFormat(value.ToTimeSpan(), output, out int bytesWritten, 'c'); |
| | 0 | 94 | | Debug.Assert(result); |
| | | 95 | | |
| | 0 | 96 | | writer.WritePropertyName(output.Slice(0, bytesWritten)); |
| | 0 | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, Format = |
| | | 100 | | } |
| | | 101 | | } |