de.hpi.fgis.dude.util.data.json
Class DuDeJsonParser<T extends Jsonable>

java.lang.Object
  extended by de.hpi.fgis.dude.util.AbstractIterator<T>
      extended by de.hpi.fgis.dude.util.data.json.DuDeJsonParser<T>
Type Parameters:
T - The type that is used for instantiating during the iteration process.
All Implemented Interfaces:
Closeable, Iterator<T>

public class DuDeJsonParser<T extends Jsonable>
extends AbstractIterator<T>
implements Closeable

DuDeJsonParser can be used for converting a String containing Json syntax into its Java representation. Besides providing methods for reading the data it also implements the Iterator interface. The iterating functionality can be used for parsing the content of a JsonArray. DuDeJsonParser can be used for converting a Json String into its Java representation. Besides providing methods for reading the data it also implements the Iterator interface. The iterating functionality can be used for parsing the content of a JsonArray.

Author:
Matthias Pohl

Field Summary
protected  Class<T> internalType
          is needed for creating new instances (@see Jsonable#fromJson(DuDeJsonParser))
 
Constructor Summary
protected DuDeJsonParser(Class<T> type)
          Internal constructor for sub classing.
  DuDeJsonParser(Class<T> type, InputStream stream)
          Initializes a new DuDeJsonParser using the passed InputStream.
  DuDeJsonParser(Class<T> type, Reader reader)
          Initializes a new DuDeJsonParser using the passed Reader.
  DuDeJsonParser(Class<T> type, String str)
          Initializes a new DuDeJsonParser that parses the passed String.
  DuDeJsonParser(InputStream stream)
          Initializes a new DuDeJsonParser using the passed InputStream.
  DuDeJsonParser(Reader reader)
          Initializes a new DuDeJsonParser using the passed Reader.
  DuDeJsonParser(String str)
          Initializes a new DuDeJsonParser that parses the passed String.
 
Method Summary
 void close()
          Closes the used reader.
 void consolidatePushBack()
          Cleans up helper structures used with createPushBackGenerator().
This method should especially be invoked when all pushed json elements have been read.
Without calling this method, a slight performance decrease should be observable.
protected  org.codehaus.jackson.JsonParseException createJsonParseException(String msg)
          Creates a new JsonParseException.
protected  org.codehaus.jackson.JsonParseException createJsonParseException(String msg, Throwable cause)
          Creates a new JsonParseException.
 DuDeJsonGenerator createPushBackGenerator()
          Creates a push back generator for the parser similar to PushbackInputStream.
The returned DuDeJsonGenerator might be used to generate arbitrary json elements in front of the current parser position in a FIFO manner.
In order to adjust the internal state to the newly generated json elements, a closing the returned generator with DuDeJsonGenerator.close() is necessary.
 String currentFieldName()
          Returns the current field name without setting the cursor to the next token.
 org.codehaus.jackson.JsonToken currentToken()
          Returns the current token that was loaded during the last nextToken() call.
protected  String currentTokenName()
          Returns the type name of the current token.
 boolean endOfContentReached()
          Checks whether the end of the content was reached.
 boolean endTokenReached()
          Checks whether the current token declares the end of a complex Json structure.
protected  void finalize()
          Closes all streams if it is not already done.
static JsonValue getScalarValue(String jsonCode)
          Converts the passed String into the corresponding atomic JsonValue.
protected  boolean hasCurrentToken()
          Checks whether a token is loaded.
protected  T loadNextElement()
          Returns the element of the next iteration step.
 boolean nextBoolean()
          Returns the current boolean value.
 String nextFieldName()
          Returns the current field name.
<ReturnType extends Jsonable>
ReturnType
nextJsonable(Class<ReturnType> returnType)
          Returns the next Json instance.
 JsonArray nextJsonArray()
          Returns the next JsonArray.
 JsonBoolean nextJsonBoolean()
          Returns the next JsonBoolean.
 JsonNull nextJsonNull()
          Returns the next JsonNull.
 JsonNumber nextJsonNumber()
          Returns the next JsonNumber.
 JsonRecord nextJsonRecord()
          Returns the next JsonRecord.
 JsonString nextJsonString()
          Returns the next JsonString.
 JsonValue nextJsonValue()
          Returns any JsonValue.
<C extends AutoJsonable>
C
nextObject(Class<C> clazz)
          Returns the next instance of the given type initialized with the current json.
 String nextString()
          Returns the current textual value.
protected  void nextToken()
          Loads the next token from the stream.
 void skipFieldName(String fieldName)
          Safely skips the current token if it is a field name with the specified value.
 void skipToken()
          Skips the current token.
 void skipToken(org.codehaus.jackson.JsonToken expectedType)
          Safely skips the current token if it is of the given type.
 
Methods inherited from class de.hpi.fgis.dude.util.AbstractIterator
hasNext, next, remove
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

internalType

protected Class<T extends Jsonable> internalType
is needed for creating new instances (@see Jsonable#fromJson(DuDeJsonParser))

Constructor Detail

DuDeJsonParser

protected DuDeJsonParser(Class<T> type)
Internal constructor for sub classing.

Parameters:
type - The type that is generated during the iteration process. JsonValue instances are returned, if null is passed.

DuDeJsonParser

public DuDeJsonParser(Class<T> type,
                      InputStream stream)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser using the passed InputStream.

Parameters:
type - The type that is generated during the iteration process. JsonValue instances are returned, if null is passed.
stream - The stream whose data will be parsed.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

DuDeJsonParser

public DuDeJsonParser(Class<T> type,
                      Reader reader)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser using the passed Reader.

Parameters:
type - The type that is generated during the iteration process. JsonValue instances are returned, if null is passed.
reader - The reader that is used for parsing the data.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

DuDeJsonParser

public DuDeJsonParser(Class<T> type,
                      String str)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser that parses the passed String.

Parameters:
type - The type that is generated during the iteration process. JsonValue instances are returned, if null is passed.
str - The code that shall be parsed.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

DuDeJsonParser

public DuDeJsonParser(InputStream stream)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser using the passed InputStream.

Parameters:
stream - The stream whose data will be parsed.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

DuDeJsonParser

public DuDeJsonParser(Reader reader)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser using the passed Reader.

Parameters:
reader - The reader that is used for parsing the data.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

DuDeJsonParser

public DuDeJsonParser(String str)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Initializes a new DuDeJsonParser that parses the passed String.

Parameters:
str - The code that shall be parsed.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.
Method Detail

getScalarValue

public static JsonValue getScalarValue(String jsonCode)
Converts the passed String into the corresponding atomic JsonValue.

Parameters:
jsonCode - The String that will be converted.
Returns:
The corresponding JsonValue.

close

public void close()
           throws IOException
Closes the used reader.

Specified by:
close in interface Closeable
Throws:
IOException
See Also:
Closeable.close()

consolidatePushBack

public void consolidatePushBack()
Cleans up helper structures used with createPushBackGenerator().
This method should especially be invoked when all pushed json elements have been read.
Without calling this method, a slight performance decrease should be observable. However, the parser still works correctly.


createJsonParseException

protected org.codehaus.jackson.JsonParseException createJsonParseException(String msg)
Creates a new JsonParseException.

Parameters:
msg - The error message.
Returns:
A newly created JsonParseException instance.

createJsonParseException

protected org.codehaus.jackson.JsonParseException createJsonParseException(String msg,
                                                                           Throwable cause)
Creates a new JsonParseException.

Parameters:
msg - The error message.
cause - The cause of this exception.
Returns:
A newly created JsonParseException instance.

createPushBackGenerator

public DuDeJsonGenerator createPushBackGenerator()
Creates a push back generator for the parser similar to PushbackInputStream.
The returned DuDeJsonGenerator might be used to generate arbitrary json elements in front of the current parser position in a FIFO manner.
In order to adjust the internal state to the newly generated json elements, a closing the returned generator with DuDeJsonGenerator.close() is necessary.

Returns:
the push back generator

currentFieldName

public String currentFieldName()
                        throws org.codehaus.jackson.JsonParseException,
                               IOException
Returns the current field name without setting the cursor to the next token.

Returns:
The current field name.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading the Json.

currentToken

public org.codehaus.jackson.JsonToken currentToken()
                                            throws EOFException
Returns the current token that was loaded during the last nextToken() call.

Returns:
The current token.
Throws:
EOFException - If the end of the content was already reached.

currentTokenName

protected String currentTokenName()
Returns the type name of the current token.

Returns:
The type name of the current token.

endOfContentReached

public boolean endOfContentReached()
Checks whether the end of the content was reached.

Returns:
true, if #readNextCharacter() will return null; otherwise false.

endTokenReached

public boolean endTokenReached()
                        throws EOFException
Checks whether the current token declares the end of a complex Json structure.

Returns:
true, if the end of an array or a record is reached; otherwise false.
Throws:
EOFException - If the stream's end was reached.

finalize

protected void finalize()
                 throws Throwable
Closes all streams if it is not already done.

Overrides:
finalize in class Object
Throws:
Throwable

hasCurrentToken

protected boolean hasCurrentToken()
Checks whether a token is loaded.

Returns:
true, if currentToken() would return the current token; false, if the end of the content was already reached.

loadNextElement

protected T loadNextElement()
Description copied from class: AbstractIterator
Returns the element of the next iteration step. This method needs to be implemented by each sub-class.

Specified by:
loadNextElement in class AbstractIterator<T extends Jsonable>
Returns:
The next element.

nextBoolean

public boolean nextBoolean()
                    throws org.codehaus.jackson.JsonParseException,
                           IOException
Returns the current boolean value.

Returns:
The current boolean value.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading the Json.

nextFieldName

public String nextFieldName()
                     throws org.codehaus.jackson.JsonParseException,
                            IOException
Returns the current field name.

Returns:
The current field name.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading the Json.

nextJsonable

public <ReturnType extends Jsonable> ReturnType nextJsonable(Class<ReturnType> returnType)
                                         throws org.codehaus.jackson.JsonParseException,
                                                IOException
Returns the next Json instance. This instance is casted into the passed type.

Type Parameters:
ReturnType - The type that shall be returned.
Parameters:
returnType - The type that shall be returned.
Returns:
The parsed instance.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading the data.
InstantiationException - If an error occurred while instantiating the new object.
IllegalAccessException - If an access error occurred while instantiating the new object.

nextJsonArray

public JsonArray nextJsonArray()
                        throws org.codehaus.jackson.JsonParseException,
                               IOException
Returns the next JsonArray. The syntax of the JsonArray has to start directly. Any leading whitespaces or similar characters will cause a ParseException.

Returns:
The next JsonArray.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonArray is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonBoolean

public JsonBoolean nextJsonBoolean()
                            throws org.codehaus.jackson.JsonParseException,
                                   IOException
Returns the next JsonBoolean. The syntax of the JsonBoolean has to start directly. Any leading whitespaces or similar characters will cause a ParseException.

Returns:
The next JsonBoolean.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonBoolean is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonNull

public JsonNull nextJsonNull()
                      throws org.codehaus.jackson.JsonParseException,
                             IOException
Returns the next JsonNull. The syntax of the JsonNull has to start directly. Any leading whitespaces or similar characters will cause a ParseException.

Returns:
The next JsonNull.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonNull is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonNumber

public JsonNumber nextJsonNumber()
                          throws org.codehaus.jackson.JsonParseException,
                                 IOException
Returns the next JsonNumber. The syntax of the JsonNumber has to start directly. Any leading whitespaces or similar characters will cause a ParseException. The passed number will be converted into one of the following types: Integer, Long, or BigDecimal; Float or Double. The "smallest" data type will be preferred (e.g.: 10000 is converted into a JsonNumber type since it fits into the Integer range.).

Returns:
The next JsonNumber.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonNumber is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonRecord

public JsonRecord nextJsonRecord()
                          throws org.codehaus.jackson.JsonParseException,
                                 IOException
Returns the next JsonRecord. The syntax of the JsonRecord has to start directly. Any leading whitespaces or similar characters will cause a ParseException.

Returns:
The next JsonRecord.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonRecord is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonString

public JsonString nextJsonString()
                          throws org.codehaus.jackson.JsonParseException,
                                 IOException
Returns the next JsonString. The syntax of the JsonString has to start directly. Any leading whitespaces or similar characters will cause a JsonParseException.

Returns:
The next JsonString.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonString is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextJsonValue

public JsonValue nextJsonValue()
                        throws org.codehaus.jackson.JsonParseException,
                               IOException
Returns any JsonValue. The syntax of the JsonValue has to start directly. Any leading whitespaces or similar characters will cause a ParseException.

Returns:
The next JsonValue.
Throws:
org.codehaus.jackson.JsonParseException - If the read JsonValue is invalid in terms of the Json syntax or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextObject

public <C extends AutoJsonable> C nextObject(Class<C> clazz)
                                  throws org.codehaus.jackson.JsonParseException,
                                         IOException
Returns the next instance of the given type initialized with the current json.

Type Parameters:
C - the type whose instance is being created
Parameters:
clazz - the type whose instance is being created
Returns:
an instance of clazz or null if the current json value is null.
Throws:
org.codehaus.jackson.JsonParseException - If the read json is invalid or some leading whitespaces are read.
IOException - If an error occurs during the reading process.

nextString

public String nextString()
                  throws org.codehaus.jackson.JsonParseException,
                         IOException
Returns the current textual value.

Returns:
The current textual value.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading the Json.

nextToken

protected void nextToken()
                  throws org.codehaus.jackson.JsonParseException,
                         IOException
Loads the next token from the stream.

Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

skipFieldName

public void skipFieldName(String fieldName)
                   throws org.codehaus.jackson.JsonParseException,
                          IOException
Safely skips the current token if it is a field name with the specified value.

Parameters:
fieldName - The expected field name
Throws:
IOException - If an error occurs while reading the Json.
org.codehaus.jackson.JsonParseException - If the current token is not the expected field name or an error occurs while parsing the Json.

skipToken

public void skipToken()
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Skips the current token.

Throws:
IOException - If an error occurs while reading the Json.
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.

skipToken

public void skipToken(org.codehaus.jackson.JsonToken expectedType)
               throws org.codehaus.jackson.JsonParseException,
                      IOException
Safely skips the current token if it is of the given type.

Parameters:
expectedType - The expected JsonToken type
Throws:
IOException - If an error occurs while reading the Json.
org.codehaus.jackson.JsonParseException - If the current token is not of the expected type or an error occurs while parsing the Json.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.