I have parsed json to java object using gson.
Convert Json to Java Sample Code
Now I am facing heap memory issue in eclispe, same as: Java heap space with GSON
As I refer article of gson-streaming like
Both have discussion on streaming like,
Gson streaming processing is fast, but difficult to code, because you need to handle each and every detail of processing JSON data.
I have used a communication object for client & server communication. The communication structure is like
class CommunicationObject
{
//Other variables
IsBean isBean; //Interface of hibernate entity bean
//Getter & setter methods
}
Here isBean as a generic while accessing any single entity bean. there are no of cases
->Do login logic then isBean has a user object
->Get organization then isBean has a Organization object.
...
etc
Code to process request method:
public String processFacadeActionTest(String requestJson) {
Gson gson = new GsonBuilder().registerTypeAdapter(IsBean.class, new InterfaceAdapter<IsBean>())
.create();
CommunicationObject requestObj=null;
try{
requestObj=gson.fromJson(requestJson, CommunicationObject.class);
}catch (Exception e) {
e.printStackTrace();
}
FacadeActionHandler actionHandler=new FacadeActionHandler();
CommunicationObject responseObj = actionHandler.handleRequest(requestObj);
String returnString="";
try{
//TODO:Comment Bhumika heap space issue
returnString=convertIntoJson(responseObj);
return returnString;
}catch (Exception e) {
e.printStackTrace();
}
return returnString;
}
Convert object to json
private String convertIntoJson(CommunicationObject obj) throws IOException
{
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(outputStream);
writeJsonStream(out, obj);
return outputStream.toString();
}
streaming using gson
private void writeJsonStream(OutputStream out,CommunicationObject communicationObject) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndent(" ");
writer.beginArray();
Gson gson = new GsonBuilder().registerTypeAdapter(IsBean.class, new InterfaceAdapter<IsBean>())
.create();
gson.toJson(communicationObject, CommunicationObject.class, writer); //Hear stuck how to map isBean object
writer.endArray();
writer.close();
}
I am stuck on gson.toJson(communicationObject, CommunicationObject.class, writer) how can I do streaming on such request object? or any other alternative available? Let me know still any query in question.