contextera

Tuesday, April 20, 2010

Java Object serialization

Here i have used Java's object serialization to serialize a map into memory and create that map back from memory. This is helpful if your are trying to dump objects into databases [into blob cloumns] instead of Files.

Normal googling gives serialization of objects into Files.

from java.io import ObjectOutputStream, ObjectInputStream, ByteArrayOutputStream, ByteArrayInputStream


//JYTHON CODE USING JAVA OBJECT SERIALIZATION


from java.util import HashMap


m = HashMap()
m.put("Name", "Sarath")
m.put("Company", "Strand")
print m

bout = ByteArrayOutputStream()
oout = ObjectOutputStream(bout)

oout.writeObject(m)

bytes = bout.toByteArray()

bin = ByteArrayInputStream(bytes)
#bin.write(bytes, 0, bytes.length)
oin = ObjectInputStream(bin)

print "### output"
m2 = oin.readObject()

print m2

No comments:

Post a Comment