Class JsonHelper

java.lang.Object
org.svetovid.util.JsonHelper

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

    • JsonHelper

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

    • getType

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

      public 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 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 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 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 Map<String,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(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 Boolean asBoolean(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 Number asNumber(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 String asString(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 Iterable<?> asArray(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 Map<String,Object> asObject(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 Boolean getBoolean(Object object, 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 Number getNumber(Object object, 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 String getString(Object object, 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 Iterable<?> getArray(Object object, 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 Map<String,Object> getObject(Object object, 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 Object get(Object object, 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.