using Newtonsoft.Json;
namespace DS.Module.Core;
public class JsonConverterLong : 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(long?)) && reader.Value == null)
{
return null;
}
else
{
long.TryParse(reader.Value != null ? reader.Value.ToString() : "", out long value);
return value;
}
}
///
/// 写json
///
///
///
///
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
writer.WriteValue(value);
else if ((Int64)value == 0)
{
writer.WriteValue("");
}
else
writer.WriteValue(value + "");
}
}