Package org.noggit
Class JSONParser
- java.lang.Object
-
- org.noggit.JSONParser
-
- Direct Known Subclasses:
JSONParserWS
public class JSONParser extends java.lang.Object- Version:
- $Id: JSONParser.java 1099557 2011-05-04 18:54:26Z yonik $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJSONParser.ParseException
-
Field Summary
Fields Modifier and Type Field Description static intALLOW_BACKSLASH_ESCAPING_ANY_CHARACTERstatic intALLOW_COMMENTSFlags to control parsing behaviorstatic intALLOW_EXTRA_COMMASALLOW_EXTRA_COMMAS causes any nunber of extra commas in arrays and objects to be ignored Note that a trailing comma in [] would be [,] (hence calling the feature "trailing" commas isn't really correct.static intALLOW_SINGLE_QUOTESstatic intALLOW_UNQUOTED_KEYSstatic intALLOW_UNQUOTED_STRING_VALUESstatic intARRAY_ENDEvent indicating the end of a JSON arraystatic intARRAY_STARTEvent indicating the start of a JSON arraystatic intBIGNUMBEREvent indicating a JSON number value that was not produced by toString of any Java primitive numerics such as Double or Long.private booleanboolstatic intBOOLEANEvent indicating a JSON boolean(package private) char[]bufprivate static CharArrdevNullprivate static byteDID_ARRELEMprivate static byteDID_ARRSTARTprivate static byteDID_MEMNAMEprivate static byteDID_MEMVALprivate static byteDID_OBJSTART(package private) intend(package private) booleaneofstatic intEOFEvent indicating the end of input has been reached(package private) intevent(package private) intflagsstatic intFLAGS_DEFAULTstatic intFLAGS_STRICT(package private) longgposprivate static intHAS_EXPONENTprivate static intHAS_FRACTION(package private) java.io.Readerinstatic intLONGEvent indicating a JSON number value which fits into a signed 64 bit integerprivate longlvalprivate intnstatestatic intNULLEvent indicating a JSON nullstatic intNUMBEREvent indicating a JSON number value which has a fractional part or an exponent and with string length <= 23 chars not including sign.static intOBJECT_ENDEvent indicating the end of a JSON objectstatic intOBJECT_STARTEvent indicating the start of a JSON objectprivate CharArroutprivate intptrprivate byte[]stack(package private) intstartprivate bytestatestatic intSTRINGEvent indicating a JSON string value, including member names of objects(package private) intstringTermprivate CharArrtmpprivate intvalstateprivate static longWS_MASK
-
Constructor Summary
Constructors Constructor Description JSONParser(char[] data, int start, int end)JSONParser(java.io.Reader in)JSONParser(java.io.Reader in, char[] buffer)JSONParser(java.lang.String data)JSONParser(java.lang.String data, int start, int end)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcontinueNumber(CharArr arr)protected JSONParser.ParseExceptionerr(java.lang.String msg)private java.lang.StringerrEscape(int a, int b)protected voidfill()booleangetBoolean()Reads a boolean valueprotected intgetChar()private intgetCharExpected(int expected)protected intgetCharNWS()protected intgetCharNWS(int ch)private java.lang.StringgetContext()doublegetDouble()Reads a number from the input stream and parses it as a doublestatic java.lang.StringgetEventString(int e)intgetFlags()intgetLevel()longgetLong()Reads a number from the input stream and parses it as a long, only if the value will in fact fit into a signed 64 bit integer.private voidgetMore()protected voidgetNewlineComment()voidgetNull()Reads a null valueCharArrgetNumberChars()Returns the characters of a JSON numeric value.voidgetNumberChars(CharArr output)Reads a JSON numeric value into the output.longgetPosition()protected voidgetSlashComment()java.lang.StringgetString()Returns the JSON string value, decoding any escaped characters.voidgetString(CharArr output)Reads a JSON string into the output, decoding any escaped characters.CharArrgetStringChars()Returns the characters of a JSON string value, decoding any escaped characters.private voidgoTo(int what)private voidhandleNonDoubleQuoteString(int ch, boolean isName)private inthexval(int hexdig)private static booleanisUnquotedStringChar(int ch)private static booleanisUnquotedStringStart(int ch)protected static booleanisWhitespace(int ch)Returns true if the given character is considered to be whitespace.intlastEvent()private booleanmatchBareWord(char[] arr)private intnext(int ch)alternate implementation // middle is the pointer to the middle of a buffer to start scanning for a non-string // character ('"' or "/").intnextEvent()Returns the next event encountered in the JSON stream, one ofSTRINGLONGNUMBERBIGNUMBERBOOLEANNULLOBJECT_STARTOBJECT_ENDOBJECT_ENDARRAY_STARTARRAY_ENDEOFprivate voidpop()private voidpush()private charreadEscapedChar()private intreadExp(CharArr arr, int lim)private intreadExpDigits(CharArr arr, int lim)private intreadFrac(CharArr arr, int lim)private longreadNumber(int firstChar, boolean isNeg)Returns the long read...private voidreadStringBare(CharArr arr)private CharArrreadStringChars()private voidreadStringChars2(CharArr arr, int middle)intsetFlags(int flags)java.lang.StringtoString()booleanwasKey()
-
-
-
Field Detail
-
STRING
public static final int STRING
Event indicating a JSON string value, including member names of objects- See Also:
- Constant Field Values
-
LONG
public static final int LONG
Event indicating a JSON number value which fits into a signed 64 bit integer- See Also:
- Constant Field Values
-
NUMBER
public static final int NUMBER
Event indicating a JSON number value which has a fractional part or an exponent and with string length <= 23 chars not including sign. This covers all representations of normal values for Double.toString().- See Also:
- Constant Field Values
-
BIGNUMBER
public static final int BIGNUMBER
Event indicating a JSON number value that was not produced by toString of any Java primitive numerics such as Double or Long. It is either an integer outside the range of a 64 bit signed integer, or a floating point value with a string representation of more than 23 chars.- See Also:
- Constant Field Values
-
BOOLEAN
public static final int BOOLEAN
Event indicating a JSON boolean- See Also:
- Constant Field Values
-
NULL
public static final int NULL
Event indicating a JSON null- See Also:
- Constant Field Values
-
OBJECT_START
public static final int OBJECT_START
Event indicating the start of a JSON object- See Also:
- Constant Field Values
-
OBJECT_END
public static final int OBJECT_END
Event indicating the end of a JSON object- See Also:
- Constant Field Values
-
ARRAY_START
public static final int ARRAY_START
Event indicating the start of a JSON array- See Also:
- Constant Field Values
-
ARRAY_END
public static final int ARRAY_END
Event indicating the end of a JSON array- See Also:
- Constant Field Values
-
EOF
public static final int EOF
Event indicating the end of input has been reached- See Also:
- Constant Field Values
-
ALLOW_COMMENTS
public static final int ALLOW_COMMENTS
Flags to control parsing behavior- See Also:
- Constant Field Values
-
ALLOW_SINGLE_QUOTES
public static final int ALLOW_SINGLE_QUOTES
- See Also:
- Constant Field Values
-
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
public static final int ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
- See Also:
- Constant Field Values
-
ALLOW_UNQUOTED_KEYS
public static final int ALLOW_UNQUOTED_KEYS
- See Also:
- Constant Field Values
-
ALLOW_UNQUOTED_STRING_VALUES
public static final int ALLOW_UNQUOTED_STRING_VALUES
- See Also:
- Constant Field Values
-
ALLOW_EXTRA_COMMAS
public static final int ALLOW_EXTRA_COMMAS
ALLOW_EXTRA_COMMAS causes any nunber of extra commas in arrays and objects to be ignored Note that a trailing comma in [] would be [,] (hence calling the feature "trailing" commas isn't really correct. Since trailing commas is fundamentally incompatible with any future "fill-in-missing-values-with-null", it was decided to extend this feature to handle any number of extra commas.- See Also:
- Constant Field Values
-
FLAGS_STRICT
public static final int FLAGS_STRICT
- See Also:
- Constant Field Values
-
FLAGS_DEFAULT
public static final int FLAGS_DEFAULT
- See Also:
- Constant Field Values
-
devNull
private static final CharArr devNull
-
flags
int flags
-
buf
final char[] buf
-
start
int start
-
end
int end
-
in
final java.io.Reader in
-
eof
boolean eof
-
gpos
long gpos
-
event
int event
-
stringTerm
int stringTerm
-
out
private final CharArr out
-
stack
private byte[] stack
-
ptr
private int ptr
-
state
private byte state
-
DID_OBJSTART
private static final byte DID_OBJSTART
- See Also:
- Constant Field Values
-
DID_ARRSTART
private static final byte DID_ARRSTART
- See Also:
- Constant Field Values
-
DID_ARRELEM
private static final byte DID_ARRELEM
- See Also:
- Constant Field Values
-
DID_MEMNAME
private static final byte DID_MEMNAME
- See Also:
- Constant Field Values
-
DID_MEMVAL
private static final byte DID_MEMVAL
- See Also:
- Constant Field Values
-
valstate
private int valstate
-
WS_MASK
private static final long WS_MASK
- See Also:
- Constant Field Values
-
bool
private boolean bool
-
lval
private long lval
-
nstate
private int nstate
-
HAS_FRACTION
private static final int HAS_FRACTION
- See Also:
- Constant Field Values
-
HAS_EXPONENT
private static final int HAS_EXPONENT
- See Also:
- Constant Field Values
-
tmp
private final CharArr tmp
-
-
Constructor Detail
-
JSONParser
public JSONParser(java.io.Reader in)
-
JSONParser
public JSONParser(java.io.Reader in, char[] buffer)
-
JSONParser
public JSONParser(char[] data, int start, int end)
-
JSONParser
public JSONParser(java.lang.String data)
-
JSONParser
public JSONParser(java.lang.String data, int start, int end)
-
-
Method Detail
-
getEventString
public static java.lang.String getEventString(int e)
-
getFlags
public int getFlags()
-
setFlags
public int setFlags(int flags)
-
push
private final void push()
-
pop
private final void pop()
-
fill
protected void fill() throws java.io.IOException- Throws:
java.io.IOException
-
getMore
private void getMore() throws java.io.IOException- Throws:
java.io.IOException
-
getChar
protected int getChar() throws java.io.IOException- Throws:
java.io.IOException
-
isWhitespace
protected static final boolean isWhitespace(int ch)
Returns true if the given character is considered to be whitespace. One difference between Java's Character.isWhitespace() is that this method considers a hard space (non-breaking space, or nbsp) to be whitespace.
-
getCharNWS
protected int getCharNWS() throws java.io.IOException- Throws:
java.io.IOException
-
getCharNWS
protected int getCharNWS(int ch) throws java.io.IOException- Throws:
java.io.IOException
-
getCharExpected
private int getCharExpected(int expected) throws java.io.IOException- Throws:
java.io.IOException
-
getNewlineComment
protected void getNewlineComment() throws java.io.IOException- Throws:
java.io.IOException
-
getSlashComment
protected void getSlashComment() throws java.io.IOException- Throws:
java.io.IOException
-
matchBareWord
private boolean matchBareWord(char[] arr) throws java.io.IOException- Throws:
java.io.IOException
-
err
protected JSONParser.ParseException err(java.lang.String msg)
-
getContext
private java.lang.String getContext()
-
errEscape
private java.lang.String errEscape(int a, int b)
-
readNumber
private long readNumber(int firstChar, boolean isNeg) throws java.io.IOExceptionReturns the long read... only significant if valstate==LONG after this call. firstChar should be the first numeric digit read.- Throws:
java.io.IOException
-
readFrac
private int readFrac(CharArr arr, int lim) throws java.io.IOException
- Throws:
java.io.IOException
-
readExp
private int readExp(CharArr arr, int lim) throws java.io.IOException
- Throws:
java.io.IOException
-
readExpDigits
private int readExpDigits(CharArr arr, int lim) throws java.io.IOException
- Throws:
java.io.IOException
-
continueNumber
private void continueNumber(CharArr arr) throws java.io.IOException
- Throws:
java.io.IOException
-
hexval
private int hexval(int hexdig)
-
readEscapedChar
private char readEscapedChar() throws java.io.IOException- Throws:
java.io.IOException
-
readStringChars
private CharArr readStringChars() throws java.io.IOException
- Throws:
java.io.IOException
-
readStringChars2
private void readStringChars2(CharArr arr, int middle) throws java.io.IOException
- Throws:
java.io.IOException
-
readStringBare
private void readStringBare(CharArr arr) throws java.io.IOException
- Throws:
java.io.IOException
-
handleNonDoubleQuoteString
private void handleNonDoubleQuoteString(int ch, boolean isName) throws java.io.IOException- Throws:
java.io.IOException
-
isUnquotedStringStart
private static boolean isUnquotedStringStart(int ch)
-
isUnquotedStringChar
private static boolean isUnquotedStringChar(int ch)
-
next
private int next(int ch) throws java.io.IOExceptionalternate implementation // middle is the pointer to the middle of a buffer to start scanning for a non-string // character ('"' or "/"). start<=middle=end) { getMore(); middle=start; } else { start = middle+1; // set buffer pointer to correct spot if (ch=='"') { valstate=0; return; } else if (ch=='\\') { arr.write(readEscapedChar()); if (start>=end) getMore(); middle=start; } } } } - Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
nextEvent
public int nextEvent() throws java.io.IOExceptionReturns the next event encountered in the JSON stream, one of- Throws:
java.io.IOException
-
lastEvent
public int lastEvent()
-
wasKey
public boolean wasKey()
-
goTo
private void goTo(int what) throws java.io.IOException- Throws:
java.io.IOException
-
getString
public java.lang.String getString() throws java.io.IOExceptionReturns the JSON string value, decoding any escaped characters.- Throws:
java.io.IOException
-
getStringChars
public CharArr getStringChars() throws java.io.IOException
Returns the characters of a JSON string value, decoding any escaped characters. The underlying buffer of the returnedCharArrshould *not* be modified as it may be shared with the input buffer. The returnedCharArrwill only be valid up until the next JSONParser method is called. Any required data should be read before that point.- Throws:
java.io.IOException
-
getString
public void getString(CharArr output) throws java.io.IOException
Reads a JSON string into the output, decoding any escaped characters.- Throws:
java.io.IOException
-
getLong
public long getLong() throws java.io.IOExceptionReads a number from the input stream and parses it as a long, only if the value will in fact fit into a signed 64 bit integer.- Throws:
java.io.IOException
-
getDouble
public double getDouble() throws java.io.IOExceptionReads a number from the input stream and parses it as a double- Throws:
java.io.IOException
-
getNumberChars
public CharArr getNumberChars() throws java.io.IOException
Returns the characters of a JSON numeric value. The underlying buffer of the returnedCharArrshould *not* be modified as it may be shared with the input buffer. The returnedCharArrwill only be valid up until the next JSONParser method is called. Any required data should be read before that point.- Throws:
java.io.IOException
-
getNumberChars
public void getNumberChars(CharArr output) throws java.io.IOException
Reads a JSON numeric value into the output.- Throws:
java.io.IOException
-
getBoolean
public boolean getBoolean() throws java.io.IOExceptionReads a boolean value- Throws:
java.io.IOException
-
getNull
public void getNull() throws java.io.IOExceptionReads a null value- Throws:
java.io.IOException
-
getLevel
public int getLevel()
- Returns:
- the current nesting level, the number of parent objects or arrays.
-
getPosition
public long getPosition()
-
-