Class JSON


  • public final class JSON
    extends java.lang.Object
    Contains utility methods for working with JSON or JSON-like data structures.

    Special treatment is afforded to java.util.Map and org.apache.pivot.collections.Map objects at any level of the hierarchy. Otherwise a BeanAdapter is used to fetch the value from the object.

    If, however, the object at a given level is a Sequence then the key is assumed to be an integer index into the sequence.

    Also, special consideration is given to an object that implements the Dictionary interface.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> boolean containsKey​(java.lang.Object root, java.lang.String path)
      Tests the existence of a path in a given object.
      static <T> T get​(java.lang.Object root, java.lang.String path)
      Returns the value at a given path.
      static <T> T get​(java.lang.Object root, Sequence<java.lang.String> keys)
      Returns the value at a given path.
      static byte getByte​(java.lang.Object root, java.lang.String path)  
      static double getDouble​(java.lang.Object root, java.lang.String path)  
      static float getFloat​(java.lang.Object root, java.lang.String path)  
      static int getInt​(java.lang.Object root, java.lang.String path)  
      static long getLong​(java.lang.Object root, java.lang.String path)  
      static short getShort​(java.lang.Object root, java.lang.String path)  
      static Sequence<java.lang.String> parse​(java.lang.String path)
      Parses a JSON path into a sequence of string keys.
      static <T> T put​(java.lang.Object root, java.lang.String path, T value)
      Sets the value at the given path.
      static <T> T remove​(java.lang.Object root, java.lang.String path)
      Removes the value at the given path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        public static <T> T get​(java.lang.Object root,
                                java.lang.String path)
        Returns the value at a given path.
        Type Parameters:
        T - The type of value to expect.
        Parameters:
        root - The root object.
        path - The path to the value as a JavaScript path.
        Returns:
        The value at the given path.
        See Also:
        get(Object, Sequence)
      • get

        public static <T> T get​(java.lang.Object root,
                                Sequence<java.lang.String> keys)
        Returns the value at a given path.
        Type Parameters:
        T - The type of value to expect.
        Parameters:
        root - The root object.
        keys - The path to the value as a sequence of keys.
        Returns:
        The value at the given path.
      • getByte

        public static byte getByte​(java.lang.Object root,
                                   java.lang.String path)
      • getShort

        public static short getShort​(java.lang.Object root,
                                     java.lang.String path)
      • getInt

        public static int getInt​(java.lang.Object root,
                                 java.lang.String path)
      • getLong

        public static long getLong​(java.lang.Object root,
                                   java.lang.String path)
      • getFloat

        public static float getFloat​(java.lang.Object root,
                                     java.lang.String path)
      • getDouble

        public static double getDouble​(java.lang.Object root,
                                       java.lang.String path)
      • put

        public static <T> T put​(java.lang.Object root,
                                java.lang.String path,
                                T value)
        Sets the value at the given path.
        Type Parameters:
        T - The type of value we're dealing with.
        Parameters:
        root - The root object.
        path - The path to the desired location from the root.
        value - The new value to set at the given path.
        Returns:
        The value previously associated with the path.
      • remove

        public static <T> T remove​(java.lang.Object root,
                                   java.lang.String path)
        Removes the value at the given path.
        Type Parameters:
        T - The type of value we're dealing with.
        Parameters:
        root - The root object.
        path - The path to the object (from the root) to remove.
        Returns:
        The value that was removed.
      • containsKey

        public static <T> boolean containsKey​(java.lang.Object root,
                                              java.lang.String path)
        Tests the existence of a path in a given object.
        Type Parameters:
        T - The type of value we're dealing with.
        Parameters:
        root - The root object.
        path - The path to test (from the root).
        Returns:
        true if the path exists; false, otherwise.
      • parse

        public static Sequence<java.lang.String> parse​(java.lang.String path)
        Parses a JSON path into a sequence of string keys.
        Parameters:
        path - The path to parse.
        Returns:
        The sequence of keys corresponding to the given path.