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()
Related
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)
I stored the java object in hbase (i.e) Let's say I have an object 'User' with 3 parameters like firstname, middlename and lastname. I used the following code for serialization in java
Object object = (object) user;
byte[] byteData = SerializationUtils.serialize((Serializable) object);
and stored in hbase like 'storing complete object (in byte[] format of above) in the Value portion of the KeyValue pair'
It is stored in hbase like (Example)
column=container:container, timestamp=1480016194005, value=\xAC\xED\x00\x05sr\x00&com.test.container\x07\x89\x83\xFA\x7F\xD0F\xA5\x02\x00\x08I\x00\x07classIdJ\x00\x14dateTimeInLongFormatZ\x00\x04rootZ\x00\x09undefinedL\x00\x03keyt\x00\x12Ljava/lang/String;L\x00\x04modeq\x00~\x00\x01L\x00\x04nameq\x00~\x00\x01L\x00\x06userIdq\x00~\x00\x01xp\x00\x00\x00\x02\x00\x00\x01X\x967\xBA\xF0\x00\x00t\x00\x1Econtainer_393_5639181044834024t\x00\x06expandt\x00\x02ert\x00\x08testadmin
when I try to retrieve the data, I used the following deserialization in java and converted back to object of readable format
object = SerializationUtils.deserialize(bytes);
I would like to retrieve the data stored in java format via happybase using python and I achieved it and received the data as available in hbase like
It is stored in hbase like (Example)
column=container:container, timestamp=1480016194005, value=\xAC\xED\x00\x05sr\x00&com.test.container\x07\x89\x83\xFA\x7F\xD0F\xA5\x02\x00\x08I\x00\x07classIdJ\x00\x14dateTimeInLongFormatZ\x00\x04rootZ\x00\x09undefinedL\x00\x03keyt\x00\x12Ljava/lang/String;L\x00\x04modeq\x00~\x00\x01L\x00\x04nameq\x00~\x00\x01L\x00\x06userIdq\x00~\x00\x01xp\x00\x00\x00\x02\x00\x00\x01X\x967\xBA\xF0\x00\x00t\x00\x1Econtainer_393_5639181044834024t\x00\x06expandt\x00\x02ert\x00\x08testadmin
Is there a way to deserialize the java object via python
Thanks Much
Hari
There is a Python library for that:
https://pypi.python.org/pypi/javaobj-py3/
Usage seems pretty easy with:
import javaobj
jobj = self.read_file("obj5.ser")
pobj = javaobj.loads(jobj)
print(pobj)
I know that Scalding's default serialization uses Kryo. So for this example, lets say I have a pipe of student objects.
case class Student(name:String, id:String)
val pipe: Pipe[Student] = //....
Then I write that pipe to a TextDelimited file using Kryo.
pipe.write(args("output"))
Now that I have these "output" files, how do I easily read them back into Student objects? My general idea is something like the following but it does not work.
Tsv("Kryo output files").read.map {bytes =>
val instantiator = (new ScalaKryoInstantiator).setRegistrationRequired(true)
val kryo = instantiator.newKryo
kryo.register(classOf[Person])
val deserialized = kryo.readObject(new Input(bytes), classOf[Person])
deserialized
}
How do I deserialize my Kryo written text files into the objects they were originally in?
I have a Map:
Map<String, DistributorAdd> map= new TreeMap<String, DistributorAdd>();
and I save it in a file.txt
FileOutputStream fos = new FileOutputStream("Distrib.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.close();
The problem is that Distributor who was yesterday like:
public DistributorAdd(String distributor, String
emailAdress, String name, String speciality){...}
Will be tomorrow like this:
public void ajouter(String Distributor, String EmailAdress,
String Name, String Phone, String Image) {..}
My coworker already placed a lot of info in her Distrib.txt so what I want is to be able to put a new String in the Map without destroying it.
I would like to keep Distrib.txt and my DistributorAdd function is there any easy step I could do to do that?
The kind of error I get is:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Distrib.txt"));
VendorA = (DistributorAdd) ois.readObject();
Error:
IOException : table.java => table()java.io.StreamCorruptedException: invalid stream header: ACED0573
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at car.Table.<init>(Table.java:185)
at car.Table.main(Table.java:837)
If you have any question or any more information that I need to give I will be happy to do it.
Please note following things about serialization:
1)It has to be used very judiciously.
2)When we serialise a class , it is like we are exporting its API in terms of
instance variables.
3)Always explicitely declare SerialVersionUID in your serializable class,if we
don't it will be automatically calculated by JVM based on internal structure of your class
(instance vars,public methods etc.) so changing internal strcture of class
chnages this SerialVersionUID.
Hence , If we serailise a class in one version and then deserialise it in another version
(by version change I mean we are changing some internal structure of serializable
object) , version incompatibility is bound to happen.
I am not sure what exactly may have caused for your code to fail since I don't know
what chnages are you doing in your Serializable class.
But I think you should consider point 3 once
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.