Package com.google.gson
Class JsonStreamParser
java.lang.Object
com.google.gson.JsonStreamParser
- All Implemented Interfaces:
Iterator<JsonElement>
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.setLenient(boolean).
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();
}
}
- Since:
- 1.4
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanhasNext()Returns true if aJsonElementis available on the input for consumptionnext()Returns the next availableJsonElementon the reader.voidremove()This optionalIteratormethod is not relevant for stream parsing and hence is not implemented.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
JsonStreamParser
- Parameters:
json- The string containing JSON elements concatenated to each other.- Since:
- 1.4
-
JsonStreamParser
- Parameters:
reader- The data stream containing JSON elements concatenated to each other.- Since:
- 1.4
-
-
Method Details
-
next
Returns the next availableJsonElementon the reader. Throws aNoSuchElementExceptionif no element is available.- Specified by:
nextin interfaceIterator<JsonElement>- Returns:
- the next available
JsonElementon the reader. - Throws:
JsonSyntaxException- if the incoming stream is malformed JSON.NoSuchElementException- if noJsonElementis available.JsonParseException- Since:
- 1.4
-
hasNext
public boolean hasNext()Returns true if aJsonElementis available on the input for consumption- Specified by:
hasNextin interfaceIterator<JsonElement>- Returns:
- true if a
JsonElementis available on the input, false otherwise - Throws:
JsonSyntaxException- if the incoming stream is malformed JSON.- Since:
- 1.4
-
remove
public void remove()This optionalIteratormethod is not relevant for stream parsing and hence is not implemented.- Specified by:
removein interfaceIterator<JsonElement>- Since:
- 1.4
-