com.google.gson
Class JsonStreamParser

java.lang.Object
  extended by com.google.gson.JsonStreamParser
All Implemented Interfaces:
java.util.Iterator<JsonElement>

public final class JsonStreamParser
extends java.lang.Object
implements java.util.Iterator<JsonElement>

A streaming parser that allows reading of multiple JsonElements from the specified reader asynchronously.

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
Author:
Inderjeet Singh, Joel Leitch

Constructor Summary
JsonStreamParser(java.io.Reader reader)
           
JsonStreamParser(java.lang.String json)
           
 
Method Summary
 boolean hasNext()
          Returns true if a JsonElement is available on the input for consumption
 JsonElement next()
          Returns the next available JsonElement on the reader.
 void remove()
          This optional Iterator method 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, wait
 

Constructor Detail

JsonStreamParser

public JsonStreamParser(java.lang.String json)
Parameters:
json - The string containing JSON elements concatenated to each other.
Since:
1.4

JsonStreamParser

public JsonStreamParser(java.io.Reader reader)
Parameters:
reader - The data stream containing JSON elements concatenated to each other.
Since:
1.4
Method Detail

next

public JsonElement next()
                 throws JsonParseException
Returns the next available JsonElement on the reader. Null if none available.

Specified by:
next in interface java.util.Iterator<JsonElement>
Returns:
the next available JsonElement on the reader. Null if none available.
Throws:
JsonParseException - if the incoming stream is malformed JSON.
Since:
1.4

hasNext

public boolean hasNext()
Returns true if a JsonElement is available on the input for consumption

Specified by:
hasNext in interface java.util.Iterator<JsonElement>
Returns:
true if a JsonElement is available on the input, false otherwise
Since:
1.4

remove

public void remove()
This optional Iterator method is not relevant for stream parsing and hence is not implemented.

Specified by:
remove in interface java.util.Iterator<JsonElement>
Since:
1.4


Copyright © 2008-2011 Google, Inc.. All Rights Reserved.