Solved

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

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

706 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

11 Experts available now in Live!

Get 1:1 Help Now