Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Difference of import usage in Python 1 56
detecting  the added row index in a datagridview 3 54
Java Timer (static) 9 36
windows explorer path to command prompt 5 27
This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

943 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

4 Experts available now in Live!

Get 1:1 Help Now