jeramyRR
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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ConnorRigby
This is actually one of my favorite use cases for macros in Elixir. I’ll show how i’d do it without macros first and if there is interest i can show how i’d build a macro later.
Anyway i really don’t like Poison’s
as: %Data{}feature. I won’t go into much details, other than you are now locked into using Poison for this feature that other json libraries might not support. Long story short: you should separate you’re JSON decoding from structuring and restructuring. Elixir != JSON and it shouldn’t be treated as if it is. Anyway here’s my decoder:jeramyRR
Thanks for taking the time out to put that together. You’ve got me really curious about the macro now!
ConnorRigby
So you may have noticed a lot of duplication in that
decoder. Duplication can be a great use case for a macro. Each and every module defined above has adecode/1function. Lets extract that out. Here’s a refactored module:a bit cleaner in my opinion. Here’s the
Decodermodule implementation:Pretty simple.
mdecodedefines a struct and the headdecode/1function from the original implementation.that
Enum.map(&decode/1)will calldecode({key, value})for every one of our expected keys after converting them to atoms. theunquote(block)is a bit of a hack to make the syntax look beteter imo. You could do this without that pretty easily.the
mlist/2macro just defines that functiondecode({key, value})and maps overvalueby either callingunquote(module).decode/1or if supplied a function, it calls that instead which is what we do for:filtersAfter writing that all up i realized your original question wasn’t really answered and this may have been an overload of information depending on how comfortable with Elixir/Metaprogramming you are.
The main point i guess i want to get across is a subjective one: don’t just take my word for it -
Don’t rely on features of a dep outside of the scope of it’s domain. Poison is a json decoder, not a domain data structurer/destructurer. It’s main concern should be taking binary data and parsing it as Elixir data, NOT taking binary data and turning it into youre domain specific data. It doesn’t have enough information to perform that task.
jeramyRR
Thanks again for spending so much time to go through this.