Class 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
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONObject()
      an empty JSON object
      JSONObject​(java.lang.Object object)
      construct using an object.
      JSONObject​(java.lang.String string)
      https://tools.ietf.org/html/rfc7159#section-4
      JSONObject​(java.util.Map<java.lang.String,​java.lang.Object> map)
      construct using a map
      JSONObject​(JsonEngine.Element jsonElement)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JSONObject accumulate​(java.lang.String key, java.lang.Object additionalValue)
      Add a element to a JSONArray in a element.
      JSONObject append​(java.lang.String key, java.lang.Object value)
      appends to an existing array
      (package private) JsonEngine.Element asElement()  
      static java.lang.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
      boolean equals​(java.lang.Object other)  
      java.lang.Object get​(java.lang.String key)
      get and element by key as its native object
      java.math.BigDecimal getBigDecimal​(java.lang.String key)
      get an element property as a BigDecimal
      java.math.BigInteger getBigInteger​(java.lang.String key)
      get an element property as a BigInteger
      boolean getBoolean​(java.lang.String key)
      gets a boolean value at a particular key
      double getDouble​(java.lang.String key)
      get the value as a double
      <T extends java.lang.Enum<T>>
      T
      getEnum​(java.lang.Class<T> enumClass, java.lang.String key)
      get element as a enum value
      float getFloat​(java.lang.String key)
      get the value as a float
      int getInt​(java.lang.String key)
      get an element property as a int
      JSONArray getJSONArray​(java.lang.String key)
      get the element as a JSONArray
      JSONObject getJSONObject​(java.lang.String key)
      get the element as a JSONObject
      long getLong​(java.lang.String key)
      get the value as a long
      static java.lang.String[] getNames​(java.lang.Object o)
      get all of the keys of a JSONObject or a empty array if not an JSONObject
      static java.lang.String[] getNames​(JSONObject jsonObject)
      get all of the keys of a JSONObject
      java.lang.Number getNumber​(java.lang.String key)
      get an element property as a Number
      private <T> T getOrDefault​(java.util.function.Supplier<T> supplier, T defaultValue)  
      private JsonEngine.Element getProperty​(java.lang.String key)  
      java.lang.String getString​(java.lang.String key)
      get a element property as a string
      boolean has​(java.lang.String key)  
      int hashCode()  
      JSONObject increment​(java.lang.String key)
      increments a numeric value by 1, or creates it with a value of 1 if it does not exist.
      boolean isEmpty()  
      boolean isNull​(java.lang.String key)
      indicate if the key does not exist or its value is null
      private static boolean isPrimitive​(java.lang.Object o)  
      java.util.Iterator<java.lang.String> keys()
      get a iterator for the keyset
      java.util.Set<java.lang.String> keySet()
      get all the keys as a set
      int length()  
      JSONArray names()
      get the key names as a JSONArray
      static java.lang.String numberToString​(java.lang.Number number)
      Convert a number to a string
      java.lang.Object opt​(java.lang.String key)
      optionally return the object or null if it doesn't exist
      java.math.BigDecimal optBigDecimal​(java.lang.String key, java.math.BigDecimal defaultValue)
      get the value as a BigDecimal or default value
      java.math.BigInteger optBigInteger​(java.lang.String key, java.math.BigInteger defaultValue)
      get the value as a BigInteger or default value
      boolean optBoolean​(java.lang.String key)
      gets a boolean value at a particular key or false as default
      boolean optBoolean​(java.lang.String key, boolean defaultValue)
      gets a boolean value at a particular key or a default value
      double optDouble​(java.lang.String key)
      the value as double or NaN
      double optDouble​(java.lang.String key, double defaultValue)
      get the value as a double or default value
      <T extends java.lang.Enum<T>>
      T
      optEnum​(java.lang.Class<T> enumClass, java.lang.String key)
      get element as a enum value or null if the value cannot be mapped
      <T extends java.lang.Enum<T>>
      T
      optEnum​(java.lang.Class<T> enumClass, java.lang.String key, T defaultValue)
      get element as a enum value or a default value if the value cannot be mapped
      float optFloat​(java.lang.String key)
      the value as double or NaN
      float optFloat​(java.lang.String key, float defaultValue)
      get the value as a float or default value
      int optInt​(java.lang.String key)
      the value as int or NaN
      int optInt​(java.lang.String key, int defaultValue)
      get the value as a int or default value
      JSONArray optJSONArray​(java.lang.String key)
      optionally get the element as a JSONArray
      JSONObject optJSONObject​(java.lang.String key)
      get the element as a JSONObject
      long optLong​(java.lang.String key)
      the value as long or NaN
      long optLong​(java.lang.String key, long defaultValue)
      get the value as a long or default value
      java.lang.Number optNumber​(java.lang.String key)
      the value as int or 0
      java.lang.Number optNumber​(java.lang.String key, java.lang.Number defaultValue)
      get the value as a Number or default value
      java.lang.String optString​(java.lang.String key)
      get a element property as a string
      java.lang.String optString​(java.lang.String key, java.lang.String defaultValue)
      get a element property as a string
      JSONObject put​(java.lang.String key, boolean value)
      put a boolean at a particular key
      JSONObject put​(java.lang.String key, double value)
      put a double at a particular key
      JSONObject put​(java.lang.String key, float value)
      put a float at a particular key
      JSONObject put​(java.lang.String key, int value)
      put a int at a particular key
      JSONObject put​(java.lang.String key, long value)
      put a long at a particular key
      JSONObject put​(java.lang.String key, java.lang.Number value)
      put a Number at a particular key
      JSONObject put​(java.lang.String key, java.lang.Object value)
      put an object to a key.
      JSONObject put​(java.lang.String key, java.lang.String value)
      put a String at a particular key
      JSONObject put​(java.lang.String key, java.util.Collection value)
      put a Collection as a JSONArray at a particular key
      JSONObject put​(java.lang.String key, java.util.Map value)
      put a Collection as a JSONArray at a particular key
      JSONObject put​(java.lang.String key, JSONArray array)
      put a JSONArray at a particular key
      JSONObject put​(java.lang.String key, JSONObject object)
      put a JSONObject at a particular key
      <T extends java.lang.Enum<T>>
      JSONObject
      put​(java.lang.String key, T enumvalue)
      put a enum at a particular key.
      JSONObject putOnce​(java.lang.String key, java.lang.Object value)
      put a value to a key only if it does not exist
      JSONObject putOpt​(java.lang.String key, java.lang.Object value)
      optional put a value at a key as long as both they key and value are not null otherwise it does nothing
      static java.lang.String quote​(java.lang.String s)
      quite escape a string
      static java.io.Writer quote​(java.lang.String s, java.io.Writer writer)
      quite escape a string
      java.lang.Object remove​(java.lang.String key)
      remove a element by key name
      boolean similar​(java.lang.Object o)
      indicates if a JSONObject has the same elements as another JSONObject
      static java.lang.Object stringToValue​(java.lang.String str)
      convert a primitive JSON type in a string (bool, number, null) to its primitive type all decimal types will become doubles
      JSONArray toJSONArray​(JSONArray names)
      creates an array of the values for they keys you provide
      java.util.Map<java.lang.String,​java.lang.Object> toMap()
      converts this object to a map
      java.lang.String toString()  
      java.lang.String toString​(int i)
      render the object as a JSON String
      private <T extends java.lang.Number>
      T
      tryNumber​(java.util.function.Supplier<T> supplier, java.lang.String key)  
      static java.lang.String valueToString​(java.lang.Object o)
      Converts an object to a JSON String
      static java.lang.Object wrap​(java.lang.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
      private static JSONArray wrapArray​(java.lang.Object obj)  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NULL

        public static final java.lang.Object NULL
    • Constructor Detail

      • JSONObject

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

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

        public JSONObject​(java.lang.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
    • Method Detail

      • quote

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

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

        public static java.lang.Object stringToValue​(java.lang.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 java.lang.Object wrap​(java.lang.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​(java.lang.Object obj)
      • doubleToString

        public static java.lang.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 java.lang.String numberToString​(java.lang.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 java.lang.String valueToString​(java.lang.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 java.lang.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 java.lang.String[] getNames​(java.lang.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
      • toString

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

        public java.lang.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​(java.lang.Object o)
        indicates if a JSONObject has the same elements as another JSONObject
        Parameters:
        o - another object
        Returns:
        a bool
      • has

        public boolean has​(java.lang.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 java.lang.Object get​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.String getString​(java.lang.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 java.lang.String optString​(java.lang.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 java.lang.String optString​(java.lang.String key,
                                          java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.Number getNumber​(java.lang.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 java.lang.Number optNumber​(java.lang.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 java.lang.Number optNumber​(java.lang.String key,
                                          java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.math.BigInteger getBigInteger​(java.lang.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 java.math.BigInteger optBigInteger​(java.lang.String key,
                                                  java.math.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 java.math.BigDecimal getBigDecimal​(java.lang.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 java.math.BigDecimal optBigDecimal​(java.lang.String key,
                                                  java.math.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​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.Enum<T>> T getEnum​(java.lang.Class<T> enumClass,
                                                       java.lang.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 java.lang.Enum<T>> T optEnum​(java.lang.Class<T> enumClass,
                                                       java.lang.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 java.lang.Enum<T>> T optEnum​(java.lang.Class<T> enumClass,
                                                       java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String key,
                              java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String key,
                              java.lang.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​(java.lang.String key,
                              java.util.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​(java.lang.String key,
                              java.util.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 java.lang.Enum<T>> JSONObject put​(java.lang.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 java.lang.Object remove​(java.lang.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​(java.lang.String key,
                                     java.lang.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​(java.lang.String key,
                                 java.lang.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​(java.lang.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​(java.lang.String key,
                                  java.lang.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​(java.lang.String key,
                              java.lang.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​(java.lang.String key,
                                 java.lang.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 java.util.Set<java.lang.String> keySet()
        get all the keys as a set
        Returns:
        a set of keys
      • keys

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

        public java.util.Map<java.lang.String,​java.lang.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
      • tryNumber

        private <T extends java.lang.Number> T tryNumber​(java.util.function.Supplier<T> supplier,
                                                         java.lang.String key)
      • getOrDefault

        private <T> T getOrDefault​(java.util.function.Supplier<T> supplier,
                                   T defaultValue)
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public java.lang.Object opt​(java.lang.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​(java.lang.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​(java.lang.Object o)