I've a simple controller that takes one parameter from the POST request body. Normally it should automatically deserialize it from JSON to the object type but it fails. When I try to deserialize it myself it works withot problems. Here is some code:
The controller (the documentCommand variable is null):
public async Task<IActionResult> Create([FromBody]CreateDocumentCommand documentCommand)
{
if (documentCommand == null)
{
return StatusCode(403); //Deserialization fails, documentCommand is null
}
//we have to reach this :(
return Json(documentCommand.Document.Id);
}
Here is how I serialize it and how I test if it will be able to deserialize it:
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
string serialized = JsonConvert.SerializeObject(item, jsonSerializerSettings);
CreateDocumentCommand deserialized = JsonConvert.DeserializeObject<CreateDocumentCommand>(serialized, jsonSerializerSettings);
In my CreateDocumentCommand class I have an interface property and when I remove the TypeNameHandling = TypeNameHandling.Auto it fails in the second example too.
Is there a way yo tell the MVC deserializer to take the TypeNameHandling into account? It seems to me that it skips it.
EDIT Some more code:
public class CreateDocumentCommand : Command, ICreateDocumentCommand
{
public CreateDocumentCommand()
{
}
public IDocument Document { get; set; }
}
MY SOLUTION: Added that ConcreteTypeConverter which I found at the link provided by Babak Naffas and made some changes because I was getting some circular reference exeptions. Also Added the [JsonConverter(typeof(ConcreteTypeConverter))] beefore the CreateDocumentCommand class.
public class ConcreteTypeConverter<T> : JsonConverter
{
static bool read = false;
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
serializer.TypeNameHandling = TypeNameHandling.Auto;
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
return serializer.Deserialize<T>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override bool CanWrite
{
get
{
return false;
}
}
public override bool CanRead
{
get
{
read = !read;
return read;
}
}
}