我遇到了一个涉及多态对象数组的JSON反序列化问题。我尝试了这里和这里记录的序列化解决方案,它们对序列化非常有效,但在反序列化方面都失败了。
我的类结构如下:IDable
[DataContract(IsReference=true)]
public abstract class IDable<T> {
[DataMember]
public T ID { get; set; }
}
观察组
[DataContract(IsReference=true)]
[KnownType(typeof(DescriptiveObservation))]
[KnownType(typeof(NoteObservation))]
[KnownType(typeof(NumericObservation))]
[KnownType(typeof(ScoredObservation))]
public class ObservationGroup : IDable<int> {
[DataMember]
public string Title { get; set; }
[DataMember]
public List<Observation> Observations { get; set; }
[OnDeserializing]
void OnDeserializing(StreamingContext context)
{
init();
}
public ObservationGroup() {
init();
}
private void init()
{
Observations = new List<Observation>();
ObservationRecords = new List<ObservationRecord>();
}
}
DescriptiveObservation
[DataContract(IsReference = true)]
public class DescriptiveObservation : Observation
{
protected override ObservationType GetObservationType()
{
return ObservationType.Descriptive;
}
}
NoteObservation
[DataContract(IsReference = true)]
public class NoteObservation : Observation
{
protected override ObservationType GetObservationType()
{
return ObservationType.Note;
}
}
NumericObservation
[DataContract(IsReference = true)]
public class NumericObservation : Observation
{
[DataMember]
public double ConstraintMaximum { get; set; }
[DataMember]
public double ConstraintMinimum { get; set; }
[DataMember]
public int PrecisionMaximum { get; set; }
[DataMember]
public int PrecisionMinimum { get; set; }
[DataMember]
public string UnitType { get; set; }
protected override ObservationType GetObservationType()
{
return ObservationType.Numeric;
}
}
ScoredObservation
[DataContract(IsReference = true)]
public class ScoredObservation : Observation {
[DataMember]
public int Value { get; set; }
protected override ObservationType GetObservationType() {
return ObservationType.Scored;
}
}
我对使用内置的JavaScriptSerializer或Newtonsoft JSON库持中立态度。
序列化代码var settings = new Newtonsoft.Json.JsonSerializerSettings();
settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);
反序列化代码return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet));
//Scoresheet contains a list of observationgroups
我得到的错误是
"无法创建projectxcommon . datastore . observation类型的实例。"类型是接口或抽象类,不能实例化。"
任何帮助将非常感激!
您没有添加任何反序列化设置。您需要将TypeNameHandling
设置为Object
或All
。
像这样:
JsonConvert.DeserializeObject(
returnedStringFromClient,
typeof(Scoresheet),
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
文档:TypeNameHandling设置
使用这个JsonKnownTypes,方法类似:
[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
[JsonKnownType(typeof(Base), "base")]
[JsonKnownType(typeof(Derived), "derived")]
public class Base
{
public string Name;
}
public class Derived : Base
{
public string Something;
}
现在当你序列化json对象将添加"$type"
与"base"
和"derived"
值,它将用于反序列化
序列化列表示例:
[
{"Name":"some name", "$type":"base"},
{"Name":"some name", "Something":"something", "$type":"derived"}
]