A simple JSON to model

In one of my works, we had a scenario were the front devs will call an API with different type of messages. I tried to find a simple solution, and try to avoid a list of API per model.

So I should have some common properties and a string value with the right data to parse. My API entry point will be something like this:

              public class Message
{
    // Type of class I want to instantiate
    public int ContentType { get; set; }
    // The content
    public JSonElement Content { get; set; }
}

...

public IActionResult ParseSomeMessage([FromBody] Message message) { }

      

How I use the class Message ? The ContentType value will be parsed to its enum definition with the list of class I can actually manage :

        public enum ContentType
{
    Others = 0,
    TheClass = 1,
    TheOtherClass = 2
}

public class TheClass
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

      

The Content value is a json format of the class I need to instantiate.
So in my ParseSomeMessage method, after all the required validations, I can do something like this :

        // ContentType contentType = (ContentType)message.ContentType;
// string content = Content.GetRawText();

string classTypeOfMessage = $"{contentType}";
Type typeMessage = Type.GetType($"{GetType().Namespace}.{classTypeOfMessage}");
var messageContent = JsonSerializer.Deserialize(content, typeMessage, _options)

      

And messageContent will be the class I'm looking for. So If I call the API with this body :

        {
    "contentType": 1,
    "content": {"firstName": "John", "lastName": "Doe"}
}

      

I should be able to have my variable messageContent as a TheClass type with all its properties filled :

        Assert.That(messageContent.FirstName, Is.Equal("John"));
Assert.That(messageContent.LastName, Is.Equal("Doe"));

      

Drawbacks

This solution is not perfect. It's a simple way to parse simple models. Managing errors could be tricky. For example, this 2 solutions works, but the result can be not what we want :

        {
    "contentType": 1,
    "content": {"lastName": "Doe"}
}

{
    "contentType": 1,
    "content": {"firstName": "John", "lastName": "Doe", "birthDate": "2000-01-01T00:00:00:000"}
}

      

In the first example, the class will exists but FirstName will be null. In the second one, the property birthDate will be lost in the process. No exception will be throw, so it's up to you to had some validations (null not allowed, verify that Json.Serialize is equal to content, ...)