Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Include properties of child class when serializing parent class

$
0
0

Is it possible to include the properties of 'GroupedPaginatedItemsDto' (child class) when serializing 'PaginatedItemsDto' (parent class) by adding [JsonDerivedType(typeof(GroupedPaginatedItemsDto)] to parent class.

Please see controller code below. I am using parent class 'PaginatedItemsDto' in the code while mapping, but I want this to include properties of child class using [JsonDerivedType(typeof()] or something similar. Thank you.

DatapPovider class:

returnData = new GroupedPaginatedItems{    Items = result,    Skip = 0,    Take = result.Count()};

Controller class:

public async Task<PaginatedItemsDto> GetItems() {    // here I receive correct appropriate child 'Items' and 'skip', 'take' properties    result = await _itemService.GetItemsAsync();     // Here is the issue, 'Items' property gets lost here. I receive only 'skip' and 'take'. So mapping does not work with child class.         return _mapper.Map<PaginatedItemsDto>(result);  }

PaginatedItemsDto:

[JsonDerivedType(typeof(GroupedPaginatedItemsDto)]  // Is this correct way to useJsonDerivedType?, but does not work in my case.public class PaginatedItemsDto{    public int Skip { get; set; }    public int Take { get; set; }public class FlatPaginatedItemsDto : PaginatedItemsDto{    [JsonInclude]    public List<DetailsDto> Items { get; set; } = new();}public class GroupedPaginatedItemsDto : PaginatedItemsDto{    [JsonInclude]    public Dictionary<string, Dictionary<string, List<DetailsDto>>>? Items { get; set; }}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>