You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.3 KiB
74 lines
2.3 KiB
using System;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Znyc.Cloudcar.Admin.Commons.Helpers
|
|
{
|
|
/// <summary>
|
|
/// 时间类型格式数据处理
|
|
/// </summary>
|
|
public class DateTimeJsonConverter : JsonConverter<DateTime>
|
|
{
|
|
/// <summary>
|
|
/// 时间格式
|
|
/// </summary>
|
|
private readonly string _dateFormatString;
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
public DateTimeJsonConverter()
|
|
{
|
|
_dateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
}
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="dateFormatString">时间格式</param>
|
|
public DateTimeJsonConverter(string dateFormatString)
|
|
{
|
|
_dateFormatString = dateFormatString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="reader"></param>
|
|
/// <param name="typeToConvert"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
if (DateTime.TryParse(reader.GetString(), out DateTime date))
|
|
{
|
|
return date;
|
|
}
|
|
}
|
|
|
|
return reader.GetDateTime();
|
|
}
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="writer"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="options"></param>
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
}
|
|
|
|
public class DateTimeNullableConverter : JsonConverter<DateTime?>
|
|
{
|
|
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
return string.IsNullOrEmpty(reader.GetString()) ? default(DateTime?) : DateTime.Parse(reader.GetString());
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
}
|
|
}
|