vrod

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!

Showing Posts 1 to 6

sezaru

sezaru

There you go:

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

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_term or do it yourself.

I’d like to see a JSON decoder like python’s

json.load(fp, *, cls=None, object_hook=None ... object_pairs_hook=None ...)

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes priority.

vrod

vrod OP

I did not know this!

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)
hauleth

hauleth

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
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

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews