Package org.noggit

Class JSONParser

  • Direct Known Subclasses:
    JSONParserWS

    public class JSONParser
    extends java.lang.Object
    Version:
    $Id: JSONParser.java 1099557 2011-05-04 18:54:26Z yonik $
    • 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
      • 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_BACKSLASH_ESCAPING_ANY_CHARACTER

        public static final int ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
        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
      • devNull

        private static final CharArr devNull
      • flags

        protected int flags
      • buf

        protected final char[] buf
      • start

        protected int start
      • end

        protected int end
      • in

        protected final java.io.Reader in
      • eof

        protected boolean eof
      • gpos

        protected long gpos
      • event

        protected int event
      • stringTerm

        protected int stringTerm
      • missingOpeningBrace

        protected boolean missingOpeningBrace
      • stack

        private byte[] stack
      • ptr

        private int ptr
      • state

        private byte state
      • valstate

        private int valstate
      • bool

        private boolean bool
      • lval

        private long lval
      • nstate

        private int nstate
    • 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

        protected 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

        protected boolean matchBareWord​(char[] arr)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • 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

        protected 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 class java.lang.Object
      • 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 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:
        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 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:
        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()