org.pfsw.text.json
Class SimpleJsonParser

java.lang.Object
  extended by org.pfsw.text.json.SimpleJsonParser

public class SimpleJsonParser
extends java.lang.Object

A parser that creates a Java internal structure of JsonObject and JsonArray objects from a given JSON text.

The data will be mapped on the following Java types:

Object
JsonObject
Array
JsonArray
null
null
true
Boolean.TRUE
false
Boolean.FALSE
"string"
String
integer number
Integer
long number
Long
other numbers
BigDecimal

It is based on the RFC-7159 (http://tools.ietf.org/html/rfc7159).


Constructor Summary
SimpleJsonParser()
          Creates a new parser.
 
Method Summary
protected  void checkNextMandatoryToken(char expectedChar)
           
static SimpleJsonParser create()
          Creates a new parser.
protected  JsonArray createArray()
           
protected  JsonObject createObject()
           
 org.pfsw.text.json.CharEscaper getCharEscaper()
           
protected  java.lang.String getCurrentStringRegion(int backwards, int forwards)
          Just for debugging purposes!
protected  int getLastPosition()
           
protected  char getStringDelimiter()
           
protected  StringExaminer getStringExaminer()
           
protected  boolean isArrayEnd(char ch)
           
protected  boolean isArrayStart(char ch)
           
protected  boolean isEscape(char ch)
           
protected  boolean isNumberStart(char ch)
           
protected  boolean isObjectEnd(char ch)
           
protected  boolean isObjectStart(char ch)
           
protected  boolean isStringEnd(char ch)
           
protected  boolean isStringStart(char ch)
           
protected  boolean isValueSeparator(char ch)
           
protected  char nextChar()
          Returns simply the next character of the underlying JSON string.
protected  char nextNoneWhitespaceChar()
          Looks for the next character that is no white space and returns it.
 java.lang.Object parse(java.lang.String jsonText)
          Parse the given JSON text and return its Java representation.
protected  JsonArray parseArray()
           
 JsonArray parseArray(java.lang.String jsonText)
          Parse the given JSON text and return its Java representation as JsonArray.
protected  java.lang.Object parseInternal()
           
protected  void parseKeyValueInto(java.util.Map<java.lang.String,java.lang.Object> jsonObject)
           
protected  java.lang.Object parseLiteral()
           
protected  java.lang.String parseLiteralString(int maxLength, java.lang.String allowedChars)
           
protected  java.lang.Number parseNumber()
          Returns either an Integer, a Long or a BigDecimal.
protected  JsonObject parseObject()
           
 JsonObject parseObject(java.lang.String jsonText)
          Parse the given JSON text and return its Java representation as JsonObject.
protected  java.lang.String parseString()
           
protected
<T extends JsonType>
T
parseTo(java.lang.String json, java.lang.Class<T> resultType)
           
protected  java.lang.String parseUpToQuote()
           
protected  java.lang.Object parseValue()
           
protected  char peekNextChar()
          Returns the next character of the underlying JSON string without moving the examiner's position forward.
protected  char peekNextNoneWhitespaceChar()
          Looks for the next character that is no white space and returns it.
protected  void setStringExaminer(StringExaminer stringExaminer)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleJsonParser

public SimpleJsonParser()
Creates a new parser.

Method Detail

create

public static SimpleJsonParser create()
Creates a new parser.


parse

public java.lang.Object parse(java.lang.String jsonText)
                       throws JsonParseException
Parse the given JSON text and return its Java representation. That is one of the following types:

Parameters:
jsonText - The JSON text to be parsed (must not be null).
Returns:
The parsed data as Java objects.
Throws:
JsonParseException - Any parsing problem.

parseObject

public JsonObject parseObject(java.lang.String jsonText)
                       throws JsonParseException
Parse the given JSON text and return its Java representation as JsonObject. That implies that the given JSON text starts with a "{".

Parameters:
jsonText - The JSON text to be parsed (must not be null).
Returns:
The parsed data as Java objects.
Throws:
JsonParseException - Any parsing problem.
java.lang.ClassCastException - If the JSON text did not represent a JSON object.

parseArray

public JsonArray parseArray(java.lang.String jsonText)
                     throws JsonParseException
Parse the given JSON text and return its Java representation as JsonArray. That implies that the given JSON text starts with a "[".

Parameters:
jsonText - The JSON text to be parsed (must not be null).
Returns:
The parsed data as Java objects.
Throws:
JsonParseException - Any parsing problem.
java.lang.ClassCastException - If the JSON text did not represent a JSON array.

isObjectStart

protected boolean isObjectStart(char ch)

isObjectEnd

protected boolean isObjectEnd(char ch)

isArrayStart

protected boolean isArrayStart(char ch)

isArrayEnd

protected boolean isArrayEnd(char ch)

isStringStart

protected boolean isStringStart(char ch)

isStringEnd

protected boolean isStringEnd(char ch)

isEscape

protected boolean isEscape(char ch)

isValueSeparator

protected boolean isValueSeparator(char ch)

isNumberStart

protected boolean isNumberStart(char ch)

parseInternal

protected java.lang.Object parseInternal()
                                  throws JsonParseException
Throws:
JsonParseException

parseValue

protected java.lang.Object parseValue()
                               throws JsonParseException
Throws:
JsonParseException

parseObject

protected JsonObject parseObject()
                          throws JsonParseException
Throws:
JsonParseException

parseKeyValueInto

protected void parseKeyValueInto(java.util.Map<java.lang.String,java.lang.Object> jsonObject)
                          throws JsonParseException
Throws:
JsonParseException

parseArray

protected JsonArray parseArray()
                        throws JsonParseException
Throws:
JsonParseException

parseString

protected java.lang.String parseString()
                                throws JsonParseException
Throws:
JsonParseException

parseUpToQuote

protected java.lang.String parseUpToQuote()
                                   throws JsonParseException
Throws:
JsonParseException

parseNumber

protected java.lang.Number parseNumber()
                                throws JsonParseException
Returns either an Integer, a Long or a BigDecimal.

Throws:
JsonParseException - If the string cannot be parsed to a number.

parseLiteralString

protected java.lang.String parseLiteralString(int maxLength,
                                              java.lang.String allowedChars)
                                       throws JsonParseException
Throws:
JsonParseException

parseLiteral

protected java.lang.Object parseLiteral()
                                 throws JsonParseException
Throws:
JsonParseException

parseTo

protected <T extends JsonType> T parseTo(java.lang.String json,
                                         java.lang.Class<T> resultType)
                              throws JsonParseException
Throws:
JsonParseException

getLastPosition

protected int getLastPosition()

checkNextMandatoryToken

protected void checkNextMandatoryToken(char expectedChar)
                                throws JsonParseException
Throws:
JsonParseException

nextNoneWhitespaceChar

protected char nextNoneWhitespaceChar()
                               throws JsonParseException
Looks for the next character that is no white space and returns it. Position when finished: on next character after the one returned.

Throws:
JsonParseException - If the end of the underlying JSON data was reached.

nextChar

protected char nextChar()
                 throws JsonParseException
Returns simply the next character of the underlying JSON string. Position when finished: on next character after the one returned.

Throws:
JsonParseException - If the end of the underlying JSON data was reached.

peekNextChar

protected char peekNextChar()
Returns the next character of the underlying JSON string without moving the examiner's position forward. That is, the next nextChar() call will return the same character again. Position when finished: still on same character.


peekNextNoneWhitespaceChar

protected char peekNextNoneWhitespaceChar()
                                   throws JsonParseException
Looks for the next character that is no white space and returns it. Position when finished: on the character returned.

Throws:
JsonParseException - If the end of the underlying JSON data was reached.

getStringDelimiter

protected char getStringDelimiter()

createObject

protected JsonObject createObject()

createArray

protected JsonArray createArray()

getStringExaminer

protected StringExaminer getStringExaminer()

setStringExaminer

protected void setStringExaminer(StringExaminer stringExaminer)

getCharEscaper

public org.pfsw.text.json.CharEscaper getCharEscaper()

getCurrentStringRegion

protected java.lang.String getCurrentStringRegion(int backwards,
                                                  int forwards)
Just for debugging purposes!