Solved

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

Posted on 2014-12-25
3
723 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 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