Package org.noggit
Class JSONParser
- java.lang.Object
-
- org.noggit.JSONParser
-
public class JSONParser extends Object
-
-
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 number of extra commas in arrays and objects to be ignored Note that a trailing comma in [] would be [,] (hence calling the feature "trailing" commas is either limiting or misleading.static intALLOW_MISSING_COLON_COMMA_BEFORE_OBJECTstatic 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.static intBOOLEANEvent indicating a JSON booleanprotected char[]bufprotected intendprotected booleaneofstatic intEOFEvent indicating the end of input has been reachedprotected inteventprotected intflagsstatic intFLAGS_DEFAULTstatic intFLAGS_STRICTprotected longgposprotected Readerinstatic intLONGEvent indicating a JSON number value which fits into a signed 64 bit integerprotected booleanmissingOpeningBracestatic 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 objectstatic intOPTIONAL_OUTER_BRACESprotected intstartstatic intSTRINGEvent indicating a JSON string value, including member names of objectsprotected intstringTerm
-
Constructor Summary
Constructors Constructor Description JSONParser(char[] data, int start, int end)JSONParser(Reader in)JSONParser(Reader in, char[] buffer)JSONParser(String data)JSONParser(String data, int start, int end)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected JSONParser.ParseExceptionerr(String msg)protected voidfill()booleangetBoolean()Reads a boolean valueprotected intgetChar()protected intgetCharExpected(int expected)protected intgetCharNWS()protected intgetCharNWS(int ch)doublegetDouble()Reads a number from the input stream and parses it as a doublestatic 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.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()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.protected voidhandleNonDoubleQuoteString(int ch, boolean isName)protected static booleanisWhitespace(int ch)Returns true if the given character is considered to be whitespace.intlastEvent()protected booleanmatchBareWord(char[] arr)intnextEvent()Returns the next event encountered in the JSON stream, one ofSTRINGLONGNUMBERBIGNUMBERBOOLEANNULLOBJECT_STARTOBJECT_ENDOBJECT_ENDARRAY_STARTARRAY_ENDEOFintsetFlags(int flags)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 number of extra commas in arrays and objects to be ignored Note that a trailing comma in [] would be [,] (hence calling the feature "trailing" commas is either limiting or misleading. 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
-
ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT
public static final int ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT
- See Also:
- Constant Field Values
-
OPTIONAL_OUTER_BRACES
public static final int OPTIONAL_OUTER_BRACES
- 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
-
flags
protected int flags
-
buf
protected final char[] buf
-
start
protected int start
-
end
protected int end
-
in
protected final Reader in
-
eof
protected boolean eof
-
gpos
protected long gpos
-
event
protected int event
-
stringTerm
protected int stringTerm
-
missingOpeningBrace
protected boolean missingOpeningBrace
-
-
Method Detail
-
getEventString
public static String getEventString(int e)
-
getFlags
public int getFlags()
-
setFlags
public int setFlags(int flags)
-
fill
protected void fill() throws IOException- Throws:
IOException
-
getChar
protected int getChar() throws IOException- Throws:
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 IOException- Throws:
IOException
-
getCharNWS
protected int getCharNWS(int ch) throws IOException- Throws:
IOException
-
getCharExpected
protected int getCharExpected(int expected) throws IOException- Throws:
IOException
-
getNewlineComment
protected void getNewlineComment() throws IOException- Throws:
IOException
-
getSlashComment
protected void getSlashComment() throws IOException- Throws:
IOException
-
matchBareWord
protected boolean matchBareWord(char[] arr) throws IOException- Throws:
IOException
-
err
protected JSONParser.ParseException err(String msg)
-
handleNonDoubleQuoteString
protected void handleNonDoubleQuoteString(int ch, boolean isName) throws IOException- Throws:
IOException
-
nextEvent
public int nextEvent() throws IOExceptionReturns the next event encountered in the JSON stream, one of- Throws:
IOException
-
lastEvent
public int lastEvent()
-
wasKey
public boolean wasKey()
-
getString
public String getString() throws IOException
Returns the JSON string value, decoding any escaped characters.- Throws:
IOException
-
getStringChars
public CharArr getStringChars() throws 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:
IOException
-
getString
public void getString(CharArr output) throws IOException
Reads a JSON string into the output, decoding any escaped characters.- Throws:
IOException
-
getLong
public long getLong() throws 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:
IOException
-
getDouble
public double getDouble() throws IOExceptionReads a number from the input stream and parses it as a double- Throws:
IOException
-
getNumberChars
public CharArr getNumberChars() throws IOException
Returns the characters of a JSON numeric value.The underlying buffer of the returned
CharArrshould *not* be modified as it may be shared with the input buffer.The returned
CharArrwill only be valid up until the next JSONParser method is called. Any required data should be read before that point.- Throws:
IOException
-
getNumberChars
public void getNumberChars(CharArr output) throws IOException
Reads a JSON numeric value into the output.- Throws:
IOException
-
getBoolean
public boolean getBoolean() throws IOExceptionReads a boolean value- Throws:
IOException
-
getNull
public void getNull() throws IOExceptionReads a null value- Throws:
IOException
-
getLevel
public int getLevel()
- Returns:
- the current nesting level, the number of parent objects or arrays.
-
getPosition
public long getPosition()
-
-