?
Solved

Is it possible to serialize an object in Python so it can be read in Java?

Posted on 2014-12-25
3
Medium Priority
?
1,025 Views
Last Modified: 2014-12-28
Hello,

I was wondering if it was possible to serialize a Python dictionary, so that it can be read in as a HashMap object in Java?

I'm a little skeptical, but I've seen Python do some amazing things...

Appreciate any information confirming this one way or the other.
0
Comment
Question by:Errang Genevre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 40517964
Why not use something like XML or JSON as the serailization format. Then it should be trivial to convert from one environment to the other. If you're looking for binary serialization, then you would have to know the inner working of Java's and/or Python's implementation .
0
 

Author Comment

by:Errang Genevre
ID: 40518066
Well, I can't use an intermediate format; because I'm trying to enhance a large project, and the rest of the project is in Java.

I take it there's no library that would do the converting?
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1600 total points
ID: 40518131
What type of objects are you trying to serialize in your dictionary and can you use jython -- http://www.jython.org ?  

If you're trying to serialize simple objects (strings, integers, or java-based objects rather than python objects) and jython is an option, then you can create a HashMap and use java's serialization methods to serialize it and it should be readable.   For example, the following is working for me:

jython
C:\jython2.5.3>jython
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_25
Type "help", "copyright", "credits" or "license" for more information.
>>> import java.util
>>> h = java.util.HashMap()
>>> d = {"a": 1, "b": 2, "c": 3}
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> for (k,v) in d.items(): h.put(k, v)
...
>>> h.get("a")
1
>>> h.keySet()
[a, b, c]
>>> h.values()
[1, 2, 3]
>>> fout = java.io.FileOutputStream("map.bin")
>>> oos = java.io.ObjectOutputStream(fout)
>>> oos.writeObject(h)
>>> oos.close()
>>> quit()

Open in new window


java
import java.util.*;
import java.io.*;

public class TestSerialize {

   public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
   {
        HashMap h;
	FileInputStream fis = new FileInputStream("map.bin");
	ObjectInputStream ois = new ObjectInputStream(fis);
        h = (HashMap) ois.readObject();
        System.out.println(h.get("b"));
   }
}

Open in new window


If jython isn't an option, you could also probably use py4j (http://py4j.sourceforge.net/), JPype (http://jpype.sourceforge.net/) or pyjnius (http://txzone.net/2012/08/pyjnius-accessing-java-classes-from-python/).  I've had  good luck with pyjnius in a number of projects.  

Essentially, they'll all be doing the same thing.  You won't really be using python to do the serialization but instead python will be instantiating a jvm and you'll be marshalling your python object into the jvm and using the jvm to do the serialization.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question