50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
namespace CloudGaming.Core.TextJson;
|
|
|
|
/// <summary>
|
|
/// Json时间转化器
|
|
/// </summary>
|
|
public class DateTimeJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
|
|
{
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var value = reader.GetString();
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
return DateTime.Parse(value);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Json时间转化器
|
|
/// </summary>
|
|
public class DateTimeNullJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime?>
|
|
{
|
|
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var value = reader.GetString();
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
return DateTime.Parse(value);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
|
{
|
|
if (value == null)
|
|
writer.WriteNullValue();
|
|
else
|
|
writer.WriteStringValue(value.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
}
|
|
|