34 lines
952 B
C#
34 lines
952 B
C#
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiveForum.Code.JsonConverterExtend
|
|
{
|
|
// 自定义字符串转换器
|
|
public class NullToEmptyStringConverter : Newtonsoft.Json.JsonConverter
|
|
{
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(string);
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
if (reader.TokenType == JsonToken.Null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return reader.Value?.ToString() ?? string.Empty;
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
writer.WriteValue(value?.ToString() ?? string.Empty);
|
|
}
|
|
}
|
|
}
|