0
爲什麼我的自定義JsonConverter.WriteJson()方法沒有被調用?爲什麼JsonConverter.WriteJson()永遠不會被調用,儘管JsonConverter.ReadJson()確實被調用?
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);
我有相反的問題。 WriteJson被調用,但是ReadJson永遠不會被調用。 – Triynko