Class JsonHandler<A,​O>

  • Type Parameters:
    A - The type of handlers used for JSON arrays
    O - The type of handlers used for JSON objects

    public abstract class JsonHandler<A,​O>
    extends Object
    A handler for parser events. Instances of this class can be given to a JsonParser. The parser will then call the methods of the given handler while reading the input.

    The default implementations of these methods do nothing. Subclasses may override only those methods they are interested in. They can use getLocation() to access the current character position of the parser at any point. The start* methods will be called while the location points to the first character of the parsed element. The end* methods will be called while the location points to the character position that directly follows the last character of the parsed element. Example:

     ["lorem ipsum"]
      ^            ^
      startString  endString
     

    Subclasses that build an object representation of the parsed JSON can return arbitrary handler objects for JSON arrays and JSON objects in startArray() and startObject(). These handler objects will then be provided in all subsequent parser events for this particular array or object. They can be used to keep track the elements of a JSON array or object.

    See Also:
    JsonParser
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonHandler()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void endArray​(A array)
      Indicates the end of an array in the JSON input.
      void endArrayValue​(A array)
      Indicates the end of an array element in the JSON input.
      void endBoolean​(boolean value)
      Indicates the end of a boolean literal (true or false) in the JSON input.
      void endNull()
      Indicates the end of a null literal in the JSON input.
      void endNumber​(String string)
      Indicates the end of a number in the JSON input.
      void endObject​(O object)
      Indicates the end of an object in the JSON input.
      void endObjectName​(O object, String name)
      Indicates the end of an object member name in the JSON input.
      void endObjectValue​(O object, String name)
      Indicates the end of an object member value in the JSON input.
      void endString​(String string)
      Indicates the end of a string in the JSON input.
      protected Location getLocation()
      Returns the current parser location.
      A startArray()
      Indicates the beginning of an array in the JSON input.
      void startArrayValue​(A array)
      Indicates the beginning of an array element in the JSON input.
      void startBoolean()
      Indicates the beginning of a boolean literal (true or false) in the JSON input.
      void startNull()
      Indicates the beginning of a null literal in the JSON input.
      void startNumber()
      Indicates the beginning of a number in the JSON input.
      O startObject()
      Indicates the beginning of an object in the JSON input.
      void startObjectName​(O object)
      Indicates the beginning of the name of an object member in the JSON input.
      void startObjectValue​(O object, String name)
      Indicates the beginning of the name of an object member in the JSON input.
      void startString()
      Indicates the beginning of a string in the JSON input.
    • Method Detail

      • getLocation

        protected Location getLocation()
        Returns the current parser location.
        Returns:
        the current parser location
      • startNull

        public void startNull()
        Indicates the beginning of a null literal in the JSON input. This method will be called when reading the first character of the literal.
      • endNull

        public void endNull()
        Indicates the end of a null literal in the JSON input. This method will be called after reading the last character of the literal.
      • startBoolean

        public void startBoolean()
        Indicates the beginning of a boolean literal (true or false) in the JSON input. This method will be called when reading the first character of the literal.
      • endBoolean

        public void endBoolean​(boolean value)
        Indicates the end of a boolean literal (true or false) in the JSON input. This method will be called after reading the last character of the literal.
        Parameters:
        value - the parsed boolean value
      • startString

        public void startString()
        Indicates the beginning of a string in the JSON input. This method will be called when reading the opening double quote character ('"').
      • endString

        public void endString​(String string)
        Indicates the end of a string in the JSON input. This method will be called after reading the closing double quote character ('"').
        Parameters:
        string - the parsed string
      • startNumber

        public void startNumber()
        Indicates the beginning of a number in the JSON input. This method will be called when reading the first character of the number.
      • endNumber

        public void endNumber​(String string)
        Indicates the end of a number in the JSON input. This method will be called after reading the last character of the number.
        Parameters:
        string - the parsed number string
      • startArray

        public A startArray()
        Indicates the beginning of an array in the JSON input. This method will be called when reading the opening square bracket character ('[').

        This method may return an object to handle subsequent parser events for this array. This array handler will then be provided in all calls to startArrayValue(), endArrayValue(), and endArray() for this array.

        Returns:
        a handler for this array, or null if not needed
      • endArray

        public void endArray​(A array)
        Indicates the end of an array in the JSON input. This method will be called after reading the closing square bracket character (']').
        Parameters:
        array - the array handler returned from startArray(), or null if not provided
      • startArrayValue

        public void startArrayValue​(A array)
        Indicates the beginning of an array element in the JSON input. This method will be called when reading the first character of the element, just before the call to the start method for the specific element type (startString(), startNumber(), etc.).
        Parameters:
        array - the array handler returned from startArray(), or null if not provided
      • endArrayValue

        public void endArrayValue​(A array)
        Indicates the end of an array element in the JSON input. This method will be called after reading the last character of the element value, just after the end method for the specific element type (like endString(), endNumber(), etc.).
        Parameters:
        array - the array handler returned from startArray(), or null if not provided
      • startObject

        public O startObject()
        Indicates the beginning of an object in the JSON input. This method will be called when reading the opening curly bracket character ('{').

        This method may return an object to handle subsequent parser events for this object. This object handler will be provided in all calls to startObjectName(), endObjectName(), startObjectValue(), endObjectValue(), and endObject() for this object.

        Returns:
        a handler for this object, or null if not needed
      • endObject

        public void endObject​(O object)
        Indicates the end of an object in the JSON input. This method will be called after reading the closing curly bracket character ('}').
        Parameters:
        object - the object handler returned from startObject(), or null if not provided
      • startObjectName

        public void startObjectName​(O object)
        Indicates the beginning of the name of an object member in the JSON input. This method will be called when reading the opening quote character ('"') of the member name.
        Parameters:
        object - the object handler returned from startObject(), or null if not provided
      • endObjectName

        public void endObjectName​(O object,
                                  String name)
        Indicates the end of an object member name in the JSON input. This method will be called after reading the closing quote character ('"') of the member name.
        Parameters:
        object - the object handler returned from startObject(), or null if not provided
        name - the parsed member name
      • startObjectValue

        public void startObjectValue​(O object,
                                     String name)
        Indicates the beginning of the name of an object member in the JSON input. This method will be called when reading the opening quote character ('"') of the member name.
        Parameters:
        object - the object handler returned from startObject(), or null if not provided
        name - the member name
      • endObjectValue

        public void endObjectValue​(O object,
                                   String name)
        Indicates the end of an object member value in the JSON input. This method will be called after reading the last character of the member value, just after the end method for the specific member type (like endString(), endNumber(), etc.).
        Parameters:
        object - the object handler returned from startObject(), or null if not provided
        name - the parsed member name