|
| 1 | +#if NET6_0 |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Serialization; |
| 4 | +#endif |
| 5 | + |
| 6 | +#if NET6_0 || NET7_0 |
| 7 | +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
| 8 | +#endif |
| 9 | + |
| 10 | +#if NET6_0 |
| 11 | +// TODO: Remove after migrate to net7+ |
| 12 | +// https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-7/ |
| 13 | +// https://stackoverflow.com/questions/74246482/ |
| 14 | +public sealed class Net6DateOnlyJsonConverter : JsonConverter<DateOnly> { |
| 15 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => DateOnly.FromDateTime(reader.GetDateTime()); |
| 16 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString("O")); |
| 17 | +} |
| 18 | +public sealed class Net6TimeOnlyJsonConverter : JsonConverter<TimeOnly> { |
| 19 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => TimeOnly.Parse(reader.GetString()); |
| 20 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString("HH:mm:ss.fff")); |
| 21 | +} |
| 22 | +#endif |
| 23 | + |
| 24 | +#if NET6_0 || NET7_0 |
| 25 | +public sealed class NetLess8DateOnlyValueConverter : ValueConverter<DateOnly, DateTime> { |
| 26 | + public NetLess8DateOnlyValueConverter() : base( |
| 27 | + dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue), |
| 28 | + dateTime => DateOnly.FromDateTime(dateTime)) { } |
| 29 | +} |
| 30 | + |
| 31 | +public sealed class NetLess8TimeOnlyValueConverter : ValueConverter<TimeOnly, TimeSpan> { |
| 32 | + public NetLess8TimeOnlyValueConverter() : base( |
| 33 | + timeOnly => timeOnly.ToTimeSpan(), |
| 34 | + timeSpan => TimeOnly.FromTimeSpan(timeSpan)) { } |
| 35 | +} |
| 36 | +#endif |
0 commit comments