28

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.

My class structure is as follows:

IDable

[DataContract(IsReference=true)]
public abstract class IDable<T> {

    [DataMember]
    public T ID { get; set; }
}

Observation Group

[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;
    }
}

I'm impartial to using either the built in JavaScriptSerializer or the Newtonsoft JSON library.

Serialization Code

var settings = new Newtonsoft.Json.JsonSerializerSettings();
settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;

Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);

Deserialization Code

return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet));
//Scoresheet contains a list of observationgroups

The error that I get is

"Could not create an instance of type ProjectXCommon.DataStores.Observation. Type is an interface or abstract class and cannot be instantated."

Any help would be much appreciated!

Community
  • 1
  • 1
Chainlink
  • 730
  • 2
  • 9
  • 15
  • I'm pretty sure that it's related to your `List` the deserializers are trying to instantiate a type of `Observation` not the specific type that it actually is. With Newtonsoft you can override parts of the deserialization by adding your own Converter (here is an example https://gist.github.com/1140171 ) I'm not sure how much this will help you though as I'm not entirely certain but fairly certain this is the case. :) – Buildstarted Aug 17 '11 at 15:39
  • json samples would be helpful – Frank Aug 17 '11 at 17:15

2 Answers2

30

You have not added any settings upon deserialization. You need to apply settings with TypeNameHandling set to Object or All.

Like this:

JsonConvert.DeserializeObject(
    returnedStringFromClient, 
    typeof(Scoresheet), 
    new JsonSerializerSettings 
    { 
        TypeNameHandling = TypeNameHandling.Objects 
    });

Documentation: TypeNameHandling setting

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
asgerhallas
  • 16,890
  • 6
  • 50
  • 68
  • Doh! For those still using the .NET JavascriptSerializer you can also use `new JavaScriptSerializer(new System.Web.Script.Serialization.SimpleTypeResolver()).Deserialize(returnedStringFromClient, typeof(Scoresheet));` – Chainlink Aug 19 '11 at 13:27
  • 1
    As of RavenDB build 499 (RavenDB uses JSON.NET) when using a generic List where T is an interface, you need to add the [JsonProperty(TypeNameHandling = TypeNameHandling.All)] attribute. This fixes the exception "Could not create an instance of type 'your interface' Type is an interface or abstract class and cannot be instantated." – DalSoft Mar 04 '12 at 21:27
2

Use this JsonKnownTypes, similar way to do that:

[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;
}

Now when you serialize object in json will be add "$type" with "base" and "derived" value and it will be use for deserialize

Serialized list example:

[
    {"Name":"some name", "$type":"base"},
    {"Name":"some name", "Something":"something", "$type":"derived"}
]
Marcel Lamothe
  • 12,452
  • 9
  • 34
  • 44
Dmitry
  • 192
  • 1
  • 3