Class JsonObject

    • Constructor Detail

      • JsonObject

        public JsonObject()
        Instantiates an empty JsonObject.
      • JsonObject

        public JsonObject​(Map<String,​?> map)
        Instantiate a new JsonObject by accepting a map's entries, which could lead to de/serialization issues of the resulting JsonObject since the entry values aren't validated as JSON values.
        Parameters:
        map - represents the mappings to produce the JsonObject with.
    • Method Detail

      • getBigDecimal

        public BigDecimal getBigDecimal​(String key)
        A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is there its Number#toString() is used to construct a new BigDecimal(String). If a String is there it is used to construct a new BigDecimal(String).
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key.
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        BigDecimal, Object.toString()
      • getBigDecimalOrDefault

        public BigDecimal getBigDecimalOrDefault​(String key,
                                                 BigDecimal defaultValue)
        A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is there its Number#toString() is used to construct a new BigDecimal(String). If a String is there it is used to construct a new BigDecimal(String).
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return types.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        BigDecimal, Object.toString()
      • getBoolean

        public Boolean getBoolean​(String key)
        A convenience method that assumes there is a Boolean or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key.
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
      • getBooleanOrDefault

        public Boolean getBooleanOrDefault​(String key,
                                           boolean defaultValue)
        A convenience method that assumes there is a Boolean or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
      • getByte

        public Byte getByte​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.byteValue()
      • getByteOrDefault

        public Byte getByteOrDefault​(String key,
                                     byte defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.byteValue()
      • getCollection

        public <T extends Collection<?>> T getCollection​(String key)
        A convenience method that assumes there is a Collection at the given key.
        Type Parameters:
        T - the kind of collection to expect at the key. Note unless manually added, collection values will be a JsonArray.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key.
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
      • getCollectionOrDefault

        public <T extends Collection<?>> T getCollectionOrDefault​(String key,
                                                                  T defaultValue)
        A convenience method that assumes there is a Collection at the given key.
        Type Parameters:
        T - the kind of collection to expect at the key. Note unless manually added, collection values will be a JsonArray.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
      • getDouble

        public Double getDouble​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.doubleValue()
      • getDoubleOrDefault

        public Double getDoubleOrDefault​(String key,
                                         double defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.doubleValue()
      • getEnum

        public <T extends Enum<T>> T getEnum​(String key)
                                      throws ClassNotFoundException
        A convenience method that assumes there is a String value at the given key representing a fully qualified name in dot notation of an enum.
        Type Parameters:
        T - the Enum type the value at the key is expected to belong to.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the enum based on the string found at the key, or null if the value paired with the provided key is null.
        Throws:
        ClassNotFoundException - if the value was a String but the declaring enum type couldn't be determined with it.
        ClassCastException - if the element at the index was not a String or if the fully qualified enum name is of the wrong type.
        IllegalArgumentException - if an enum type was determined but it doesn't define an enum with the determined name.
        See Also:
        Enum.valueOf(Class, String)
      • getEnumOrDefault

        public <T extends Enum<T>> T getEnumOrDefault​(String key,
                                                      T defaultValue)
                                               throws ClassNotFoundException
        A convenience method that assumes there is a String value at the given key representing a fully qualified name in dot notation of an enum.
        Type Parameters:
        T - the Enum type the value at the key is expected to belong to.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the enum based on the string found at the key, or the defaultValue provided if the key doesn't exist, or null if the value paired with provided key is null.
        Throws:
        ClassNotFoundException - if the value was a String but the declaring enum type couldn't be determined with it.
        ClassCastException - if the element at the index was not a String or if the fully qualified enum name is of the wrong type.
        IllegalArgumentException - if an enum type was determined but it doesn't define an enum with the determined name.
        See Also:
        Enum.valueOf(Class, String)
      • getFloat

        public Float getFloat​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.floatValue()
      • getFloatOrDefault

        public Float getFloatOrDefault​(String key,
                                       float defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.floatValue()
      • getInteger

        public Integer getInteger​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.intValue()
      • getIntegerOrDefault

        public Integer getIntegerOrDefault​(String key,
                                           int defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.intValue()
      • getLong

        public Long getLong​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.longValue()
      • getLongOrDefault

        public Long getLongOrDefault​(String key,
                                     long defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.longValue()
      • getMap

        public <T extends Map<?,​?>> T getMap​(String key)
        A convenience method that assumes there is a Map at the given key.
        Type Parameters:
        T - the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key.
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
      • getMapOrDefault

        public <T extends Map<?,​?>> T getMapOrDefault​(String key,
                                                            T defaultValue)
        A convenience method that assumes there is a Map at the given key.
        Type Parameters:
        T - the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
      • getShort

        public Short getShort​(String key)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key (which may involve rounding or truncation).
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.shortValue()
      • getShortOrDefault

        public Short getShortOrDefault​(String key,
                                       short defaultValue)
        A convenience method that assumes there is a Number or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
        NumberFormatException - if a String isn't a valid representation of a BigDecimal or if the Number represents the double or float Infinity or NaN.
        See Also:
        Number.shortValue()
      • getString

        public String getString​(String key)
        A convenience method that assumes there is a Boolean, Number, or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        Returns:
        the value stored at the key.
        Throws:
        ClassCastException - if the value didn't match the assumed return type.
      • getStringOrDefault

        public String getStringOrDefault​(String key,
                                         String defaultValue)
        A convenience method that assumes there is a Boolean, Number, or String value at the given key.
        Parameters:
        key - representing where the value ought to be stored at.
        defaultValue - representing what is returned when the key isn't in the JsonObject.
        Returns:
        the value stored at the key or the default provided if the key doesn't exist.
        Throws:
        ClassCastException - if there was a value but didn't match the assumed return type.
      • toJson

        public String toJson()
        Description copied from interface: Jsonable
        Serialize to a JSON formatted string.
        Specified by:
        toJson in interface Jsonable
        Returns:
        a string, formatted in JSON, that represents the Jsonable.
      • toJson

        public void toJson​(Writer writable)
                    throws IOException
        Description copied from interface: Jsonable
        Serialize to a JSON formatted stream.
        Specified by:
        toJson in interface Jsonable
        Parameters:
        writable - where the resulting JSON text should be sent.
        Throws:
        IOException - when the writable encounters an I/O error.