Package org.noggit
Class JSONParser
java.lang.Object
org.noggit.JSONParser
- Direct Known Subclasses:
JSONParserWS
- Version:
- $Id: JSONParser.java 1099557 2011-05-04 18:54:26Z yonik $
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intFlags to control parsing behaviorstatic final intALLOW_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 final intstatic final intstatic final intstatic final intstatic final intEvent indicating the end of a JSON arraystatic final intEvent indicating the start of a JSON arraystatic final intEvent indicating a JSON number value that was not produced by toString of any Java primitive numerics such as Double or Long.private booleanstatic final intEvent indicating a JSON booleanprotected final char[]private static final CharArrprivate static final byteprivate static final byteprivate static final byteprivate static final byteprivate static final byteprotected intprotected booleanstatic final intEvent indicating the end of input has been reachedprotected intprotected intstatic final intstatic final intprotected longprivate static final intprivate static final intprotected final Readerstatic final intEvent indicating a JSON number value which fits into a signed 64 bit integerprivate longprotected booleanprivate intstatic final intEvent indicating a JSON nullstatic final intEvent indicating a JSON number value which has a fractional part or an exponent and with string length invalid input: '<'= 23 chars not including sign.static final intEvent indicating the end of a JSON objectstatic final intEvent indicating the start of a JSON objectstatic final intprivate final CharArrprivate intprivate byte[]protected intprivate bytestatic final intEvent indicating a JSON string value, including member names of objectsprotected intprivate final CharArrprivate intprivate static final long -
Constructor Summary
ConstructorsConstructorDescriptionJSONParser(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
Modifier and TypeMethodDescriptionprivate voidcontinueNumber(CharArr arr) protected JSONParser.ParseExceptionprivate StringerrEscape(int a, int b) protected voidfill()booleanReads a boolean valueprotected intgetChar()protected intgetCharExpected(int expected) protected intprotected intgetCharNWS(int ch) private StringdoubleReads 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.private voidgetMore()protected voidvoidgetNull()Reads a null valueReturns the characters of a JSON numeric value.voidgetNumberChars(CharArr output) Reads a JSON numeric value into the output.longprotected voidReturns the JSON string value, decoding any escaped characters.voidReads a JSON string into the output, decoding any escaped characters.Returns the characters of a JSON string value, decoding any escaped characters.private voidgoTo(int what) protected voidhandleNonDoubleQuoteString(int ch, boolean isName) private inthexval(int hexdig) private static booleanisUnquotedStringChar(int ch) private static booleanisUnquotedStringStart(int ch) protected static final booleanisWhitespace(int ch) Returns true if the given character is considered to be whitespace.intprotected 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 "/").intReturns the next event encountered in the JSON stream, one ofSTRINGLONGNUMBERBIGNUMBERBOOLEANNULLOBJECT_STARTOBJECT_ENDOBJECT_ENDARRAY_STARTARRAY_ENDEOFprivate final voidpop()private final voidpush()private charprivate intprivate intreadExpDigits(CharArr arr, int lim) private intprivate longreadNumber(int firstChar, boolean isNeg) Returns the long read...private voidreadStringBare(CharArr arr) private CharArrprivate voidreadStringChars2(CharArr arr, int middle) intsetFlags(int flags) toString()booleanwasKey()
-
Field Details
-
STRING
public static final int STRINGEvent indicating a JSON string value, including member names of objects- See Also:
-
LONG
public static final int LONGEvent indicating a JSON number value which fits into a signed 64 bit integer- See Also:
-
NUMBER
public static final int NUMBEREvent indicating a JSON number value which has a fractional part or an exponent and with string length invalid input: '<'= 23 chars not including sign. This covers all representations of normal values for Double.toString().- See Also:
-
BIGNUMBER
public static final int BIGNUMBEREvent 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:
-
BOOLEAN
public static final int BOOLEANEvent indicating a JSON boolean- See Also:
-
NULL
public static final int NULLEvent indicating a JSON null- See Also:
-
OBJECT_START
public static final int OBJECT_STARTEvent indicating the start of a JSON object- See Also:
-
OBJECT_END
public static final int OBJECT_ENDEvent indicating the end of a JSON object- See Also:
-
ARRAY_START
public static final int ARRAY_STARTEvent indicating the start of a JSON array- See Also:
-
ARRAY_END
public static final int ARRAY_ENDEvent indicating the end of a JSON array- See Also:
-
EOF
public static final int EOFEvent indicating the end of input has been reached- See Also:
-
ALLOW_COMMENTS
public static final int ALLOW_COMMENTSFlags to control parsing behavior- See Also:
-
ALLOW_SINGLE_QUOTES
public static final int ALLOW_SINGLE_QUOTES- See Also:
-
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
public static final int ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER- See Also:
-
ALLOW_UNQUOTED_KEYS
public static final int ALLOW_UNQUOTED_KEYS- See Also:
-
ALLOW_UNQUOTED_STRING_VALUES
public static final int ALLOW_UNQUOTED_STRING_VALUES- See Also:
-
ALLOW_EXTRA_COMMAS
public static final int ALLOW_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. 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:
-
ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT
public static final int ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT- See Also:
-
OPTIONAL_OUTER_BRACES
public static final int OPTIONAL_OUTER_BRACES- See Also:
-
FLAGS_STRICT
public static final int FLAGS_STRICT- See Also:
-
FLAGS_DEFAULT
public static final int FLAGS_DEFAULT- See Also:
-
devNull
-
flags
protected int flags -
buf
protected final char[] buf -
start
protected int start -
end
protected int end -
in
-
eof
protected boolean eof -
gpos
protected long gpos -
event
protected int event -
stringTerm
protected int stringTerm -
missingOpeningBrace
protected boolean missingOpeningBrace -
out
-
stack
private byte[] stack -
ptr
private int ptr -
state
private byte state -
DID_OBJSTART
private static final byte DID_OBJSTART- See Also:
-
DID_ARRSTART
private static final byte DID_ARRSTART- See Also:
-
DID_ARRELEM
private static final byte DID_ARRELEM- See Also:
-
DID_MEMNAME
private static final byte DID_MEMNAME- See Also:
-
DID_MEMVAL
private static final byte DID_MEMVAL- See Also:
-
valstate
private int valstate -
WS_MASK
private static final long WS_MASK- See Also:
-
bool
private boolean bool -
lval
private long lval -
nstate
private int nstate -
HAS_FRACTION
private static final int HAS_FRACTION- See Also:
-
HAS_EXPONENT
private static final int HAS_EXPONENT- See Also:
-
tmp
-
-
Constructor Details
-
JSONParser
-
JSONParser
-
JSONParser
public JSONParser(char[] data, int start, int end) -
JSONParser
-
JSONParser
-
-
Method Details
-
getEventString
-
getFlags
public int getFlags() -
setFlags
public int setFlags(int flags) -
push
private final void push() -
pop
private final void pop() -
fill
- Throws:
IOException
-
getMore
- Throws:
IOException
-
getChar
- 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
- Throws:
IOException
-
getCharNWS
- Throws:
IOException
-
getCharExpected
- Throws:
IOException
-
getNewlineComment
- Throws:
IOException
-
getSlashComment
- Throws:
IOException
-
matchBareWord
- Throws:
IOException
-
err
-
getContext
-
errEscape
-
readNumber
Returns the long read... only significant if valstate==LONG after this call. firstChar should be the first numeric digit read.- Throws:
IOException
-
readFrac
- Throws:
IOException
-
readExp
- Throws:
IOException
-
readExpDigits
- Throws:
IOException
-
continueNumber
- Throws:
IOException
-
hexval
private int hexval(int hexdig) -
readEscapedChar
- Throws:
IOException
-
readStringChars
- Throws:
IOException
-
readStringChars2
- Throws:
IOException
-
readStringBare
- Throws:
IOException
-
handleNonDoubleQuoteString
- Throws:
IOException
-
isUnquotedStringStart
private static boolean isUnquotedStringStart(int ch) -
isUnquotedStringChar
private static boolean isUnquotedStringChar(int ch) -
next
alternate implementation // middle is the pointer to the middle of a buffer to start scanning for a non-string // character ('"' or "/"). startinvalid input: '<'=middleinvalid input: '<'end private void readStringChars2a(CharArr arr, int middle) throws IOException { int ch=0; for(;;) { // find the next non-string char for (; middleinvalid input: '<'end; middle++) { ch = buf[middle]; if (ch=='"' || ch=='\\') break; } arr.write(buf,start,middle-start); if (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:
IOException
-
toString
-
nextEvent
Returns the next event encountered in the JSON stream, one of- Throws:
IOException
-
lastEvent
public int lastEvent() -
wasKey
public boolean wasKey() -
goTo
- Throws:
IOException
-
getString
Returns the JSON string value, decoding any escaped characters.- Throws:
IOException
-
getStringChars
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
Reads a JSON string into the output, decoding any escaped characters.- Throws:
IOException
-
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.- Throws:
IOException
-
getDouble
Reads a number from the input stream and parses it as a double- Throws:
IOException
-
getNumberChars
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:
IOException
-
getNumberChars
Reads a JSON numeric value into the output.- Throws:
IOException
-
getBoolean
Reads a boolean value- Throws:
IOException
-
getNull
Reads 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()
-