When streaming a Json file with GSON's JsonReader, can you dump an object to string?

I am working in Java using the GSON library to process a huge JSON document.

I absolutely cannot load this into memory, it is upwards of 8G and will crash the JVM with an OOM if I try to.

So I use JsonReader to process the stream of JSON.

There are certain times when I reach a BEGIN_OBJECT that want to just dump the contents of the object to string instead of processing each individual element.

For example, I am parsing something like this:

[ { "SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.7324.1200","ErrorInfo":{ "ErrorMessage":"Access denied. You do not have permission to perform this action or access this resource.","ErrorValue":null,"TraceCorrelationId":"03ab459e-7076-5000-c1a7-196f1fc54384","ErrorCode":-2147024891,"ErrorTypeName":"System.UnauthorizedAccessException" },"TraceCorrelationId":"03ab459e-7076-5000-c1a7-196f1fc54384" } ]

When I get to ErrorInfo, I just want that as a string instead of parsed out. I don't want to have to parse each piece.

So here is the code:

try (InputStream is = (InputStream)response.getEntity(); InputStreamReader inputStreamReader = new InputStreamReader(is); JsonReader jsonReader = new JsonReader(inputStreamReader)) { if (jsonReader.peek() != JsonToken.BEGIN_ARRAY) { return; } jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); // Start of the default object on every request jsonReader.skipValue(); // name SchemaVersion jsonReader.skipValue(); // value jsonReader.skipValue(); // name LibraryVersion jsonReader.skipValue(); // value jsonReader.skipValue(); // name ErrorInfo if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { // I want to dump the upcoming object to string here
...

Is it possible to dump the contents of the json reader for a given begin object until it ends to string?

Similar question, but not quite the same: Get a dump of a section (object) of JSON

3

1 Answer

Special thanks to for the answer in the comments. You can use Gson.fromJson(jsonReader, Map.class) for this exact situation.

Gson gson = new Gson();
try (InputStream is = (InputStream)response.getEntity(); InputStreamReader inputStreamReader = new InputStreamReader(is); JsonReader jsonReader = new JsonReader(inputStreamReader)) { if (jsonReader.peek() != JsonToken.BEGIN_ARRAY) { return; } jsonReader.beginArray(); while (jsonReader.hasNext()) { Map header = gson.fromJson(jsonReader, Map.class); if (header.get("ErrorInfo") != null) { String errorDump = ((Map)header.get("ErrorInfo")).toString(); // Now I have the error dump I need but I also // didn't load the entire thing into memory!
...

more info:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like