我在使用JSON反序列多态性涉及对象的数组的一个问题。我试过记录here和here它的工作非常适合系列化,但都吹起来的反序列化。
I'm having a problem with JSON Deserialization involving an array of polymorphic objects. I've tried the solutions for serialization documented here and here which work great for serialization, but both blow up on deserialization.
我的阶级结构如下:
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
DescriptiveObservation
[DataContract(IsReference = true)] public class DescriptiveObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Descriptive; } }NoteObservation
NoteObservation
[DataContract(IsReference = true)] public class NoteObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Note; } }NumericObservation
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
ScoredObservation
[DataContract(IsReference = true)] public class ScoredObservation : Observation { [DataMember] public int Value { get; set; } protected override ObservationType GetObservationType() { return ObservationType.Scored; } }我公正的使用无论是内置的JavaScriptSerializer或Newtonsoft JSON库。
I'm impartial to using either the built in JavaScriptSerializer or the Newtonsoft JSON library.
连载code
var settings = new Newtonsoft.Json.JsonSerializerSettings(); settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);反序列化code
Deserialization Code
return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet)); //Scoresheet contains a list of observationgroups这是我得到的错误是
无法创建类型ProjectXCommon.DataStores.Observation的一个实例。类型是一个接口或抽象类,不能instantated。
"Could not create an instance of type ProjectXCommon.DataStores.Observation. Type is an interface or abstract class and cannot be instantated."
任何帮助将是非常美联社preciated!
Any help would be much appreciated!
推荐答案您还没有添加时反序列化的任何设置。你需要用 TypeNameHandling 应用设置设置为对象或所有。
You have not added any settings upon deserialization. You need to apply settings with TypeNameHandling set to Object or All.
这样的:
JsonConvert.DeserializeObject( returnedStringFromClient, typeof(Scoresheet), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });文档: TypeNameHandling设置
更多推荐
JSON反序列多态对象的数组
发布评论