为什么我的自定义JsonConverter.WriteJson()方法没有被调用?
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);
答案 0 :(得分:0)
显然这是因为GuiData
为空...我想我可以指定:
TypeNameHandling = TypeNameHandling.Objects
但是我想只序列化GuiData
,即使它是null(我在序列化期间设置它的值),而没有序列化所有的null属性......好吧,如果我找不到更好的方法,我猜我必须满足它......