Object serialization: Difference between revisions

no edit summary
m (→‎[[Object Serialization#ALGOL 68]]: add {{trans|python}})
No edit summary
Line 4:
=={{header|Ada}}==
This file contains the package specification containing the public definitions of the inheritance tree rooted at the type ''Message''. Each type in the inheritance tree has its own print procedure.
<lang ada>with Ada.Calendar; use Ada.Calendar;
 
package Messages is
Line 28:
procedure Print(Item : Control_Message);
end Messages;</adalang>
 
The next portion contains the implementation of the procedures defined in the package specification.
<lang ada>with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
Line 98:
end Display;
end Messages;</adalang>
 
The final section of code creates objects of the three message types and performs the printing, writing, and reading. The Ada attributes '' 'Class'Output'' serialize the object and write it to the specified stream. The '' 'Class'Input'' attributes call a function automatically provided by the compiler which reads from the specified stream file and returns the object read. The ''Display'' procedure takes an object in the inheritance tree rooted at ''Message'' and dispatches the correct print procedure.
 
<lang ada>with Messages; use Messages;
with Ada.Streams.Stream_Io; use Ada.Streams.Stream_Io;
with Ada.Calendar; use Ada.Calendar;
Line 141:
end loop;
Close(The_File);
end Streams_Example;</adalang>
Output results:
Time Stamp:2007-3-9
Line 241:
surgeon.addExit(makeArrival, "makeArrival")
 
The 'exits' of the surgeon (so called because it cuts and joins object subgraphs) specify the points at which serialization should stop, instead replacing references to the objects with the specified names. On unserialization, the names are looked up and replaced with the corresponding objects. (The surgeon provides default exits for such things as <codett>false</codett>, <codett>true</codett>, <codett>null</codett>, and the list constructor.)
def objs := [makeEvent(timer.now()),
Line 251:
 
=={{header|Java}}==
<lang java>import java.io.*;
 
// classes must implement java.io.Serializable in order to be serializable
Line 310:
}
}
}</javalang>
 
=={{header|OCaml}}==
Line 316:
Objects which contain methods are difficult to serialize because it will want to serialize those methods too, but functions usually cannot be serialized. Instead, here we perform the task on non-object datatypes, with an outside function to print them.
 
<lang ocaml>type entity = { name : string }
 
let create_entity () = { name = "Entity" }
Line 338:
 
print_entity result1;;
print_entity result2;;</ocamllang>
 
=={{header|Perl}}==
{{libheader|Storable}}
<lang perl>{
package Greeting;
sub new {
Line 376:
print $g2->stringify;
print $s2->stringify;
};</perllang>
 
=={{header|PHP}}==
Serialization in PHP is straightforward. The built-in function [http://www.php.net/manual/en/function.serialize.php serialize()] handles it in a single statement.
<lang php>$myObj = new Object();
$serializedObj = serialize($myObj);</phplang>
In order to un-serialize the object, use the [http://www.php.net/manual/en/function.unserialize.php unserialize()] function. Note that the class of object must be defined in the script where un-serialization takes place, or the class' methods will be lost.
 
=={{header|Python}}==
<lang python># Object Serialization in Python
# serialization in python is accomplished via the Pickle module.
# Alternatively, one can use the cPickle module if speed is the key,
Line 421:
 
i1.printName()
i2.printName()</pythonlang>
 
=={{header|Ruby}}==
<lang ruby>class Being
def initialize(specialty=nil)
@specialty=specialty
Line 493:
end.join("\n\n")
)
puts "END LOADED DIVERSE COLLECTION"</rubylang>