24 lines
818 B
C#
24 lines
818 B
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Integry_Smart_Warehouse.Core.WebHost.Converter;
|
|
|
|
public class JsonDateConverter : JsonConverter<DateTime>
|
|
{
|
|
|
|
private const string DateFormat = "dd/MM/yyyy HH:mm:ss";
|
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
|
var stringValue = jsonDoc.RootElement.GetRawText().Trim('"').Trim('\'');
|
|
var value = DateTime.ParseExact(stringValue, DateFormat, CultureInfo.InvariantCulture);
|
|
return value;
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString(DateFormat, CultureInfo.InvariantCulture));
|
|
}
|
|
} |