Serializing a list

View: New views
4 Messages — Rating Filter:   Alert me  

Serializing a list

by sapan shah :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Lets assume that we have a list where,
List -> integer | List;

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.
How do then the serialization of the list is done?

Is there any other way we can perform 'binary_to_list(list_to_binary(L))'
functionality maintaining the structure of the list??

--
Regards,
Sapan Shah

Re: Serializing a list

by Max Lapshin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

term_to_binary, binary_to_term

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org


Re: Serializing a list

by sapan shah :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks... missed this part...

On Tue, Oct 27, 2009 at 12:13 PM, Max Lapshin <max.lapshin@...> wrote:

> term_to_binary, binary_to_term
>



--
Regards,
Sapan Shah

Re: Serializing a list

by Richard O'Keefe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org