Ga Sacchi Ga Sacchi - 9 months ago 169
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 I can just find
"Strings: Deserialized strings of over 25MB without any problems"

Context: I use..

-> BufferedReader
-> String singleLine
-> StringBuilder
-> String "jsonAttribute" to serialize

More in detail:


StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader( resultset.getClob(2).getCharacterStream() );
String line;
while ((line = br.readLine()) != null) {
}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.


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 =;
    if (len == -1)
    ostream.write(buffer, 0, len);