Ga Sacchi Ga Sacchi - 7 months ago 140
Java Question

Gson JSON max size

I need to extract some data from a Clob and serialize it in JSON format.

What's the maximum size Gson can handle?

Here https://github.com/google/gson/blob/master/UserGuide.md I can just find
"Strings: Deserialized strings of over 25MB without any problems"

Context: I use..

ResultSet.getClob()
-> BufferedReader
-> String singleLine
-> StringBuilder
-> String "jsonAttribute" to serialize


More in detail:

try{

StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader( resultset.getClob(2).getCharacterStream() );
String line;
try{
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
}catch(IOException ee){
// logger
throw ee;
}

String jsonAttribute = sb.toString();

}catch(Exception xx){..}


Note: in my current code the limitation is Integer.MAX_VALUE

My solution will consit in using chunks of the data retrieved from the DB. I would like to know the theoretical max size that GSON can handle. I won't use a browser on the receiving side.

Answer

Gson doesn't impose any limit. Nor it has any known arquitectural limitation.

I think the main problem you will face with your approach is loading the data into memory in the first place.

I recommend using Gson stream API to write the JSON as you read it from the database. Use a JsonWriter and create and stream your JSON object.

Reader reader = resultset.getClob(2).getCharacterStream();
JsonWriter jsonWriter = new JsonWriter(someOutputStream);
copyStream(reader, someOutputStream);

Where copyStream could be something like

public static void copyStream(Reader istream, Writer ostream) throws IOException {
  char buffer[] = new char[2048];
  while (true) {
    int len = istream.read(buffer);
    if (len == -1)
      return;
    ostream.write(buffer, 0, len);
  }
}
Comments