1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System.Globalization;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- namespace OnlineMetodist.API.Services
- {
- //public class JsonDateOnlyConverter : JsonConverter<DateOnly>
- //{
- // // Define the date format the data is in
- // private const string DateFormat = "yyyy-MM-dd";
- // // This is the deserializer
- // public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- // {
- // return DateOnly.ParseExact(reader.GetString()!,
- // DateFormat);
- // }
- // // This is the serializer
- // public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
- // {
- // writer.WriteStringValue(value.ToString(
- // DateFormat, CultureInfo.InvariantCulture));
- // }
- //}
- public class DateOnlyJsonConverter : JsonConverter<DateOnly>
- {
- private const string Format = "yyyy-MM-dd";
- public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- return DateOnly.ParseExact(reader.GetString()!, Format, CultureInfo.InvariantCulture);
- }
- public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
- {
- writer.WriteStringValue(value.ToString(Format, CultureInfo.InvariantCulture));
- }
- }
- }
|