标签:tps ack cmd ice add byte 解决方法 ecif []
今天在使用JSON序列化类时出现问题,原来类中有一个接口,在反序列化时不知道接口的实体是什么
Device类中,BusConnector为一通信接口,根据需要传入不同的通信方式实体,,正常反序列化时出现
“Type is an interface or abstract class and cannot be instantiated” 这样的错误
方法一:在序列化时增加对应的说明
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
JsonConvert.SerializeObject(entity, Formatting.Indented, settings);
方法二:增加转化类
public class Model
{
[JsonConverter(typeof(ConcreteTypeConverter<Something>))]
public ISomething TheThing { get; set; }
}
public class ConcreteTypeConverter<TConcrete> : JsonConverter
{
public override bool CanConvert(Type objectType)
{ //assume we can convert to anything for now
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{ //explicitly specify the concrete type we want to create
return serializer.Deserialize<TConcrete>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{ //use the default serialization - it works fine serializer.Serialize(writer, value);
}
}
方法三:直接使用JSON.NET上的在属性上增加
这三种方法者有可实现性,其中方法三最方便,方法一可以实现,但每个对象之前增加对象类型。
解决方法参考:https://stackoverflow.com/questions/2254872/using-json-net-converters-to-deserialize-properties
https://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base/8031283#8031283
标签:tps ack cmd ice add byte 解决方法 ecif []
原文地址:https://www.cnblogs.com/kingkie/p/10311929.html