title: JSON Deserialization with an array of polymorphic objects
body: <p>I'm having a problem with JSON Deserialization involving an array of polymorphic objects. I've tried the solutions for serialization documented <a href="https://stackoverflow.com/questions/5186973/json-serialization-of-array-with-polymorphic-objects">here</a> and <a href="https://stackoverflow.com/questions/2254872/using-json-net-converters-to-deserialize-properties">here</a> which work great for serialization, but both blow up on deserialization. </p>
  
  <p>My class structure is as follows:</p>
  
  <p>IDable</p>
  
  <pre><code>[DataContract(IsReference=true)]
  public abstract class IDable&lt;T&gt; {
  
      [DataMember]
      public T ID { get; set; }
  }
  </code></pre>
  
  <p>Observation Group</p>
  
  <pre><code>[DataContract(IsReference=true)]
  [KnownType(typeof(DescriptiveObservation))]
  [KnownType(typeof(NoteObservation))]
  [KnownType(typeof(NumericObservation))]
  [KnownType(typeof(ScoredObservation))]
  public class ObservationGroup : IDable&lt;int&gt; {
  
      [DataMember]
      public string Title { get; set; }
  
      [DataMember]
      public List&lt;Observation&gt; Observations { get; set; }
  
      [OnDeserializing]
      void OnDeserializing(StreamingContext context)
      {
          init();
      }
  
      public ObservationGroup()  {
          init();
      }
  
      private void init()
      {
          Observations = new List&lt;Observation&gt;();
          ObservationRecords = new List&lt;ObservationRecord&gt;();
      }
  
  }
  </code></pre>
  
  <p>DescriptiveObservation </p>
  
  <pre><code>[DataContract(IsReference = true)]
  public class DescriptiveObservation : Observation
  {
  
      protected override ObservationType GetObservationType()
      {
          return ObservationType.Descriptive;
      }
  }
  </code></pre>
  
  <p>NoteObservation</p>
  
  <pre><code>[DataContract(IsReference = true)]
  public class NoteObservation : Observation
  {
      protected override ObservationType GetObservationType()
      {
          return ObservationType.Note;
      }
  }
  </code></pre>
  
  <p>NumericObservation</p>
  
  <pre><code>[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;
      }
  }
  </code></pre>
  
  <p>ScoredObservation</p>
  
  <pre><code>[DataContract(IsReference = true)]
  public class ScoredObservation : Observation {
      [DataMember]
      public int Value { get; set; }
  
      protected override ObservationType GetObservationType() {
          return ObservationType.Scored;
      }
  }
  </code></pre>
  
  <p>I'm impartial to using either the built in JavaScriptSerializer or the Newtonsoft JSON library.</p>
  
  <p>Serialization Code</p>
  
  <pre><code>var settings = new Newtonsoft.Json.JsonSerializerSettings();
  settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
  
  Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);
  </code></pre>
  
  <p>Deserialization Code</p>
  
  <pre><code>return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet));
  //Scoresheet contains a list of observationgroups
  </code></pre>
  
  <p>The error that I get is </p>
  
  <p>"Could not create an instance of type ProjectXCommon.DataStores.Observation. Type is an interface or abstract class and cannot be instantated."</p>
  
  <p>Any help would be much appreciated!</p>