I'm creating a library wrapper which handle all API requests of this football site.
When I call an endpoint, this response structure is returned:
{
"get": "countries",
"parameters": [],
"errors": [],
"results": 161,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"name": "Albania",
"code": "AL",
"flag": "https://media.api-sports.io/flags/al.svg"
},
{
"name": "Algeria",
"code": "DZ",
"flag": "https://media.api-sports.io/flags/dz.svg"
},
{
"name": "Andorra",
"code": "AD",
"flag": "https://media.api-sports.io/flags/ad.svg"
},
]
}
The example above, is the /countries endpoint. I have used Quicktype to generate the schema structure, and I got this:
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace FootballAPI.Models
{
public partial class Temperatures
{
[JsonProperty("get")]
public string Get { get; set; }
[JsonProperty("parameters")]
public object[] Parameters { get; set; }
[JsonProperty("errors")]
public object[] Errors { get; set; }
[JsonProperty("results")]
public long Results { get; set; }
[JsonProperty("paging")]
public Paging Paging { get; set; }
[JsonProperty("response")]
public Response[] Response { get; set; }
}
public partial class Paging
{
[JsonProperty("current")]
public long Current { get; set; }
[JsonProperty("total")]
public long Total { get; set; }
}
public partial class Response
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
}
What I'm trying to do is have a base structure schema which contains everything except the response property. In the example above response must be an instance of Country which is:
public class Country
{
public string name { get; set; }
public string code { get; set; }
public string flag { get; set; }
}
how can I tell to the base schema above to use Country as T object?
I should be able to correctly parse everything with Newtonsoft.JSON:
JsonConvert.DeserializeObject<Country>;
I have different methods like GetCountries() that only pass the query string, in that case /countries. So I know the specific type I expect "response" to be at that time.
Countrystatically, in advance when you make the call tosonConvert.DeserializeObject? Or is the returned result polymorphic in the sense you don't know in advance what type gets returned and you will need to check the value of"get": "countries"?GetCountries()that only pass the query string, in that case/countries. So I can also pass the type, or I though to convert the response directly in theGetCountries()method, so:GetCountries()call the network client which return the structure above, then I parse the structure of response insideGetCountries(). I don't know if there is a better solution for thisResponseList<T>all you're looking for? dotnetfiddle.net/XXt0Eo?