Remove the [JsonConverter(typeof(TypeSerializer))] attribute from the Base class and replace the following line in the Deserialize test:
JObject.Parse(json).ToObject<Base>();
with this:
var obj = JsonConvert.DeserializeObject<Base>(json, new TypeSerializer());
UPDATE 1 This update matches the comment from the questionnaire:
Leave the [JsonConverter(typeof(TypeSerializer))] attribute on the Base class. Use the following line to deserialize:
var obj = JsonConvert.DeserializeObject<Base>(json);
and change the ReadJson method as follows:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var j = JObject.Load(reader); if (j["Type"].ToString() == "a") return new AType(int.Parse(j["Height"].ToString())); return new BType(j["Name"].ToString()); }
Ilija Dimov Aug 20 '14 at 12:55 2014-08-20 12:55
source share