Sending java object to python and receive it back - java

Consider an object in java which implements Serializable. I want to send that object to a python code on TCP socket. I am serializing object and sending on TCP by using :
//socket connection code
PySessionObject object = new PySessionObject();
object.setMethodCall(PyServerMethodConstant.SETATTRIBUTE);
object.setAttributeName(name);
object.setAttributeValue(value);// value is of object type which also implements Serializable
os = sChannel.socket().getOutputStream();
oos = new ObjectOutputStream(os);
oos.writeObject(object);
oos.flush();
os.flush();
//socket closing and exception handling
Now I want to convert this byte stream into object in python perform some operation on that object and send it back to java world and deserialize it again. For this purpose I come to about javaobj-py3, with this everything is fine until I pass LinkedHashMap in setAttributeValue(). In Python I am doing this:
total_data=b''
while True:
data = self.clientsocket.recv(8192)
if not data: break
total_data += data
pyobj = javaobj.loads(total_data)
For this I am getting exception as:
RuntimeError: Unknown OpCode in the stream: 0x8 (at offset 0x14C)
What will be cause? Is it like opcodes are not found for "something"? Is anyone can suggest any other idea to convert byte stream send from java into object in python world perform some operation on that object and send it back to java world and deserialize it again.

You can convert the object to a JSON string and then send over the wire.
To convert object to json
new GSONBuilder().create().toJSON(obj);
To convert json to object
new GSONBuilder().create().fromJSON(jsonString, YouObject.class);
I prefer using GSON for converting a object to json and reverse in java. In python you can use json library. The methods are
json.loads(string) and json.dumps(object)

Related

How to Serialize/Deserialize an object without implementing Serializable interface?

If a mail is send to my inbox, I recieve a message, and I'm inserting the contents into DB.
I have a org.springframework.integration.core.Message something like follows:
public void receive(Message<?> message)
{
//I am inserting message contents into DB
}
Now in the event of failure, I wanted to have fail safe recovery mechanism, what I am thinking is to serialize the Message object into a file and later deserialize and update to DB.
Question
1. In this situation how to serialize the Message object?
2. Other than serialization any other mechanism that can be used?
EDIT
I have not done Serialization before, I heard like the class should implements Serializable in order to use ObjectOutputStream, in this case I don't want to create a subclass of Message, So how to serialize Message to File?
Sure, there are many serialization mechanisms apart from the jvm one.
XML
JSON
BSON
MessagePack
protobufs
...
Some of them are text-based, some are binary. All have drawbacks and pluses. Text-based ones are human-readable, binary ones are faster and take up less space.
There are java libraries that handle all the above formats: JAXB (XML), Jackson (JSON), etc.
In this situation how to serialize the Message object? Other than serialization any other mechanism that can be used?
Extract all the data you need from the Message and save it. You can do this in any manner you choose.
You can deserialize it by populating a new Message with the data you saved.
I don't know if I probably understood it al right.. but assuming Message is not much more than lots of strings and some integers you can just use directly an ObjectOutputStream and write it to a file (binary) and then readin later. Why not?
Message e = new Message();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("whatever");
oos.writeObject(message);
// read in
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("whatever");
Message e = (Message) ois.readObject();

Python equivalent of java ObjectOutputStream and ObjectInputStream?

In java I can transfer objects between server and client by using Object Output Stream and Object Input Stream. Is there anything equivalent in python?
Related:
python equivalent of java OutputStream?
The pickle module in Python provides object serialization and deserialization functionality. http://docs.python.org/library/pickle.html
It's not particularly secure, so you should always validate the incoming data, but it should support your needs.
The multiprocessing module has the Pipe() function that handles serializing and passing objects between processes.
http://docs.python.org/library/multiprocessing.html#multiprocessing.Pipe
example (pipes work within the same process too)
import multiprocessing
class ObjectToSend:
def __init__(self,data):
self.data = data
obj = ObjectToSend(['some data'])
#create 2 read/write ends to a pipe
a, b = multiprocessing.Pipe()
#serialize and send obj across the pipe using send method
a.send(obj)
#deserialize object at other end of the pipe using recv method
print(b.recv().data)

groovy soapUI deserialize

Just started using soapUI and I like it a lot.
In a particular case using REST, I'm receiving serialized object.
I would like :
to retrieve the serialized byte array and transform it into a Java object
re-transform the java object into an XML response (using JAXB)
so it can be human readable.
Is this feasible?
Be sure to consider using XML serialization (e.g. XStream) instead of binary one to avoid version compatibility problems before using the next solution:
Import your Java class to SoapUI groovy script (as described there) or re-define your Java class in Groovy code with Serializable interface implemented:
class Person implements Serializable { String name; int age }
Use ObjectInputStream and classLoader to load deserialize objects into object:
// use your byte array variable instead of yourByteArray
input = new ByteArrayInputStream(yourByteArray)
// use your object variable instead of yourObject
yourObject = null
input.withObjectInputStream(getClass().classLoader){ ois -> yourObject = ois.readObject() }
Use ObjectOutputStream to serialize updated objects and save them to an XML response:
output = new ByteArrayOutputStream()
output.withObjectOutputStream { oos -> oos << yourObject }
//save serialized data as byte array
output.toByteArray()

Converting a byte stream to an Object

I have a Packet class (com.lab.tracking.Packet) in an Android project. I need to send this packet via UDP to a Server, that has an exactly copy of that Packet class (com.server.serverTracking.Packet).
The android app converts the Packet into a byte stream, and then it sends it to the server. The server receives the byte stream correctly, but when it tries to convert it to a Packet, it throws an exception:
java.lang.ClassNotFoundException: com.lab.tracking.Packet
I understand that. The server is trying to convert this object to a com.lab.tracking.Packet, instead of a com.server.serverTracking.Packet).
The conversion from byte stream to Object is:
public static Object toObject(byte[] dataReceived) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(dataReceived);
ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
How could I avoid this? Both classes are exactly the same, so I think the problem is in the complete name (or ID, or whatever).
Thanks!
To expand on djg's answer, the problem is that your Packet classes are not "exactly the same" because they are in different packages.
So the solution would be to get rid of com.server.serverTracking.Packet and move com.lab.tracking.Packet to a common library that both the android app and the server side code depend on.
That being said, I would recommend against using Java object serialization between and android app and the server. If your Packet changes in any significant way (new member variables, etc.) with new releases, you will likely break all previous versions. At the very least, you should explicitly specify a serialVersionUid.
A better solution would be to design a structure for the packet byte array, and use a factory class or constructor that parses the byte array into a Java object.
Put the packet implementation in a core library that both the android project and server depend on.

serialize java object to text file

I have a java library, I would like to save an instance of a java object to a text file. I have tried to use all java libraries for serialization and deserialization to xml: http://karussell.wordpress.com/2009/09/03/xml-serializers-for-java/ but deserializing does not work. I have posted a question here: http://stackoverflow.com/questions/6139702/deserializing-xml-file-from-xstream but It seems that I could not get a solution for that.
I also have tried to serialize to json but deserialize from json does not work.
So, I would like to know apart of serializing to xml and json, is there any other way to do serialization and deserialization from a java object (cannot modify to add tags: #XmlRootElement(name="customer")) to text file?
Thanks in advance!
The easiest way is probably to use the native Java serialization. It will generate a binary representation of the object, but you can encode the generated byte array with Base64 to transform it to text:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(myObject);
oos.flush();
byte[] binary = baos.toByteArray();
String text = Base64.encodeBase64String(binary); // Base64 is in the apache commons codec library
// save text to a file
Note that the object, and every object it references (recursively) must implement java.io.Serializable for this to work.
You can use Gson to convert java object to Json and vice versa
Here is example from the Gson user guide.
Or may be apache digester can help.

Categories

Resources