我正在编写自定义JsonConverter,但只是真的想要更改读取逻辑。写逻辑应该正常执行。无论如何我尝试它,我得到堆栈溢出,因为Json.NET直接回到转换器。有没有办法指定我不想自定义编写器,或者我想延迟回到正常的Json.NET逻辑?
答案 0 :(得分:9)
事实证明,您可以覆盖JsonConverter上的CanWrite属性,该属性将阻止Json.NET调用转换器。
public class MyConverter: JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException(); //This will never be called since CanWrite is false
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//Do your thing
return new object();
}
public override bool CanWrite { get { return false; } }
public override bool CanConvert(Type objectType)
{
//Do your thing
return true;
}
}