[erlang-questions] Serializing a list

Richard O'Keefe <>
Wed Oct 28 01:10:22 CET 2009

On Oct 27, 2009, at 7:40 PM, sapan shah wrote:
> eg L1 = [1,[[2,3]],[4]].
> when I perform
> binary_to_list(list_to_binary(L1))
> The result is [1,2,3,4]. I loose the original structure of the list.

That's because list_to_binary/1 is defined to take an "iolist",
which is a tree structure whose leaves are binaries and *bytes*,
flatten it, and give you a binary.  Had you tried a larger integer,
such as list_to_binary([999]), you would have had even more trouble.

What you want is term_to_binary/1 to do the encoding
and binary_to_term/1 to do the decoding.

Searching the 'erlang' manual page for 'to_binary' and 'binary_to'
would have found these.

More information about the erlang-questions mailing list