Solved

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

Posted on 2014-12-25
3
679 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
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 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 400 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 29
ASP/VB email question 4 30
Adding multiple JVM environments to RedHat 6 7 17
Chrome and Firefox Java 5 23
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now