Search code examples
javajsonorg.json

How to put a limit on memory usage while parsing a JSON string?


How can I rule out a java.lang.OutOfMemoryError when calling

new JSONObject(longAndMalformedJSONString)

I'm using the org.json implementation of a JSON parser.

I'm not looking for a way to decode the bad JSON String. I just want to put an upper limit on memory usage (and possibly CPU usage) and maybe get an exception that I can recover from.

Or, alternatively, is it safe to say, memory usage while parsing will never exceed a certain ratio compared to input string length? Then I could just limit that.

Or is there an alternate library that offers that?


Solution

  • There are two approaches when reading serialized data (JSON, XML, whatever): you either parse the entire input and keep the object in memory, or you navigate the stream via the provided API and you just keep the pieces you are interested in. It seems org.json doesn't have a streaming API, but more sophisticated libraries like Gson do:

    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    List<Message> messages = new ArrayList<Message>();
    reader.beginArray();
    while (reader.hasNext()) {
      Message message = gson.fromJson(reader, Message.class);
      messages.add(message);
    }
    reader.endArray();
    reader.close();
    

    You can also also put limits on the input, but it depends on the protocol you use for transferring the JSON payload