Passing java object to python -


i prototyping interface our application allow other people use python, our application written in java. pass of our data java app python code unsure how pass object python. have done simple java->python function call using simple parameters using jython , found useful trying do. given class below, how can use in python/jython input function/class:

public class testobject  {    private double[] values;    private int length;    private int anothervariable;     //getters, setters  } 

one solution. use sort of message system, queue, or broker of sort serialize/deserialize, or pass messages between python , java. create sort workers/producer/consumers put work on queues processed in python, or java.

also consider checking out inspiration: https://www.py4j.org/

py4j used heavily by/for pyspark , hadoop type stuff.

to answer question more immediately.

example using json-simple.:

import org.apache.commons.io.fileutils; import org.json.simple.jsonobject;  //import org.json.simple.jsonobject;   public class testobject  {    private double[] values;    private int length;    private int anothervariable;    private boolean somebool;    private string somestring;     //getters, setters     public string tojson() {        jsonobject obj=new jsonobject();        obj.put("values",new double(this.values));        obj.put("length",new integer(this.length));        obj.put("bool_val",new boolean(this.somebool));        obj.put("string_key",this.somestring);        stringwriter out = new stringwriter();        obj.writejsonstring(out);        return out.tostring();    }     public void writeobject(){           writer writer = new bufferedwriter(                               new outputstreamwriter(                                   new fileoutputstream("anobject.json"), "utf-8")                               )                            )            writer.write(this.tojson());    }     public static void setobject(){        values = 100.134;        length = 12;        anothervariable = 15;        somestring = "spam";    }  } 

and in python:

class dostuffwithobject(object):     def __init__(self,obj):         self.obj = obj         self.changeobj()         self.writeobj()      def changeobj(self):         self.obj['values'] = 100.134;         self.obj['length'] = 12;         self.obj['anothervariable'] = 15;         self.obj['somestring'] = "spam";      def writeobj(self):         ''' write file '''         open('anobject.json', 'w') f:             json.dump(self.obj, f)      def someothermethod(self, s):        ''' else '''        print('hello {}'.format(s))  import json open('anobject.json','r') f:     obj = json.loads(f.read()) # print out obj['values'] obj['somebool'] ... key in obj:     print(key, obj[key])  athing = dostuffwithobject(obj) athing.someothermethod('there') 

and in java read object. there solutions exist implementing idea (json-rpc, xml-rpc, , variants). depending, may may want consider using ( http://docs.mongodb.org/ecosystem/drivers/java/ ) benefit being mongo json.

see:

a more comprehensive list of queues:

resources referenced:


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -