Best Practice - String vs InputStream in Json Parsing (using gson) Best Practice - String vs InputStream in Json Parsing (using gson) json json

Best Practice - String vs InputStream in Json Parsing (using gson)


The lesson learned by XML users is that large object trees in-memory can take up lots of memory.

JSON parse trees don't intrinsically take up less memory than XML, but it's usually simpler. An XML DOM is quite featureful compared to a GSON JsonObject for instance. GSON may (I don't know) use a streaming parser (similar to SAX for XML) that loads only what is needed.

But the point I'm trying to make is that we've learned since then. Reasons JSON is typically loaded as a string include: parsers are more efficient, fewer features than a full DOM are needed in most cases, hardware is more powerful, JSON files are usually shorter, and programmers are lazier.

That said, I found this post when I realized I have to work in complex ways with JSON data sets that are too big to efficiently store in a single string. You shouldn't do that, but I'm grateful JsonParser.parse() has an implementation that can also take an InputStream.