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.
41 lines
1018 B
41 lines
1018 B
3 weeks ago
|
using Newtonsoft.Json;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace Wood.Util
|
||
|
{
|
||
|
public class DateOnlyConverter:JsonConverter
|
||
|
{
|
||
|
public override bool CanConvert(Type objectType)
|
||
|
{
|
||
|
return objectType == typeof(DateTime) || objectType == typeof(DateTime?);
|
||
|
}
|
||
|
|
||
|
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||
|
{
|
||
|
if ((reader.Value == null || reader.Value.ToString() == "") && objectType.IsNullableType())
|
||
|
return null;
|
||
|
return Convert.ToDateTime(reader.Value).Date;
|
||
|
}
|
||
|
|
||
|
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||
|
{
|
||
|
if (value == null)
|
||
|
{
|
||
|
writer.WriteNull();
|
||
|
return;
|
||
|
}
|
||
|
DateTime? dt = value as DateTime?;
|
||
|
if (dt == null)
|
||
|
{
|
||
|
writer.WriteNull();
|
||
|
return;
|
||
|
}
|
||
|
writer.WriteValue(dt.Value.ToString("yyyy-MM-dd"));
|
||
|
}
|
||
|
}
|
||
|
}
|