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:
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.
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:
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?