2

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

  1. gson-streaming
  2. gson-streaming-to-read-and-write-json

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.

Community
  • 1
  • 1
bNd
  • 7,512
  • 7
  • 39
  • 72
  • Does `isBean` represent your data structure? Can you post the structure? – home Dec 17 '12 at 06:02
  • isBean is a interface, which is implemented on user, organization, privilege or document... etc. objects. I created a single communication object for conversion between server and client[GWT Technology]. And isBean is a part of communication object. plz refer updated post and my implementation something like this http://stackoverflow.com/questions/13873887/issue-with-parse-json-to-java-object-using-gson. – bNd Dec 17 '12 at 06:12
  • 3
    Down voter please explain the reason. – bNd Dec 24 '12 at 05:53

0 Answers0