views:

332

answers:

6

is there any library to deserialize with python which is serialized with java.

+6  A: 

Java serialisation is a representation of the class/data structures and tightly tied to the virtual machine. Consequently that's going to be difficult to translate to the Python world.

  1. Your Java program can serialise/deserialise in XML, which would be translatable. Check out JAXB or XStream
  2. Have you looked at the possibility of running in Jython - the Python implementation in Java
Brian Agnew
+3  A: 

Java binary serialization is really designed to be used with Java. To do it in Python you'd have to have all the relevant Java classes available for inspection, and create Python objects appropriately - it would be pretty hideous and fragile.

You're better off using a cross-platform serialization format such as Thrift, Protocol Buffers, JSON or XML. If you can't change which serialization format is used in the Java code, I'd suggest writing new Java code which deserializes from the binary format and then reserializes to a cross-platform format.

Jon Skeet
A: 

If I were you, I'd read the data with Jython, and either reserialize it with pickle ( so that you can read it from Python ), or in a language neutral format, like XML.

Geo
+1  A: 

If you are using Java classes, then I don't even know what it would mean to deserialize a Java class in a Python environment. If you are only using simple primitives (ints, floats, strings), then it probably wouldn't be too hard to build a Python library that could deserialize the Java format.

But as others have said, there are better cross-platform solutions.

Ned Batchelder
+2  A: 

You don't say whether you have control over the serialization of the data or not, but if you do, JSON seems to be a nice format which is cross platform and has a good balance between human readable and machine readable. For java and distributed with python.

Nick Craig-Wood
+1 Who downvoted this? The question is ambiguous and this is certainly a valid response. The OP doesn't give any description as to what is being serialized. I suppose you can assume that the OP is referring to java.io.Serializable, but he/she could just as easily be referring to some proprietary wire protocol.
Joe Holloway
A: 

An other option is to use Perspective Broker from Twisted. Their's a Java implementation. If you just need serialisation/deserialisation you can use only Banana (protocol) or Jelly (persistence) directly. Jelly is a S-expression-based persistence of objects.

Etienne