(WID/WPS) Serialization & Deserialization


This post shows the sample Java code for Serialization & Deserialization.


//String to DataObject (Deserialization)
private DataObject deserialize(String serializedBO)
{
DataObject dataObject = null;
try {
if(serializedBO != null) {
ByteArrayInputStream bais = new ByteArrayInputStream(serializedBO.getBytes());
BufferedInputStream bis = new BufferedInputStream(bais);
BOXMLSerializer serializer = (BOXMLSerializer)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOXMLSerializer");
dataObject = serializer.readXMLDocument(bis).getDataObject();
bis.close();
}
}
catch (IOException e) {
e.printStackTrace(System.out);
}
return dataObject;
}

//DataObject to String (Serialization)
private String serialize(DataObject dataObject)
{
String serializedBO = null;
if(dataObject != null)
{
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedOutputStream bos = new BufferedOutputStream(baos);
BOXMLSerializer serializer = (BOXMLSerializer)ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOXMLSerializer");
//InputData is the BusinessObject name.
serializer.writeDataObject(dataObject,"http://JMSSampleLibrary/bpc/samples","InputData",bos);
bos.flush();
bos.close();
serializedBO = baos.toString("UTF-8");
}
catch (IOException e) {
e.printStackTrace(System.out);
}
}
return serializedBO;
}

No comments:

Post a Comment