You can try using the functions :erlang.term_to_binary/1 and :erlang.binary_to_term/1 (small pieces of documentation here ).
A small example:
iex> defmodule FooStruct do ...> defstruct foofield: nil ...> end iex> struct = %FooStruct{foofield: 42} iex> binary_representation = :erlang.term_to_binary(struct) <<131, 116, 0, 0, 0, 2, 100, 0 ... >> iex> :erlang.binary_to_term(binary_representation) %FooStruct{foofield: 42}
It should work in almost everything (I think!).
In Redis, in particular, you can send directly raw binary data to a Redis server and convert it back to Elixir data structures after they return from the server (again, like binary data). Here is a small example (using exredis ):
iex> client = Exredis.start iex> data = :erlang.term_to_binary(%{foo: "bar"}) << ... >> iex> client |> Exredis.query(["SET", "mymap", data]) "OK" iex> retrieved_data = client |> Exredis.query(["GET", "mymap"]) << ... >> iex> :erlang.binary_to_term(retrieved_data) %{foo: "bar"}
source share