Учитывая следующее JSON:
[
{
"ticker": "AAPL",
"name": "Apple Inc.",
"market": "STOCKS",
"locale": "US",
"currency": "USD",
"active": true,
"primaryExch": "NGS",
"type": "cs",
"codes": {
"cik": "0000320193",
"figiuid": "EQ0010169500001000",
"scfigi": "BBG001S5N8V8",
"cfigi": "BBG000B9XRY4",
"figi": "BBG000B9Y5X2"
},
"updated": "2019-01-15T05:21:28.437Z",
"url": "https://api.polygon.io/v2/reference/tickers/AAPL"
},
{
"ticker": "$AEDAUD",
"name": "United Arab Emirates dirham - Australian dollar",
"market": "FX",
"locale": "G",
"currency": "AUD",
"active": true,
"primaryExch": "FX",
"updated": "2019-01-25T00:00:00.000Z",
"attrs": {
"currencyName": "Australian dollar,",
"currency": "AUD,",
"baseName": "United Arab Emirates dirham,",
"base": "AED"
},
"url": "https://api.polygon.io/v2/tickers/$AEDAUD"
},
{ /* another stock */ },
{ /* another stock */ },
{ /* another FX*/ },
{ /* another stock */ },
{ /* another FX*/ }
/* and so forth */
]
I wi sh для десериализации в общий список типа List<Ticker>
:
class Ticker
{
string Ticker {get; set;}
string Name {get; set;}
MarketEnum Market {get; set;}
LocaleEnum Locale {get; set;}
CurrencyEnum Currency {get; set;}
bool Active {get; set;}
PrimaryExchEnum PrimaryExch {get; set;}
DateTimeOffset Updated {get; set;}
}
class Stock : Ticker
{
int Type {get; set;}
string CIK {get; set;}
string FIGIUID {get; set;}
string SCFIGI {get; set;}
string CFIGI {get; set;}
string FIGI {get; set;}
}
class ForeignExchange : Ticker
{
BaseCurrencyEnum BaseCurrency {get; set;}
}
У меня есть следующий код :
class TickerConvertor : CustomCreationConverter<Ticker>
{
public override Ticker Create(Type objectType)
{
throw new NotImplementedException();
}
public Ticker Create(Type objectType, JObject jObject)
{
var type = (PrimaryExch)(int)jObject.Property("PrimaryExch");
if (type == PrimaryExch.FX)
return new ForeignExchange();
else
return new Stock();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jObject = JObject.Load(reader); // Load JObject from stream
var target = Create(objectType, jObject); // Create target object based on JObject
serializer.Populate(jObject.CreateReader(), target); // Populate the object properties
return target;
}
}
Но я не уверен, как бы я поступил со сглаживанием подструктуры codes
для типа Ticker
и подструктуры attrs
для типа ForeignExchange
.