Class JSONObject

java.lang.Object
kong.unirest.core.json.JSONElement
kong.unirest.core.json.JSONObject
Direct Known Subclasses:
NullObject

public class JSONObject extends JSONElement
https://json.org/ https://tools.ietf.org/html/rfc7159#section-4 represents a JSON Object
  • Field Details

  • Constructor Details

    • JSONObject

      public JSONObject(String string)
      https://tools.ietf.org/html/rfc7159#section-4
      Parameters:
      string - a json object string
    • JSONObject

      public JSONObject(Map<String,Object> map)
      construct using a map
      Parameters:
      map - a map representing the elements of a JSON Object
    • JSONObject

      public JSONObject(Object object)
      construct using an object. The Properties of the JSONObject will be taken from getters and properties of the object
      Parameters:
      object - the object to turn into a JSONObject
    • JSONObject

      public JSONObject()
      an empty JSON object
    • JSONObject

      JSONObject(JsonEngine.Element jsonElement)
  • Method Details

    • quote

      public static String quote(String s)
      quite escape a string
      Parameters:
      s - a string
      Returns:
      a quoted string
    • quote

      public static Writer quote(String s, Writer writer) throws IOException
      quite escape a string
      Parameters:
      s - a string
      writer - a writer to write the string to
      Returns:
      the same writer
      Throws:
      IOException - if some IO thing goes wrong
    • stringToValue

      public static Object stringToValue(String str)
      convert a primitive JSON type in a string (bool, number, null) to its primitive type all decimal types will become doubles
      Parameters:
      str - a string
      Returns:
      a object
    • wrap

      public static Object wrap(Object obj)
      Convert an object to a object that can be added to a JSONElement If the object is null return the NULL object If the object is primitive return the original object If the object is a map convert it to a JSONObject If the object is a Collection or array return a JSONArray If the object is anything else return a empty JSON Object
      Parameters:
      obj - the object
      Returns:
      another object suitable for use as JSON
    • wrapArray

      private static JSONArray wrapArray(Object obj)
    • doubleToString

      public static String doubleToString(double d)
      convert a primitive number to a string if the double can be converted to a whole number the decimal will be dropped
      Parameters:
      d - a double
      Returns:
      a string representation of the double
    • numberToString

      public static String numberToString(Number number) throws JSONException
      Convert a number to a string
      Parameters:
      number - the number to convert
      Returns:
      a string representation of that number
      Throws:
      JSONException - if something goes wrong
    • valueToString

      public static String valueToString(Object o) throws JSONException
      Converts an object to a JSON String
      Parameters:
      o - any object
      Returns:
      a json string
      Throws:
      JSONException - if something goes wrong
    • getNames

      public static String[] getNames(JSONObject jsonObject)
      get all of the keys of a JSONObject
      Parameters:
      jsonObject - a JSONObject
      Returns:
      a String[] of the objects keys
    • getNames

      public static String[] getNames(Object o)
      get all of the keys of a JSONObject or a empty array if not an JSONObject
      Parameters:
      o - a Object
      Returns:
      a String[] of the objects keys
    • asElement

      JsonEngine.Element asElement()
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      the object as a JSON string with no formatting
    • toString

      public String toString(int i) throws JSONException
      render the object as a JSON String
      Parameters:
      i - (ignored due to limitations in gson which uses a hardcoded indentation)
      Returns:
      a JSON String
      Throws:
      JSONException
    • similar

      public boolean similar(Object o)
      indicates if a JSONObject has the same elements as another JSONObject
      Parameters:
      o - another object
      Returns:
      a bool
    • has

      public boolean has(String key)
      Parameters:
      key - the key element to operate on
      Returns:
      indicates that the structure has this key
    • length

      public int length()
      Returns:
      number of keys in the structure
    • get

      public Object get(String key) throws JSONException
      get and element by key as its native object
      Parameters:
      key - the key element to operate on
      Returns:
      the object, this could be an object, array or primitive
      Throws:
      JSONException - if the key does not exist
    • getJSONObject

      public JSONObject getJSONObject(String key) throws JSONException
      get the element as a JSONObject
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a JSONObject
      Throws:
      JSONException - if it is not a object or the key does not exist
    • optJSONObject

      public JSONObject optJSONObject(String key)
      get the element as a JSONObject
      Parameters:
      key - the key element to operate on
      Returns:
      an object or null if it is not an object or the key does not exist
    • getJSONArray

      public JSONArray getJSONArray(String key) throws JSONException
      get the element as a JSONArray
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a JSONArray
      Throws:
      JSONException - if it is not an array or the key does not exist
    • optJSONArray

      public JSONArray optJSONArray(String key)
      optionally get the element as a JSONArray
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a JSONArray or null if it doesn't exist or is not an array
    • getString

      public String getString(String key) throws JSONException
      get a element property as a string
      Parameters:
      key - the key element to operate on
      Returns:
      a string representation of the value
      Throws:
      JSONException - if the key does not exist
    • optString

      public String optString(String key)
      get a element property as a string
      Parameters:
      key - the key element to operate on
      Returns:
      a string representation of the value or null of it doesn't exist
    • optString

      public String optString(String key, String defaultValue)
      get a element property as a string
      Parameters:
      key - the key element to operate on
      defaultValue - default value if the key does not exist or cannot be converted to a string
      Returns:
      a string representation of the value or default value
    • getDouble

      public double getDouble(String key) throws JSONException
      get the value as a double
      Parameters:
      key - the key element to operate on
      Returns:
      the value
      Throws:
      JSONException - if the object is not a number or does not exist
    • optDouble

      public double optDouble(String key)
      the value as double or NaN
      Parameters:
      key - the key element to operate on
      Returns:
      the value as a double or NaN if the key doesn't exist or the value is not a number
    • optDouble

      public double optDouble(String key, double defaultValue)
      get the value as a double or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as double or a default value if value is not viable
    • getFloat

      public float getFloat(String key) throws JSONException
      get the value as a float
      Parameters:
      key - the key element to operate on
      Returns:
      the value
      Throws:
      JSONException - if the object is not a number or does not exist
    • optFloat

      public float optFloat(String key)
      the value as double or NaN
      Parameters:
      key - the key element to operate on
      Returns:
      the value as a float or NaN if the key doesn't exist or the value is not a number
    • optFloat

      public float optFloat(String key, float defaultValue)
      get the value as a float or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as double or a default value if value is not viable
    • getLong

      public long getLong(String key) throws JSONException
      get the value as a long
      Parameters:
      key - the key element to operate on
      Returns:
      the value
      Throws:
      JSONException - if the object is not a number or does not exist
    • optLong

      public long optLong(String key)
      the value as long or NaN
      Parameters:
      key - the key element to operate on
      Returns:
      the value as a long or NaN if the key doesn't exist or the value is not a number
    • optLong

      public long optLong(String key, long defaultValue)
      get the value as a long or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as long or a default value if value is not viable
    • getNumber

      public Number getNumber(String key) throws JSONException
      get an element property as a Number
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a Number if it can be cast to one.
      Throws:
      JSONException - if it is not a number or the key does not exist
    • optNumber

      public Number optNumber(String key)
      the value as int or 0
      Parameters:
      key - the key element to operate on
      Returns:
      the value as a int or 0 if the key doesn't exist or the value is not a number
    • optNumber

      public Number optNumber(String key, Number defaultValue)
      get the value as a Number or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as long or a default value if value is not viable
    • getInt

      public int getInt(String key) throws JSONException
      get an element property as a int
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a int if it can be cast to one.
      Throws:
      JSONException - if it is not a number or the key does not exist
    • optInt

      public int optInt(String key)
      the value as int or NaN
      Parameters:
      key - the key element to operate on
      Returns:
      the value as a int or 0 if the key doesn't exist or the value is not a number
    • optInt

      public int optInt(String key, int defaultValue)
      get the value as a int or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as long or a default value if value is not viable
    • getBigInteger

      public BigInteger getBigInteger(String key) throws JSONException
      get an element property as a BigInteger
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a BigInteger if it can be cast to one.
      Throws:
      JSONException - if it is not a number or the key does not exist
    • optBigInteger

      public BigInteger optBigInteger(String key, BigInteger defaultValue)
      get the value as a BigInteger or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as BigInteger or a default value if value is not viable
    • getBigDecimal

      public BigDecimal getBigDecimal(String key) throws JSONException
      get an element property as a BigDecimal
      Parameters:
      key - the key element to operate on
      Returns:
      the element as a BigInteger if it can be cast to one.
      Throws:
      JSONException - if it is not a number or the key does not exist
    • optBigDecimal

      public BigDecimal optBigDecimal(String key, BigDecimal defaultValue)
      get the value as a BigDecimal or default value
      Parameters:
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      return value as BigDecimal or a default value if value is not viable
    • getBoolean

      public boolean getBoolean(String key) throws JSONException
      gets a boolean value at a particular key
      Parameters:
      key - the key
      Returns:
      a boolean
      Throws:
      JSONException - if the element does not exist or is not a boolean
    • optBoolean

      public boolean optBoolean(String key)
      gets a boolean value at a particular key or false as default
      Parameters:
      key - the key
      Returns:
      a boolean
    • optBoolean

      public boolean optBoolean(String key, boolean defaultValue)
      gets a boolean value at a particular key or a default value
      Parameters:
      key - the key
      defaultValue - a default value if the key does not exist or value is not a boolean
      Returns:
      a boolean
    • getEnum

      public <T extends Enum<T>> T getEnum(Class<T> enumClass, String key) throws JSONException
      get element as a enum value
      Type Parameters:
      T - the type of enum you want
      Parameters:
      enumClass - a enum class
      key - the key element to operate on
      Returns:
      the value as a enum of T
      Throws:
      JSONException - if it does not map to a enum of T or the key does not exist
    • optEnum

      public <T extends Enum<T>> T optEnum(Class<T> enumClass, String key)
      get element as a enum value or null if the value cannot be mapped
      Type Parameters:
      T - the type of enum you want
      Parameters:
      enumClass - a enum class
      key - the key element to operate on
      Returns:
      the value as a enum of T
    • optEnum

      public <T extends Enum<T>> T optEnum(Class<T> enumClass, String key, T defaultValue)
      get element as a enum value or a default value if the value cannot be mapped
      Type Parameters:
      T - the type of enum you want
      Parameters:
      enumClass - a enum class
      key - the key element to operate on
      defaultValue - the default value to return if the index or value type are not valid
      Returns:
      the value as a enum of T
    • put

      public JSONObject put(String key, JSONObject object) throws JSONException
      put a JSONObject at a particular key
      Parameters:
      key - the key element to operate on
      object - JSONObject
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public JSONObject put(String key, JSONArray array) throws JSONException
      put a JSONArray at a particular key
      Parameters:
      key - the key element to operate on
      array - JSONArray
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public JSONObject put(String key, boolean value) throws JSONException
      put a boolean at a particular key
      Parameters:
      key - the key element to operate on
      value - the boolean value to put
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • put

      public JSONObject put(String key, Number value) throws JSONException
      put a Number at a particular key
      Parameters:
      key - the key element to operate on
      value - Number
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public JSONObject put(String key, double value) throws JSONException
      put a double at a particular key
      Parameters:
      key - the key element to operate on
      value - double
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • put

      public JSONObject put(String key, float value) throws JSONException
      put a float at a particular key
      Parameters:
      key - the key element to operate on
      value - float
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • put

      public JSONObject put(String key, long value) throws JSONException
      put a long at a particular key
      Parameters:
      key - the key element to operate on
      value - long
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • put

      public JSONObject put(String key, int value) throws JSONException
      put a int at a particular key
      Parameters:
      key - the key element to operate on
      value - int
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • put

      public JSONObject put(String key, String value) throws JSONException
      put a String at a particular key
      Parameters:
      key - the key element to operate on
      value - Number
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public JSONObject put(String key, Collection value) throws JSONException
      put a Collection as a JSONArray at a particular key
      Parameters:
      key - the key element to operate on
      value - Collection
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public JSONObject put(String key, Map value) throws JSONException
      put a Collection as a JSONArray at a particular key
      Parameters:
      key - the key element to operate on
      value - Collection
      Returns:
      this JSONObject
      Throws:
      JSONException
    • put

      public <T extends Enum<T>> JSONObject put(String key, T enumvalue) throws JSONException
      put a enum at a particular key. The enum will be stored as a string by name
      Type Parameters:
      T - a type of enum
      Parameters:
      key - the key element to operate on
      enumvalue - a enum
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • remove

      public Object remove(String key)
      remove a element by key name
      Parameters:
      key - the key element to operate on
      Returns:
      the object value that was removed
    • accumulate

      public JSONObject accumulate(String key, Object additionalValue) throws JSONException
      Add a element to a JSONArray in a element. If the value is not already an array it will be made one with the original value as the first element
      Parameters:
      key - the key element to operate on
      additionalValue - value to append to the array
      Returns:
      this JSONObject
      Throws:
      JSONException
    • append

      public JSONObject append(String key, Object value) throws JSONException
      appends to an existing array
      Parameters:
      key - the key element to operate on
      value - the object to put
      Returns:
      this JSONObject
      Throws:
      JSONException - if the value exists and is not an array
    • increment

      public JSONObject increment(String key) throws JSONException
      increments a numeric value by 1, or creates it with a value of 1 if it does not exist.
      Parameters:
      key - the key element to operate on
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • putOnce

      public JSONObject putOnce(String key, Object value) throws JSONException
      put a value to a key only if it does not exist
      Parameters:
      key - the key element to operate on
      value - the object to put
      Returns:
      this JSONObject
      Throws:
      JSONException - if the key exists.
    • put

      public JSONObject put(String key, Object value) throws JSONException
      put an object to a key. the value must be a JSON type
      Parameters:
      key - the key element to operate on
      value - the object to put
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • putOpt

      public JSONObject putOpt(String key, Object value) throws JSONException
      optional put a value at a key as long as both they key and value are not null otherwise it does nothing
      Parameters:
      key - the key element to operate on
      value - the object to put
      Returns:
      this JSONObject
      Throws:
      JSONException - if something goes wrong
    • keySet

      public Set<String> keySet()
      get all the keys as a set
      Returns:
      a set of keys
    • keys

      public Iterator<String> keys()
      get a iterator for the keyset
      Returns:
      a Iterator of keys
    • toMap

      public Map<String,Object> toMap()
      converts this object to a map
      Returns:
      this object as a map
    • names

      public JSONArray names()
      get the key names as a JSONArray
      Returns:
      a JSONArray of keys
    • toJSONArray

      public JSONArray toJSONArray(JSONArray names) throws JSONException
      creates an array of the values for they keys you provide
      Parameters:
      names - a list of keys you want an array for
      Returns:
      a JSONArray of values or null of the array is null or empty
      Throws:
      JSONException - if something goes wrong
    • getProperty

      private JsonEngine.Element getProperty(String key)
    • tryNumber

      private <T extends Number> T tryNumber(Supplier<T> supplier, String key)
    • getOrDefault

      private <T> T getOrDefault(Supplier<T> supplier, T defaultValue)
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • opt

      public Object opt(String key)
      optionally return the object or null if it doesn't exist
      Parameters:
      key - the key
      Returns:
      the object at the key or null
    • isEmpty

      public boolean isEmpty()
      Returns:
      boolean if the object is empty
    • isNull

      public boolean isNull(String key)
      indicate if the key does not exist or its value is null
      Parameters:
      key - the key
      Returns:
      a boolean indicating null
    • isPrimitive

      private static boolean isPrimitive(Object o)