vrod
I know I can implement some protocols to make JSON encoding work with structs and tuples.
However, decoding JSON would never produce the same input unless you included some meta data that specified which terms were atoms or structs etc.
I am seeing a few packages that do serialization/deserialization that can serialize structs, tuples, keyword lists, and atoms AND then unserialize them. The closest I think I found was php_serializer | Hex – it’s close, but the unserialize does not come back the way it went in.
I understand some things cannot be serialized (like captured functions I think?), but I think the structs and keyword lists can call be brought back. Does anyone know of a library that can unserialize this way?
So that:
input = %MyStruct{atom: :test, keyword: [a: "yes", b: "no"], tuple: {:ok, "yes"}}
result = input |> serialize() |> unserialize()
input == result
Thank you for any thoughts!
Trending in Questions
Other Trending Topics
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sezaru
There you go:
Sebb
Poison has limited support (can’t do what you want) and Jason intentionally does not support decoding into data structures at all, see: Support for decoding into data structures · Issue #124 · michalmuskala/jason · GitHub. Jiffy also can’t do it.
If possible use
term_to_binary/binary_to_termor do it yourself.I’d like to see a JSON decoder like python’s
vrod
I did not know this!
sezaru
I recommend you take a look at the documentation for this function, there are some cool options to it, for example, you can compress the binary output using the
compressedoption like this:hauleth
Just beware, that it is not safe to use
:erlang.binary_to_term/1on untrusted data.LostKobrakai
And as soon as things are stored beyond the lifecycle of a single application version one needs to still handle struct (and likely records) with care. Changing their definition might be troublesome.