19 lines
724 B
C#
19 lines
724 B
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace NejCommon.Utils
|
|
{
|
|
public class DateOnlyConverter : JsonConverter<DateOnly>
|
|
{
|
|
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
return DateOnly.TryParse(reader.GetString() ?? string.Empty, CultureInfo.InvariantCulture, out var date) ? date : throw new JsonException("Invalid DateOnly format, expected 'yyyy-MM-dd'.");
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
|
|
}
|
|
}
|
|
} |