Package org.noggit

Class JSONParser

java.lang.Object
org.noggit.JSONParser
Direct Known Subclasses:
JSONParserWS

public class JSONParser extends Object
Version:
$Id: JSONParser.java 1099557 2011-05-04 18:54:26Z yonik $
  • Field Details

    • STRING

      public static final int STRING
      Event indicating a JSON string value, including member names of objects
      See Also:
    • LONG

      public static final int LONG
      Event indicating a JSON number value which fits into a signed 64 bit integer
      See Also:
    • NUMBER

      public static final int NUMBER
      Event 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 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:
    • BOOLEAN

      public static final int BOOLEAN
      Event indicating a JSON boolean
      See Also:
    • NULL

      public static final int NULL
      Event indicating a JSON null
      See Also:
    • OBJECT_START

      public static final int OBJECT_START
      Event indicating the start of a JSON object
      See Also:
    • OBJECT_END

      public static final int OBJECT_END
      Event indicating the end of a JSON object
      See Also:
    • ARRAY_START

      public static final int ARRAY_START
      Event indicating the start of a JSON array
      See Also:
    • ARRAY_END

      public static final int ARRAY_END
      Event indicating the end of a JSON array
      See Also:
    • EOF

      public static final int EOF
      Event indicating the end of input has been reached
      See Also:
    • ALLOW_COMMENTS

      public static final int ALLOW_COMMENTS
      Flags 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_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:
    • FLAGS_STRICT

      public static final int FLAGS_STRICT
      See Also:
    • FLAGS_DEFAULT

      public static final int FLAGS_DEFAULT
      See Also:
    • devNull

      private static final CharArr devNull
    • flags

      int flags
    • buf

      final char[] buf
    • start

      int start
    • end

      int end
    • in

      final 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:
    • 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

      private final CharArr tmp
  • Constructor Details

    • JSONParser

      public JSONParser(Reader in)
    • JSONParser

      public JSONParser(Reader in, char[] buffer)
    • JSONParser

      public JSONParser(char[] data, int start, int end)
    • JSONParser

      public JSONParser(String data)
    • JSONParser

      public JSONParser(String data, int start, int end)
  • Method Details

    • getEventString

      public static 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 IOException
      Throws:
      IOException
    • getMore

      private void getMore() 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

      private 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

      private boolean matchBareWord(char[] arr) throws IOException
      Throws:
      IOException
    • err

      protected JSONParser.ParseException err(String msg)
    • getContext

      private String getContext()
    • errEscape

      private String errEscape(int a, int b)
    • readNumber

      private long readNumber(int firstChar, boolean isNeg) throws IOException
      Returns the long read... only significant if valstate==LONG after this call. firstChar should be the first numeric digit read.
      Throws:
      IOException
    • readFrac

      private int readFrac(CharArr arr, int lim) throws IOException
      Throws:
      IOException
    • readExp

      private int readExp(CharArr arr, int lim) throws IOException
      Throws:
      IOException
    • readExpDigits

      private int readExpDigits(CharArr arr, int lim) throws IOException
      Throws:
      IOException
    • continueNumber

      private void continueNumber(CharArr arr) throws IOException
      Throws:
      IOException
    • hexval

      private int hexval(int hexdig)
    • readEscapedChar

      private char readEscapedChar() throws IOException
      Throws:
      IOException
    • readStringChars

      private CharArr readStringChars() throws IOException
      Throws:
      IOException
    • readStringChars2

      private void readStringChars2(CharArr arr, int middle) throws IOException
      Throws:
      IOException
    • readStringBare

      private void readStringBare(CharArr arr) throws IOException
      Throws:
      IOException
    • handleNonDoubleQuoteString

      private void handleNonDoubleQuoteString(int ch, boolean isName) throws IOException
      Throws:
      IOException
    • isUnquotedStringStart

      private static boolean isUnquotedStringStart(int ch)
    • isUnquotedStringChar

      private static boolean isUnquotedStringChar(int ch)
    • next

      private int next(int ch) throws IOException
      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

      public String toString()
      Overrides:
      toString in class Object
    • nextEvent

      public int nextEvent() throws IOException
      Returns the next event encountered in the JSON stream, one of
      Throws:
      IOException
    • lastEvent

      public int lastEvent()
    • wasKey

      public boolean wasKey()
    • goTo

      private void goTo(int what) throws IOException
      Throws:
      IOException
    • 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 returned CharArr should *not* be modified as it may be shared with the input buffer.

      The returned CharArr will 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 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:
      IOException
    • getDouble

      public double getDouble() throws IOException
      Reads 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 CharArr should *not* be modified as it may be shared with the input buffer.

      The returned CharArr will 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 IOException
      Reads a boolean value
      Throws:
      IOException
    • getNull

      public void getNull() throws IOException
      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()