119 lines
3.8 KiB
C#
119 lines
3.8 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace YLErp.Serialization
|
|
{
|
|
public class CustomDateTimeConverter : JsonConverter<DateTime>
|
|
{
|
|
private readonly string Format;
|
|
|
|
public CustomDateTimeConverter(string format = "yyyy-MM-dd HH:mm:ss")
|
|
{
|
|
Format = format;
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime date, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(date.ToString(Format));
|
|
}
|
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
{
|
|
return DateTime.MinValue;
|
|
}
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
var str = reader.GetString();
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
return DateTime.MinValue;
|
|
}
|
|
if (DateTime.TryParse(str, out var dt))
|
|
{
|
|
return dt;
|
|
}
|
|
}
|
|
throw new JsonException("解析为日期对象失败:");
|
|
}
|
|
}
|
|
|
|
public class CustomJsonConverterForNullableDateTime : JsonConverter<DateTime?>
|
|
{
|
|
private readonly string Format;
|
|
|
|
public CustomJsonConverterForNullableDateTime(string format = "yyyy-MM-dd HH:mm:ss")
|
|
{
|
|
Format = format;
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime? date, JsonSerializerOptions options)
|
|
{
|
|
if (date == null)
|
|
{
|
|
writer.WriteStringValue("");
|
|
}
|
|
else
|
|
{
|
|
writer.WriteStringValue(date.Value.ToString(Format));
|
|
}
|
|
}
|
|
|
|
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
{
|
|
return null;
|
|
}
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
var str = reader.GetString();
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
return null;
|
|
}
|
|
if (DateTime.TryParse(str, out var dt))
|
|
{
|
|
return dt;
|
|
}
|
|
}
|
|
throw new JsonException("解析为日期对象失败:");
|
|
}
|
|
}
|
|
|
|
//public class CustomDateOnlyConverter : JsonConverter<DateOnly>
|
|
//{
|
|
// private readonly string Format;
|
|
// public CustomDateOnlyConverter(string format)
|
|
// {
|
|
// Format = format;
|
|
// }
|
|
// public override void Write(Utf8JsonWriter writer, DateOnly date, JsonSerializerOptions options)
|
|
// {
|
|
// writer.WriteStringValue(date.ToString(Format));
|
|
// }
|
|
// public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
// {
|
|
// return DateOnly.ParseExact(reader.GetString(), Format);
|
|
// }
|
|
//}
|
|
|
|
//public class CustomTimeOnlyConverter : JsonConverter<TimeOnly>
|
|
//{
|
|
// private readonly string Format;
|
|
// public CustomTimeOnlyConverter(string format)
|
|
// {
|
|
// Format = format;
|
|
// }
|
|
// public override void Write(Utf8JsonWriter writer, TimeOnly date, JsonSerializerOptions options)
|
|
// {
|
|
// writer.WriteStringValue(date.ToString(Format));
|
|
// }
|
|
// public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
// {
|
|
// return TimeOnly.ParseExact(reader.GetString(), Format);
|
|
// }
|
|
//}
|
|
}
|