I am currently struggling to deserialize received JSON into a DTO object which has IEnumerable<T>
data property. Using ASP.NET Core 3.1 and System.Text.Json 4.7.0.
Below is displayed the JSON
{
"data": [
{
"alternateId": "2cc688d6-cc51-4501-9320-9e5b6e0b8710",
"name": "random1",
},
{
"alternateId": "e1c14f81-12fa-4ed8-b5ab-e1f4b9f25401",
"name": "random2",
}
],
"pageNumber": 1,
"pageSize": 2,
"resultCount": 2,
"sortColumnName": "Name",
"orderByType": "Ascending"
}
And I am trying to deserialize this data into the following Dtos by using these commands:
var responseData = await response.Content.ReadAsAsync<PagedResponse<OutputDTO>>();
var responseString = await response.Content.ReadAsStringAsync();
var responseData2 = JsonSerializer.Deserialize<PagedResponse<OutputDTO>>(responseString);
The first ReadAsAsync
approach deserializes all other properties correctly except the data. Second, responseData2 deserialization produces only empty properties.
Dto classes:
public class PagedResponse<T>
{
public IEnumerable<T> Data { get; }
public PagedResponse()
{
}
public PagedResponse(IEnumerable<T> data)
{
Data = data;
}
public int? PageNumber { get; set; }
public int? PageSize { get; set; }
public int? ResultCount { get; set; }
public string SortColumnName { get; set; }
public OrderByType OrderByType { get; set; }
}
public class OutputDTO
{
public Guid AlternateId { get; set; }
public string Name { get; set; }
}