You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB
C#

using Fizzler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace DS.WMS.PrintApi.Utils
{
public class SemicolonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
// 指定转换器适用的类型
return typeof(string).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// 读取JSON时不需要进行任何操作因为我们是进行序列化
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
//new一个JObject对象,JObject可以像操作对象来操作json
var newStr = string.Empty;
var temp = (string)value;
if (temp != null && temp.Contains(";"))
{
// 写入JSON时将分号替换为其他字符
string sanitizedValue = temp.Replace(";", "");
writer.WriteValue(sanitizedValue);
}
else
{
writer.WriteValue(value);
}
}
}
}