I'm getting a "type is interface, cannot be instanciated" deserialization error with json.net even though I am specifying type on the object I'm trying to deserialize
private static JsonSerializerSettings settings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.Auto };
/// <summary>
/// Returns an object created from the jObject and placed in a stub object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jObj"></param>
/// <returns></returns>
public static T FromJObject<T>(JToken jObj)
{
if (jObj == null)
{
jObj = new JObject();
}
if (jObj is JValue)
{
return (T)((JValue)jObj).Value;
}
else
{
return (T)JsonConvert.DeserializeObject<T>(jObj.ToString(), settings);
}
}
this is jObj
{
"Id": 2,
"Name": "Name",
"JsonMapEnum": 0,
"Validations": [
{
"Id": 1,
"$type": "JsonMap.Default.JValidation, JsonMap"
}
],
"JSType": 3,
"SubJsonMapEnum": -1,
"$type": "JsonMap.Default.JAttribute, JsonMap"
}
This is the error
Could not create an instance of type JsonMap.Interfaces.IValidation. Type is an interface or abstract class and cannot be instantated. Path 'Validations[0].Id'
It looks like it's trying to turn Id into a Validation object. Why?
these are the interfaces implemented by my types
public interface IJsonMap
{
long Id { get; set; }
String Name { get; set; }
LazyEnum JsonMapEnum { get; set; }
}
public interface IAttribute : IJsonMap
{
IEnumerable<IValidation> Validations { get; set; }
LazyEnum JSType { get; set; }
LazyEnum SubJsonMapEnum { get; set; }
}
public interface IValidation : IJsonMap
{
IEnumerable<IArgument> Arguments { get; set; }
}
this is the call
FromJObject<JAttribute>(CreationJObj)
JAttribute implements IAttribute