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 class
JSONParser.ParseException
-
Field Summary
Fields Modifier and Type Field Description static int
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
static int
ALLOW_COMMENTS
Flags to control parsing behaviorstatic 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.static int
ALLOW_SINGLE_QUOTES
static int
ALLOW_UNQUOTED_KEYS
static int
ALLOW_UNQUOTED_STRING_VALUES
static int
ARRAY_END
Event indicating the end of a JSON arraystatic int
ARRAY_START
Event indicating the start of a JSON arraystatic int
BIGNUMBER
Event indicating a JSON number value that was not produced by toString of any Java primitive numerics such as Double or Long.private boolean
bool
static int
BOOLEAN
Event indicating a JSON boolean(package private) char[]
buf
private static CharArr
devNull
private static byte
DID_ARRELEM
private static byte
DID_ARRSTART
private static byte
DID_MEMNAME
private static byte
DID_MEMVAL
private static byte
DID_OBJSTART
(package private) int
end
(package private) boolean
eof
static int
EOF
Event indicating the end of input has been reached(package private) int
event
(package private) int
flags
static int
FLAGS_DEFAULT
static int
FLAGS_STRICT
(package private) long
gpos
private static int
HAS_EXPONENT
private static int
HAS_FRACTION
(package private) java.io.Reader
in
static int
LONG
Event indicating a JSON number value which fits into a signed 64 bit integerprivate long
lval
private int
nstate
static int
NULL
Event indicating a JSON nullstatic 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.static int
OBJECT_END
Event indicating the end of a JSON objectstatic int
OBJECT_START
Event indicating the start of a JSON objectprivate CharArr
out
private int
ptr
private byte[]
stack
(package private) int
start
private byte
state
static int
STRING
Event indicating a JSON string value, including member names of objects(package private) int
stringTerm
private CharArr
tmp
private int
valstate
private static long
WS_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 void
continueNumber(CharArr arr)
protected JSONParser.ParseException
err(java.lang.String msg)
private java.lang.String
errEscape(int a, int b)
protected void
fill()
boolean
getBoolean()
Reads a boolean valueprotected int
getChar()
private int
getCharExpected(int expected)
protected int
getCharNWS()
protected int
getCharNWS(int ch)
private java.lang.String
getContext()
double
getDouble()
Reads a number from the input stream and parses it as a doublestatic java.lang.String
getEventString(int e)
int
getFlags()
int
getLevel()
long
getLong()
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 void
getMore()
protected void
getNewlineComment()
void
getNull()
Reads a null valueCharArr
getNumberChars()
Returns the characters of a JSON numeric value.void
getNumberChars(CharArr output)
Reads a JSON numeric value into the output.long
getPosition()
protected void
getSlashComment()
java.lang.String
getString()
Returns the JSON string value, decoding any escaped characters.void
getString(CharArr output)
Reads a JSON string into the output, decoding any escaped characters.CharArr
getStringChars()
Returns the characters of a JSON string value, decoding any escaped characters.private void
goTo(int what)
private void
handleNonDoubleQuoteString(int ch, boolean isName)
private int
hexval(int hexdig)
private static boolean
isUnquotedStringChar(int ch)
private static boolean
isUnquotedStringStart(int ch)
protected static boolean
isWhitespace(int ch)
Returns true if the given character is considered to be whitespace.int
lastEvent()
private boolean
matchBareWord(char[] arr)
private int
next(int ch)
alternate implementation // middle is the pointer to the middle of a buffer to start scanning for a non-string // character ('"' or "/").int
nextEvent()
Returns the next event encountered in the JSON stream, one ofSTRING
LONG
NUMBER
BIGNUMBER
BOOLEAN
NULL
OBJECT_START
OBJECT_END
OBJECT_END
ARRAY_START
ARRAY_END
EOF
private void
pop()
private void
push()
private char
readEscapedChar()
private int
readExp(CharArr arr, int lim)
private int
readExpDigits(CharArr arr, int lim)
private int
readFrac(CharArr arr, int lim)
private long
readNumber(int firstChar, boolean isNeg)
Returns the long read...private void
readStringBare(CharArr arr)
private CharArr
readStringChars()
private void
readStringChars2(CharArr arr, int middle)
int
setFlags(int flags)
java.lang.String
toString()
boolean
wasKey()
-
-
-
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.IOException
Returns 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.IOException
alternate 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:
toString
in classjava.lang.Object
-
nextEvent
public int nextEvent() throws java.io.IOException
Returns 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.IOException
Returns 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 returnedCharArr
should *not* be modified as it may be shared with the input buffer. The returnedCharArr
will 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.IOException
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.- Throws:
java.io.IOException
-
getDouble
public double getDouble() throws java.io.IOException
Reads 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 returnedCharArr
should *not* be modified as it may be shared with the input buffer. The returnedCharArr
will 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.IOException
Reads a boolean value- Throws:
java.io.IOException
-
getNull
public void getNull() throws java.io.IOException
Reads 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()
-
-