0

I'm trying to deserialize the JSON from a POST request body with ASP.net Core MVC to its respective class object.

class Form
{
    public string name { get; set}
    // Other string properties
    // ...
    public List<Field> fields { get; set; }
}

Field is a super/parent class of a couple of different field types

class Field
{
    public string type { get; set; }
    public bool filledIn { get; set; }
}

class TextField : Field
{
    public string value { get; set; }
}

class Checkbox : Field
{
    public bool value { get; set; }
}

My controller at the moment looks like this

using Microsoft.AspNetCore.Mvc;

using My_Project.Models;

[ApiController]
[Route("/")]
public class MyController : ControllerBase
{
    [HttpPost("test")]// http://mysiteandporthere/test
    public void ParseAndSave(Form myForm)
    {
        // The rest of the code
    }
}

How can I ensure that my Field objects are deserialized into instances of their correct subclass? I'd be happy to do it using either the "type" property, or the type of the "value" property (Checkboxes will always be true/false, TextFields will always have string values).

Here's an example of the JSON in the POST body. This is a pretty simplified view of what I'm working with. I have other field types that I'm sure I could figure out if I can understand how to do this for these 2 fields.

{
    "name": "Bob"
    "fields": [
        {
            "type": "text",
            "filledIn": true,
            "value": "Here's some value"
        },
        {
            "type": "checkbox",
            "filledIn": true,
            "value": true
        }
    ]
}

In this first link, I found an example of how to handle if the root object needs special attention. Would implementing something like this be possible for my Form class and then handle the deserialization of the Fields within it? I suppose the other thing I could do is move all of the properties out of the subclasses or

https://www.c-sharpcorner.com/article/polymorphism-in-web-api-net-core-6/

In this second link, I wasn't quite able to follow what was happening in the answer. If this is what I need, could you please explain it with a little more detail?

How to override default deserialization for Controller Action in ASP.NET Core?

I haven't worked much with anything http request related in c#. I was following this tutorial and had success until I started trying to make different subclasses/derived classes for my fields

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0#verb

Eric Webb
  • 361
  • 3
  • 10
  • What version of .NET are you using? .NET 7, .NET 6 or something else? .NET 7 has support for polymorphism built into System.Text.Json. – dbc Jan 13 '23 at 22:46
  • If .NET 6 or earlier you will have to create a custom `JsonConverter` for `Field` that loads the JSON into an intermediate `JsonDocument` then queries the doc for the `type` property value, then finally deserializes to the correct subclass. See [Is polymorphic deserialization possible in System.Text.Json?](https://stackoverflow.com/q/58074304/3744182) for details. – dbc Jan 13 '23 at 22:46

0 Answers0