26 lines
675 B
C#
26 lines
675 B
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Solar.Api.Converters;
|
|
|
|
public class DateOnlyConverter : JsonConverter<DateOnly>
|
|
{
|
|
private const string serializationFormat = "yyyy-MM-dd";
|
|
|
|
public override DateOnly Read(
|
|
ref Utf8JsonReader reader,
|
|
Type typeToConvert,
|
|
JsonSerializerOptions options
|
|
)
|
|
{
|
|
var value = reader.GetString();
|
|
return DateOnly.ParseExact(value!, serializationFormat);
|
|
}
|
|
|
|
public override void Write(
|
|
Utf8JsonWriter writer,
|
|
DateOnly value,
|
|
JsonSerializerOptions options
|
|
) => writer.WriteStringValue(value.ToString(serializationFormat));
|
|
}
|