Top Description Fields Constructors Methods
com.google.gson

public final Class JsonStreamParser

extends Object
implements Iterator<JsonElement>
Class Inheritance
All Implemented Interfaces
java.util.Iterator
Imports
com.google.gson.internal.Streams, com.google.gson.stream.JsonReader, .JsonToken, .MalformedJsonException, java.io.IOException, .Reader, .StringReader, java.util.Iterator, .NoSuchElementException

A streaming parser that allows reading of multiple JsonElements from the specified reader asynchronously. The JSON data is parsed in lenient mode, see also JsonReader#setStrictness(Strictness).

This class is conditionally thread-safe (see Item 70, Effective Java second edition). To properly use this class across multiple threads, you will need to add some external synchronization. For example:

JsonStreamParser parser = new JsonStreamParser("['first'] {'second':10} 'third'");
JsonElement element;
synchronized (parser) {  // synchronize on an object shared by threads
  if (parser.hasNext()) {
    element = parser.next();
  }
}
Authors
Inderjeet Singh, Joel Leitch
Since
1.4

Field Summary

Modifier and TypeField and Description
private final Object
private final JsonReader

Constructor Summary

AccessConstructor and Description
public
JsonStreamParser(String
The string containing JSON elements concatenated to each other.
json
)

public
JsonStreamParser(Reader
The data stream containing JSON elements concatenated to each other.
reader
)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if a JsonElement is available on the input, false otherwise
hasNext
()

Implements java.util.Iterator.hasNext.

Returns true if a JsonElement is available on the input for consumption

public JsonElement

Returns:

the next available JsonElement on the reader.
next
()

Implements java.util.Iterator.next.

Returns the next available JsonElement on the reader.

public void
remove()

Overrides default java.util.Iterator.remove.

This optional Iterator method is not relevant for stream parsing and hence is not implemented.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lockback to summary
private final Object lock
parserback to summary
private final JsonReader parser

Constructor Detail

JsonStreamParserback to summary
public JsonStreamParser(String json)
Parameters
json:String

The string containing JSON elements concatenated to each other.

Since
1.4
JsonStreamParserback to summary
public JsonStreamParser(Reader reader)
Parameters
reader:Reader

The data stream containing JSON elements concatenated to each other.

Since
1.4

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Returns true if a JsonElement is available on the input for consumption

Returns:boolean

true if a JsonElement is available on the input, false otherwise

Annotations
@Override
Exceptions
JsonParseException:
if the incoming stream is malformed JSON.
Since
1.4
nextback to summary
public JsonElement next() throws JsonParseException

Implements java.util.Iterator.next.

Returns the next available JsonElement on the reader. Throws a NoSuchElementException if no element is available.

Returns:JsonElement

the next available JsonElement on the reader.

Annotations
@Override
Exceptions
JsonParseException:
if the incoming stream is malformed JSON.
NoSuchElementException:
if no JsonElement is available.
Since
1.4
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

This optional Iterator method is not relevant for stream parsing and hence is not implemented.

Annotations
@Override
Since
1.4