Object serialization: Difference between revisions

m
Alphabetized
m (→‎{{header|Python}}: Whitespace fmt)
m (Alphabetized)
Line 202:
$serializedObj = serialize($myObj);
In order to unserialize 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 unserialization takes place, or the class' methods will be lost.
 
=={{header|Python}}==
<pre>
# 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,
# everything else in this example remains the same.
 
import pickle
 
class Entity:
def __init__(self):
self.name = "Entity"
def printName(self):
print self.name
 
class Person(Entity): #OldMan inherits from Entity
def __init__(self): #override constructor
self.name = "Cletus"
 
instance1 = Person()
instance1.printName()
 
instance2 = Entity()
instance2.printName()
 
target = file("objects.dat", "w") # open file
 
# Serialize
pickle.dump((instance1, instance2), target) # serialize `instance1` and `instance2`to `target`
target.close() # flush file stream
print "Serialized..."
 
# Unserialize
target = file("objects.dat") # load again
i1, i2 = pickle.load(target)
print "Unserialized..."
 
i1.printName()
i2.printName()
</pre>
 
=={{header|Ruby}}==
Line 274 ⟶ 315:
)
puts "END LOADED DIVERSE COLLECTION"
 
=={{header|Python}}==
<pre>
# 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,
# everything else in this example remains the same.
 
import pickle
 
class Entity:
def __init__(self):
self.name = "Entity"
def printName(self):
print self.name
 
class Person(Entity): #OldMan inherits from Entity
def __init__(self): #override constructor
self.name = "Cletus"
 
instance1 = Person()
instance1.printName()
 
instance2 = Entity()
instance2.printName()
 
target = file("objects.dat", "w") # open file
 
# Serialize
pickle.dump((instance1, instance2), target) # serialize `instance1` and `instance2`to `target`
target.close() # flush file stream
print "Serialized..."
 
# Unserialize
target = file("objects.dat") # load again
i1, i2 = pickle.load(target)
print "Unserialized..."
 
i1.printName()
i2.printName()
</pre>
Anonymous user