gsonを使用してjsonをJavaオブジェクトに解析しました。
今、Eclipse でヒープ メモリの問題に直面しています: GSON を使用した Java ヒープ スペース
gson-streaming の記事を参照しているように
どちらも、次のようなストリーミングについて議論しています。
Gson ストリーミング処理は高速ですが、JSON データの処理のすべての詳細を処理する必要があるため、コーディングが困難です。
クライアントとサーバーの通信に通信オブジェクトを使用しました。通信構造はこんな感じ
class CommunicationObject
{
//Other variables
IsBean isBean; //Interface of hibernate entity bean
//Getter & setter methods
}
ここでは、任意の単一エンティティ Bean にアクセスする際のジェネリックとしての isBean です。例はありません
->Do login logic then isBean has a user object
->Get organization then isBean has a Organization object.
...
etc
リクエストメソッドを処理するコード:
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;
}
オブジェクトをjsonに変換
private String convertIntoJson(CommunicationObject obj) throws IOException
{
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(outputStream);
writeJsonStream(out, obj);
return outputStream.toString();
}
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();
}
gson.toJson(communicationObject, CommunicationObject.class, writer)に行き詰まって います。そのようなリクエスト オブジェクトでストリーミングを行うにはどうすればよいですか? または利用可能な他の代替手段はありますか?問題のクエリがあれば教えてください。