34 lines
804 B
C#
34 lines
804 B
C#
using System.Text.Json;
|
|
|
|
namespace Atomx.Utils.Json.Converts
|
|
{
|
|
public class DateTimeJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
|
|
{
|
|
readonly string _dateFormatString;
|
|
public DateTimeJsonConverter()
|
|
{
|
|
_dateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
}
|
|
|
|
public DateTimeJsonConverter(string dateFormatString)
|
|
{
|
|
_dateFormatString = dateFormatString;
|
|
}
|
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var text = reader.GetString();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return default;
|
|
}
|
|
return DateTime.Parse(text);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString("s"));
|
|
}
|
|
}
|
|
}
|