JsonReader: Support JSON intermixed with other content
See original GitHub issueI have a fairly large HTML page that has several JSON objects & arrays in it. I
use a PushbackReader to read the file and handle any text I'm interested in
(and determine when to start a JsonReader read). If I pass my Reader (or any
reader) to JsonReader, it will read 1024 characters regardless of content. This
means I may miss some JSON.
i.e. I may miss myObject2 in the following example because it might remain
unprocessed in the JsonReader buffer.
<h1>some html</h1>
<script language=javascript>
var myObject1={"a":1};
alert("some non-JSON that may or may not exceed the JsonReader buffer size");
var myObject2={"b":2};
NOTE: This is a much simplified version of my problem. The JSON data is
actually quiet large as is the HTML content.
As a temporary workaround, I've added the following method to JsonReader that
allows me to push the unprocessed characters in the JsonReader buffer back into
my PushbackReader (so I can start searching for "var myObject2=").
public char[] getUnreadCharacters() {
char[] output = new char[limit - pos];
if (pos < limit) {
System.arraycopy(buffer, pos, output, 0, limit - pos);
}
return output;
}
I also added a constructor that allows me to modify the buffer size.
Original issue reported on code.google.com by christia...@gmail.com
on 6 Oct 2013 at 3:44
Issue Analytics
- State:
- Created 9 years ago
- Comments:9
Top Results From Across the Web
JsonReader - Apollo GraphQL
Reads a JSON RFC 7159 encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls)...
Read more >JsonTextReader gets mixed up after networking error
I am trying to make a program that uses data to find solutions to large 3D mathematical nets. In JSON format, there is...
Read more >Read Multiple Fragments With JsonReader - Json.NET
This sample sets SupportMultipleContent to true so that multiple JSON fragments can be read from a Stream or TextReader.
Read more >Using JSON.NET for dynamic JSON parsing - Rick Strahl
In this post I'll discus JToken, JObject and JArray which are the dynamic JSON objects that make it very easy to create and...
Read more >10 Java API for JSON Processing - Oracle Help Center
import java.io.FileReader; import javax.json.Json; import javax.json.JsonReader; import javax.json.JsonStructure; ... JsonReader reader ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure if that’s a snipe or you are being serious, but that won’t work.
The issue is that gson/stream/JsonReader.fillBuffer (line 1300) reads up to 1024 bytes ahead from the caller-supplied Reader (up to buffer.length), but unconsumed bytes are never returned to that Reader. Unless I’m missing something, they are simply thrown away with the JsonReader, leaving the caller-supplied Reader in an unknown/unusable state.
https://github.com/google/gson/blob/2b15334a4981d4e0bd4f2c2d34b8978a4167f36a/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1300
Supplying one byte at a time from the Reader still means up to 1024 bytes will get read from it and thrown away, leaving the Reader positioned some arbitrary number of characters past the consumed input.
Therefore, the request here is to return unconsumed characters back to the caller-supplied Reader, so that the caller has access to the next byte following the consumed JSON object (presumably using reader.mark/.reset, if reader.markSupported() is true).
+1
JsonReader
should not advance the underlyingReader
past the end of the JSON that has been read.