| File: System\Text\Json\Serialization\Converters\Value\DateOnlyConverter.cs | Web Access |
| Project: src\runtime\src\libraries\System.Text.Json\src\System.Text.Json.csproj (System.Text.Json) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; using System.Globalization; using System.Text.Json.Schema; namespace System.Text.Json.Serialization.Converters { internal sealed class DateOnlyConverter : JsonPrimitiveConverter<DateOnly> { public const int FormatLength = 10; // YYYY-MM-DD public const int MaxEscapedFormatLength = FormatLength * JsonConstants.MaxExpansionFactorWhileEscaping; public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.String) { ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); } return ReadCore(ref reader); } internal override DateOnly ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); return ReadCore(ref reader); } private static unsafe DateOnly ReadCore(ref Utf8JsonReader reader) { if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, FormatLength, MaxEscapedFormatLength)) { ThrowHelper.ThrowFormatException(DataType.DateOnly); } scoped ReadOnlySpan<byte> source; if (!reader.HasValueSequence && !reader.ValueIsEscaped) { source = reader.ValueSpan; } else { Span<byte> stackSpan = stackalloc byte[MaxEscapedFormatLength]; int bytesWritten = reader.CopyString(stackSpan); // CopyString can unescape which can change the length, so we need to perform the length check again. if (bytesWritten < FormatLength) { ThrowHelper.ThrowFormatException(DataType.DateOnly); } source = stackSpan.Slice(0, bytesWritten); } if (!JsonHelpers.TryParseAsIso(source, out DateOnly value)) { ThrowHelper.ThrowFormatException(DataType.DateOnly); } return value; } public override unsafe void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) { Span<byte> buffer = stackalloc byte[FormatLength]; bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture); Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); writer.WriteStringValue(buffer); } internal override unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { Span<byte> buffer = stackalloc byte[FormatLength]; bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture); Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); writer.WritePropertyName(buffer); } internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, Format = "date" }; internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; } }