Search code examples
c#jsondeserializationsystem.text.json

Deserialization of inteface types not supported using System.Text.JSON


I am passing in a valid JSON object to my controller on my .net core 3 web api application. Doing so, I get the error:

System.NotSupportedException: Deserialization of interface types is not supported. Type 'OrderTranslationContracts.OrderContracts+IImportOrderLineModel'

So I looked in my code and I have the following concrete implementation of an Interface. Here is the line I think that's throwing the error:

   public List<OrderContracts.IImportOrderLineModel> Lines { get; set; }

Here is the portion of the JSON I am passing into the controller:

"lines": [
        {
            "orderNumber": "LV21131327",
            "lineNumber": 1,
            "itemId": "3083US",
            "customerItemId": "3083US",
            "quantity": 3,
            "price": 0.00,
            "quantityBackOrdered": null,
            "comments": "",
            "pickLocation": "",
            "orderFilled": "O",
            "hostUom": null,
            "type": null
        }

So I know the JSON is valid. Here is the signature for the controller:

[HttpPost]
    public async Task<List<ImportOrderModel>> Post([FromBody] List<ImportOrderModel> orders)
    {
        var response = await _validateOrder.ValidateAllOrdersAsync(orders, null);
        return response;
    }

I don't even break into this code as I am assuming that the JSON deserializer is throwing the error as it tries to convert it. So how do I over come this error? I am bound by the concrete implementation for the interface so I can't change the interface I need to work with what I have here if possible. If that isn't possible, are there any "work arounds" for this?


Solution

  • Here:

    public List<OrderContracts.IImportOrderLineModel> Lines { get; set; }
    

    Your list is of type IImportOrderLineModel Interface.

    it should be like

     public List<ImportOrderLineModel> Lines { get; set; }
    

    that ImportOrderLineModel is a class that implements it like:

    public class ImportOrderLineModel : IImportOrderLineModel
    {
        //......
    }