22 lines
790 B
C#
22 lines
790 B
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace MauiApp.Core.Converter;
|
|
|
|
public class DateTimeConverter : JsonConverter<DateTime>
|
|
{
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var dateString = reader.GetString();
|
|
|
|
if (dateString == null) throw new ArgumentNullException($"Impossibile deserializzare una data NON-Nullable da un NULL");
|
|
|
|
return DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss"));
|
|
}
|
|
} |