Using generics in C# to define sub-property to deserialize JSON fails with constructor weirdness

I am going to do a test project with api-football.com. Their responses always follow the same shape – an example can be seen here. The Get, Parameters, Errors, Results and Response properties are always in the JSON, but the shape of “Response” depends on what you are calling. The other properties remain the same.

I was wondering how to model this in C#, and the idea hit me to use Generics to represent the Response property – that way, I could handle many different types of responses in a semi-elegant way.

So I made a base class like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public abstract class ApiFootballResponseBase<TResponse>
where TResponse : IApiFootballResponseType
{
public string Get { get; set; }
[JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
public Dictionary<string, string> Parameters { get; set; }
[JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
public Dictionary<string, string> Errors { get; set; }
public ApiFootballPaging Paging { get; set; }
public int Results { get; set; }
public TResponse Response { get; set; }
[JsonConstructor]
public ApiFootballResponseBase()
{
}
}
</code>
<code>public abstract class ApiFootballResponseBase<TResponse> where TResponse : IApiFootballResponseType { public string Get { get; set; } [JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))] public Dictionary<string, string> Parameters { get; set; } [JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))] public Dictionary<string, string> Errors { get; set; } public ApiFootballPaging Paging { get; set; } public int Results { get; set; } public TResponse Response { get; set; } [JsonConstructor] public ApiFootballResponseBase() { } } </code>
public abstract class ApiFootballResponseBase<TResponse>
    where TResponse : IApiFootballResponseType
{
    public string Get { get; set; }

    [JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
    public Dictionary<string, string> Parameters { get; set; }

    [JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
    public Dictionary<string, string> Errors { get; set; }

    public ApiFootballPaging Paging { get; set; }

    public int Results { get; set; }

    public TResponse Response { get; set; }

    [JsonConstructor]
    public ApiFootballResponseBase()
    {
    }
}

IApiFootballResponseType is just an empty marker interface. The JsonConverter used with Parameters and Errors was written by me to handle the fact that these fields when empty are just set to an empty array ([]), while if they contain something, they are property bags that I want returned as dictionaries. The JsonConverter has been tested and works well enough.

We then have an example class I want to use to get the API status information from the URL above from.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ApiFootballStatusResponse : IApiFootballResponseType
{
public ApiFootballSubscriptionInformation Subscription { get; set; }
// TODO: Requests
public ApiFootballAccountInformation Account { get; set; }
[JsonConstructor]
public ApiFootballStatusResponse()
{
}
}
</code>
<code>public class ApiFootballStatusResponse : IApiFootballResponseType { public ApiFootballSubscriptionInformation Subscription { get; set; } // TODO: Requests public ApiFootballAccountInformation Account { get; set; } [JsonConstructor] public ApiFootballStatusResponse() { } } </code>
public class ApiFootballStatusResponse : IApiFootballResponseType
{

    public ApiFootballSubscriptionInformation Subscription { get; set; }
    // TODO: Requests

    public ApiFootballAccountInformation Account { get; set; }

    [JsonConstructor]
    public ApiFootballStatusResponse()
    {
        
    }
}

These property types are also just simple property bags with [JsonConstructor] annotations on a parameterless constructor.

And the reason for that is the exception I get when I try to deserialize with RestSharp and System.Text.Json:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ApiFootball.Responses.ApiFootballResponseBase`1[ApiFootball.Responses.ApiFootballStatusResponse]'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
</code>
<code>NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ApiFootball.Responses.ApiFootballResponseBase`1[ApiFootball.Responses.ApiFootballStatusResponse]'. Path: $ | LineNumber: 0 | BytePositionInLine: 1. </code>
NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ApiFootball.Responses.ApiFootballResponseBase`1[ApiFootball.Responses.ApiFootballStatusResponse]'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

However, as you can see, I have tried adding parameterless constructors to all classes, and marked them as JsonConstructor (the one from System.Text.Json, not NewtonSoft).

Is this exception really trying to tell me something else? Is this a bad way to try to handle the Response property with different contents depending on resource requested? If so, what would be a good way not to have to recreate the common properties of all requests in the different classes/records?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật