Base solution for your next web application
Open Closed

deserialize Json, abstract class in ApplicationService #76


User avatar
0
ioannes created

Hello,

In my ApplicationService I need to deserialize list of objects send from client to concrete classes which inherits abstract class Module.

public ModulesDto
{
public List<Module> {...}
}

public abstract class Module
{...}

public class Module1: Module
{...}

public class Module2: Module
{...}

but I got null.

Is there a way to do it? I found interesting article, and even more interesting comment bellow, how to implement it.

var settings = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto};

<a class="postlink" href="http://www.tomdupont.net/2014/04/deserialize-abstract-classes-with.html">http://www.tomdupont.net/2014/04/deseri ... -with.html</a>

Thanks. :)


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I assume that you're using application services and dynamic web API layer.

    ABP uses ASP.NET Web API's defult model binding. And as I know this is not possible, at least as default. Because we can not know which class will be used based on properties. If you can find a way of doing it with web API, you can do it with ABP.

    As a workaround, you can send JSON as string and name of the class. Then deserialize it in the C#.

  • User Avatar
    0
    ioannes created

    Thanks for advice! I will use your workaround.