using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace DS.Module.Core.Helpers
{
///
/// 时间转换
///
public class DatetimeJsonConverter : JsonConverter
{
///
/// 是否可以转换
///
///
///
public override bool CanConvert(Type objectType)
{
return true;
}
///
/// 读json
///
///
///
///
///
///
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if ((reader.ValueType == null || reader.ValueType == typeof(DateTime?)) && reader.Value == null)
{
return null;
}
else
{
DateTime.TryParse(reader.Value != null ? reader.Value.ToString() : "", out DateTime value);
return value;
}
}
///
/// 写json
///
///
///
///
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
writer.WriteValue(value);
else if (value != null)
{
var years = DateTime.Parse(value.ToString()).Year;
if (years == 1900|| years == 2000 || years == 1901 || years == 1970)
{
writer.WriteValue("");
}
else
writer.WriteValue(value);
}
}
}
}