本文介绍了为什么尽管JsonConverter.ReadJson()确实被调用,但JsonConverter.WriteJson()却从未被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我的自定义JsonConverter.WriteJson()方法没有被调用?
Why my custom JsonConverter.WriteJson() method doesn't get called ?
class MyType{
[JsonConverter(typeof(DocumentXamlDeserializer))]
public string GuiData { get; set; }
public string SimpleString;
}
尽管ReadJson
确实被调用:
public class DocumentXamlDeserializer : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(Gui.Handler.SerializeData());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var xaml = reader.Value as string;
Gui.Handler.DeserializeData(xaml);
return xaml;
}
public override bool CanConvert(Type objectType) { return typeof(string).IsAssignableFrom(objectType); }
}
序列化调用:JsonConvert.SerializeObject(dataModel, Formatting.Indented);
反序列化调用:JsonConvert.DeserializeObject<Model>(raw);
推荐答案
显然是因为GuiData
为null ...我想我可以指定:
Apparently this is because GuiData
is null... I guess I could specify:
TypeNameHandling = TypeNameHandling.Objects
但是我只想序列化GuiData
,即使它为空(我在序列化期间设置了它的值),也没有序列化所有null属性...好吧,如果我找不到更好的方法,我想我会必须满足...
But I want to serialize only GuiData
even if it's null (I set its value during serialization), without serializing all null-properties... well, if I don't find better way, I guess I'd have to suffice with it...
这篇关于为什么尽管JsonConverter.ReadJson()确实被调用,但JsonConverter.WriteJson()却从未被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!