0

I have a pretty simple dto class.

public class GridDto<T> : List<T>
{
    public int TotalItems { get; set; }

    public GridDto(IEnumerable<T> items, int totalItems = 0)
    {
        AddRange(items);
        TotalItems = totalItems;
    }
}

When I call ReadFromJsonAsync I get an NotSupportedException. Is there a fix?

3
  • It's unclear from your question exactly what you are reading when you ReadFromJsonAsync. Commented Jul 8 at 19:01
  • It's the result of a Web API call. I was expecting it to be an array of items and a number for the total count. I see now that only the array of items is being received. I guess the TotalItems property isn't being serialized. Maybe extending the List isn't possible?
    – Homer
    Commented Jul 8 at 19:36
  • We would need to see more code than this. I assume you're populating a model class using Newtonsoft.Json? Commented Jul 9 at 18:59

1 Answer 1

0

I'm having trouble understanding your "simple" dto class. How do you want this to look in json?

The only way i could think of is like this:

{
   "totalItems": 2,
   "items": [
      {
         "name": "test1"
      },
      {
         "name": "test2"
      }
   ]
}

This would be represented by a class looking like this:

public class GridDto<T>
{
    public int TotalItems { get; set; }

    public List<T> Items { get; set; }
}

Which can also be deserialized by ReadFromJsonAsync:

var content = new StringContent("{\"totalItems\": 2, \"items\": [{\"name\": \"test1\"}, {\"name\": \"test2\"}]}");
var dto = await content.ReadFromJsonAsync<GridDto<TestClassWithName>>();

Not the answer you're looking for? Browse other questions tagged or ask your own question.