question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonReader.nextString() producer/consumer alternative

See original GitHub issue

Hi,

JsonReader is a great class to read JSON data in streaming fashion. However, there are some cases when tokens are too big to fit in JVM memory making the JSON reader streaming idea neutralized. For example, here is a question at StackOverflow asking for managing huge fields somehow: http://stackoverflow.com/questions/39615673/best-way-to-handle-huge-fields-with-gson-jsonreaderString JsonReader.nextString() reads the whole huge literal input into a StringBuilder instance that makes the JVM crash on OutOfMemoryError.

Do you consider adding a streaming alternative, say void JsonReader.nextString(StringChunksConsumer) where the StringChunksConsumer interface is expected to be something like the interface below, a valuable idea?

interface StringChunksConsumer {

    void accept(char[] buffer, int offset, int length)
            throws IOException;

}

The idea of this method would be passing parsed string literal chunks to the consumer rather than aggregating them into a string builder. The StringChunksConsumer.accept method signature would perfectly match:

  • StringBuilder.append(char[], int, int)
  • Writer.write(char[], int, int)
  • etc.

For example, a simple redirecting from a reader to a writer could be just read.nextString(writer::write); consuming no more memory than JsonReader.nextString() uses internally.

Thanks!

Update 1

A similar problem: https://stackoverflow.com/questions/43470927/android-java-lang-outofmemoryerror-failed-to-allocate-with-free-bytes-and-70m

Update 2

This would also allow to work with inner string-encoded (for whatever reason) JSON documents more efficiently. Example: http://stackoverflow.com/questions/43929916/gson-parsing-error-for-my-json-string

Update 3

This possibly duplicates #415 and #718

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
swankjessecommented, Dec 12, 2016

Interesting idea. I think the most likely thing we’d build would be a method like nextReader() that returned a Reader on the characters of a string.

0reactions
Marcono1234commented, Aug 3, 2022

@lyubomyr-shaydariv, even though Gson is now in maintenance mode, would you mind if this issue remains open? Maybe this could still be implemented eventually if the project members find this useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How Can i pace a producer actor so that it does not pump to ...
I have an actor that generates UpdateItemMessage , this message is consumed by multiple child actors . my problem is the Producer is...
Read more >
JsonReader (Gson 2.8.0 API) - Javadoc.io
Consumes the next token from the JSON stream and asserts that it is the beginning of a new object. void, close(). Closes this...
Read more >
pact-jvm
So, I?m currently spiking Consumer Contract Driven Testing for a new project ... BTW, I am planning to use the pact-jvm-provider-spring as an...
Read more >
19.3 Using the Object Model API - Oracle Help Center
The following code demonstrates how to create an object model from JSON data in a text file: import java.io.FileReader; import javax.json.Json; import javax....
Read more >
Proper JSON and property bags - ample code - WordPress.com
A producer of JSON documents only does serialization, a consumer only ... What I would like to explore, then, are some alternatives with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found