vrod

vrod

Serialize and unserialize structs, tuples, keyword lists, atoms?

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!

Marked As Solved

sezaru

sezaru

There you go:

result = input |> :erlang.term_to_binary() |> :erlang.binary_to_term()

Also Liked

hauleth

hauleth

Just beware, that it is not safe to use :erlang.binary_to_term/1 on untrusted data.

sezaru

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 compressed option like this:

:erlang.term_to_binary(data, compressed: 6)
LostKobrakai

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.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement