Jump to content

Object serialization: Difference between revisions

Added Erlang
(Added Erlang)
Line 704:
<file:objects.dat>.setBytes(surgeon.serialize(objs))
stdout.println(surgeon.unserialize(<file:objects.dat>.getBytes()))</lang>
=={{header|Erlang}}==
Erlang is not object oriented. This code is based upon my understanding of the Algol 68 example above.
<lang Erlang>
-module( object_serialization ).
 
-export( [task/0] ).
 
-record( entity, {name, date} ).
-record( person, {entity, email} ).
 
task() ->
Person = #person{entity=#entity{name="Cletus", date=20080808}, email="test+1@localhost.localdomain"},
print( Person ),
Entity = #entity{name="Entity", date=20111111},
print( Entity ),
ok = file:write_file( "objects.dat", erlang:term_to_binary([Person, Entity]) ),
{ok, Binary} = file:read_file( "objects.dat" ),
[New_person, New_entity] = erlang:binary_to_term( Binary ),
io:fwrite( "Deserialized\n" ),
print( New_person ),
print( New_entity ).
 
 
print( #entity{name=Name, date=Date} ) ->
io:fwrite( "Entity: " ),
io:fwrite( "name: ~p, date: ~p~n", [Name, Date] );
print( #person{entity=Entity, email=Email} ) ->
io:fwrite( "Person: " ),
print( Entity ),
io:fwrite( "\temail: ~p~n", [Email] ).
</lang>
{{out}}
<pre>
<9> object_serialization:task().
Person: Entity: name: "Cletus", date: 20080808
email: "test+1@localhost.localdomain"
Entity: name: "Entity", date: 20111111
Deserialized
Person: Entity: name: "Cletus", date: 20080808
email: "test+1@localhost.localdomain"
Entity: name: "Entity", date: 20111111
</pre>
 
=={{header|Go}}==
Go has a few choices for serialization. The method shown here is the native method, which is compact and type-aware.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.