Class JsonHelper


  • public class JsonHelper
    extends java.lang.Object
    Helper class with many useful methods for manipulating JSON compatible objects.
    Author:
    Ivan Pribela
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonHelper​(java.lang.Object object)
      Constructs a new JsonHelper wrapper for easier manipulation of the given JSON compatible object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Iterable<?> asArray()
      Returns an array value of this object.
      static java.lang.Iterable<?> asArray​(java.lang.Object object)
      Returns the given object converted to a JSON array value.
      java.lang.Boolean asBoolean()
      Returns a boolean value of this object.
      static java.lang.Boolean asBoolean​(java.lang.Object object)
      Returns the given object converted to a boolean value.
      java.lang.Number asNumber()
      Returns a numeric value of this object.
      static java.lang.Number asNumber​(java.lang.Object object)
      Returns the given object converted to a number value.
      java.util.Map<java.lang.String,​java.lang.Object> asObject()
      Returns an object value of this object.
      static java.util.Map<java.lang.String,​java.lang.Object> asObject​(java.lang.Object object)
      Returns the given object converted to an JSON object value.
      java.lang.String asString()
      Returns a string value of this object.
      static java.lang.String asString​(java.lang.Object object)
      Returns the given object converted to a string value.
      static java.lang.Object get​(java.lang.Object object, java.lang.String path)
      Returns the raw object at the specified JSON path resolved on the given object.
      static java.lang.Iterable<?> getArray​(java.lang.Object object, java.lang.String path)
      Returns an array value at the specified JSON path resolved on the given object.
      static java.lang.Boolean getBoolean​(java.lang.Object object, java.lang.String path)
      Returns a boolean value at the specified JSON path resolved on the given object.
      static java.lang.Number getNumber​(java.lang.Object object, java.lang.String path)
      Returns a numeric value at the specified JSON path resolved on the given object.
      static java.util.Map<java.lang.String,​java.lang.Object> getObject​(java.lang.Object object, java.lang.String path)
      Returns an object value at the specified JSON path resolved on the given object.
      static java.lang.String getString​(java.lang.Object object, java.lang.String path)
      Returns a string value at the specified JSON path resolved on the given object.
      JsonType getType()
      Returns the JSON type of this object.
      static JsonType getType​(java.lang.Object object)
      Returns the JSON type to which the given object can be converted.
      • Methods inherited from class java.lang.Object

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

      • JsonHelper

        public JsonHelper​(java.lang.Object object)
        Constructs a new JsonHelper wrapper for easier manipulation of the given JSON compatible object.
        Parameters:
        object - the object to wrap
    • Method Detail

      • getType

        public JsonType getType()
        Returns the JSON type of this object.
        Returns:
        the JSON type of this object.
      • asBoolean

        public java.lang.Boolean asBoolean()
        Returns a boolean value of this object.
        Returns:
        a boolean value of this object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asNumber

        public java.lang.Number asNumber()
        Returns a numeric value of this object.
        Returns:
        a numeric value of this object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asString

        public java.lang.String asString()
        Returns a string value of this object.
        Returns:
        a string value of this object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asArray

        public java.lang.Iterable<?> asArray()
        Returns an array value of this object.
        Returns:
        an array value of this object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asObject

        public java.util.Map<java.lang.String,​java.lang.Object> asObject()
        Returns an object value of this object.
        Returns:
        an object value of this object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getType

        public static JsonType getType​(java.lang.Object object)
        Returns the JSON type to which the given object can be converted.
        Parameters:
        object - the object for which the type is determined
        Returns:
        JSON type of the given object.
      • asBoolean

        public static java.lang.Boolean asBoolean​(java.lang.Object object)
                                           throws SvetovidJsonException
        Returns the given object converted to a boolean value.
        Parameters:
        object - the object to convert
        Returns:
        a boolean value of the given object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asNumber

        public static java.lang.Number asNumber​(java.lang.Object object)
                                         throws SvetovidJsonException
        Returns the given object converted to a number value.
        Parameters:
        object - the object to convert
        Returns:
        a number value of the given object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asString

        public static java.lang.String asString​(java.lang.Object object)
                                         throws SvetovidJsonException
        Returns the given object converted to a string value.
        Parameters:
        object - the object to convert
        Returns:
        a string value of the given object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asArray

        public static java.lang.Iterable<?> asArray​(java.lang.Object object)
                                             throws SvetovidJsonException
        Returns the given object converted to a JSON array value.
        Parameters:
        object - the object to convert
        Returns:
        an array value of the given object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • asObject

        public static java.util.Map<java.lang.String,​java.lang.Object> asObject​(java.lang.Object object)
                                                                               throws SvetovidJsonException
        Returns the given object converted to an JSON object value.
        Parameters:
        object - the object to convert
        Returns:
        an object value of the given object.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getBoolean

        public static java.lang.Boolean getBoolean​(java.lang.Object object,
                                                   java.lang.String path)
                                            throws SvetovidJsonException
        Returns a boolean value at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the boolean value extracted from the given object using the given path.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getNumber

        public static java.lang.Number getNumber​(java.lang.Object object,
                                                 java.lang.String path)
                                          throws SvetovidJsonException
        Returns a numeric value at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the numeric value extracted from the given object using the given path.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getString

        public static java.lang.String getString​(java.lang.Object object,
                                                 java.lang.String path)
                                          throws SvetovidJsonException
        Returns a string value at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the string value extracted from the given object using the given path.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getArray

        public static java.lang.Iterable<?> getArray​(java.lang.Object object,
                                                     java.lang.String path)
                                              throws SvetovidJsonException
        Returns an array value at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the array value extracted from the given object using the given path.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • getObject

        public static java.util.Map<java.lang.String,​java.lang.Object> getObject​(java.lang.Object object,
                                                                                       java.lang.String path)
                                                                                throws SvetovidJsonException
        Returns an object value at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the object value extracted from the given object using the given path.
        Throws:
        SvetovidJsonException - if the object cannot be converted.
      • get

        public static java.lang.Object get​(java.lang.Object object,
                                           java.lang.String path)
        Returns the raw object at the specified JSON path resolved on the given object.
        Parameters:
        object - the object to apply the path to
        path - the path to follow
        Returns:
        the raw object extracted from the given object using the given path.
        Throws:
        SvetovidFormatException - if there was an error parsing the path.
        SvetovidJsonException - if the array or object along the path cannot be converted.