Does anyone have any experience decoding json into structs, with Poison, where the struct has an improper list?
I’m trying to decode json from binance api that looks like the following:
{
"timezone": "UTC",
"serverTime": 1508631584636,
"rateLimits": [{
"rateLimitType": "REQUESTS_WEIGHT",
"interval": "MINUTE",
"limit": 1200
},
{
"rateLimitType": "ORDERS",
"interval": "SECOND",
"limit": 10
},
{
"rateLimitType": "ORDERS",
"interval": "DAY",
"limit": 100000
}
],
"exchangeFilters": [],
"symbols": [{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC",
"quotePrecision": 8,
"orderTypes": ["LIMIT", "MARKET"],
"icebergAllowed": false,
"filters": [{
"filterType": "PRICE_FILTER",
"minPrice": "0.00000100",
"maxPrice": "100000.00000000",
"tickSize": "0.00000100"
}, {
"filterType": "LOT_SIZE",
"minQty": "0.00100000",
"maxQty": "100000.00000000",
"stepSize": "0.00100000"
}, {
"filterType": "MIN_NOTIONAL",
"minNotional": "0.00100000"
}]
}]
}
I have structs defined for each key value pair, and Poison is decoding it like a champ all the way up to the filters inside the symbols list. I believe it is because that data falls into the improper list category. Below is my call to Poison.decode:
@spec decode(json: String.t()) :: %ExchangeInfo{}
def decode(json) do
json
|> Poison.decode!(
as: %ExchangeInfo{
rateLimits: [%RateLimit{}],
exchangeFilters: [%ExchangeFilter{}],
symbols: [
%Symbol{
filters: [
%IcebergParts{},
%LotSize{},
%Price{},
%MaxNumOrders{},
%MaxNumAlgoOrders{},
%MinNotional{}
]
}
]
}
)
end
And here is how I have the Symbol struct defined:
@derive [Poison.Encoder]
defstruct [
:symbol,
:status,
:baseAsset,
:baseAssetPrecision,
:quoteAsset,
:quotePrecision,
:orderTypes,
:icebergAllowed,
filters: []
]
The part that I’m jacking up is the filters section of that decode expression. I’ll spare ya’ll the other structs, but they are pretty simple.
Any ideas on how I can get Poison to decode the filters section into the proper struct?