Мне было интересно, как смоделировать это на C#, и мне пришла в голову идея использовать Generics для представления свойства Response — таким образом я мог бы обрабатывать множество различных типы ответов в полуэлегантном виде.
Поэтому я создал такой базовый класс:
Код: Выделить всё
public abstract class ApiFootballResponseBase
where TResponse : IApiFootballResponseType
{
public string Get { get; set; }
[JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
public Dictionary Parameters { get; set; }
[JsonConverter(typeof(ApiFootballDictOrEmptyArrayJsonConverter))]
public Dictionary Errors { get; set; }
public ApiFootballPaging Paging { get; set; }
public int Results { get; set; }
public TResponse Response { get; set; }
[JsonConstructor]
public ApiFootballResponseBase()
{
}
}
Тогда у нас есть пример класса, который я хочу использовать для получения информации о статусе API по URL-адресу, указанному выше.
Код: Выделить всё
public class ApiFootballStatusResponse : IApiFootballResponseType
{
public ApiFootballSubscriptionInformation Subscription { get; set; }
// TODO: Requests
public ApiFootballAccountInformation Account { get; set; }
[JsonConstructor]
public ApiFootballStatusResponse()
{
}
}
И причина этого — исключение, которое я получить, когда я пытаюсь десериализовать с помощью RestSharp и 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.
Это исключение действительно пытается сказать мне что-то еще? Это плохой способ попытаться обработать свойство Response с разным содержимым в зависимости от запрошенного ресурса? Если да, то какой способ избежать необходимости воссоздавать общие свойства всех запросов в разных классах/записях?
Подробнее здесь: https://stackoverflow.com/questions/788 ... fails-with
Мобильная версия