| | | 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.Globalization; |
| | | 6 | | using System.Text.Json.Schema; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | internal sealed class DateOnlyConverter : JsonPrimitiveConverter<DateOnly> |
| | | 11 | | { |
| | | 12 | | public const int FormatLength = 10; // YYYY-MM-DD |
| | | 13 | | public const int MaxEscapedFormatLength = FormatLength * JsonConstants.MaxExpansionFactorWhileEscaping; |
| | | 14 | | |
| | | 15 | | public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 634 | 16 | | { |
| | 634 | 17 | | if (reader.TokenType != JsonTokenType.String) |
| | 356 | 18 | | { |
| | 356 | 19 | | ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); |
| | | 20 | | } |
| | | 21 | | |
| | 278 | 22 | | return ReadCore(ref reader); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal override DateOnly ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerO |
| | 0 | 26 | | { |
| | 0 | 27 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 28 | | return ReadCore(ref reader); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static DateOnly ReadCore(ref Utf8JsonReader reader) |
| | 278 | 32 | | { |
| | 278 | 33 | | if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, FormatLength, MaxEscapedFormatLength)) |
| | 260 | 34 | | { |
| | 260 | 35 | | ThrowHelper.ThrowFormatException(DataType.DateOnly); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | scoped ReadOnlySpan<byte> source; |
| | 18 | 39 | | if (!reader.HasValueSequence && !reader.ValueIsEscaped) |
| | 11 | 40 | | { |
| | 11 | 41 | | source = reader.ValueSpan; |
| | 11 | 42 | | } |
| | | 43 | | else |
| | 7 | 44 | | { |
| | 7 | 45 | | Span<byte> stackSpan = stackalloc byte[MaxEscapedFormatLength]; |
| | 7 | 46 | | int bytesWritten = reader.CopyString(stackSpan); |
| | | 47 | | |
| | | 48 | | // CopyString can unescape which can change the length, so we need to perform the length check again. |
| | 1 | 49 | | if (bytesWritten < FormatLength) |
| | 0 | 50 | | { |
| | 0 | 51 | | ThrowHelper.ThrowFormatException(DataType.DateOnly); |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | source = stackSpan.Slice(0, bytesWritten); |
| | 1 | 55 | | } |
| | | 56 | | |
| | 12 | 57 | | if (!JsonHelpers.TryParseAsIso(source, out DateOnly value)) |
| | 12 | 58 | | { |
| | 12 | 59 | | ThrowHelper.ThrowFormatException(DataType.DateOnly); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return value; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) |
| | 0 | 66 | | { |
| | 0 | 67 | | Span<byte> buffer = stackalloc byte[FormatLength]; |
| | 0 | 68 | | bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture |
| | 0 | 69 | | Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); |
| | 0 | 70 | | writer.WriteStringValue(buffer); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions opti |
| | 0 | 74 | | { |
| | 0 | 75 | | Span<byte> buffer = stackalloc byte[FormatLength]; |
| | 0 | 76 | | bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture |
| | 0 | 77 | | Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); |
| | 0 | 78 | | writer.WritePropertyName(buffer); |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, Format = |
| | | 82 | | } |
| | | 83 | | } |