All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>, Jsonable

public class JsonObjectextends HashMap<String,Object> implements Jsonable
JsonObject is a common non-thread safe data format for string to data mappings. The contents of a JsonObject are only validated as JSON values on serialization. Meaning all values added to a JsonObject must be recognized by the Jsoner for it to be a true 'JsonObject', so it is really a JsonableHashMap that will serialize to a JsonObject if all of its contents are valid JSON.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      The serialization version this class is compatible with. This value doesn't need to be incremented if and only if the only changes to occur were updating comments, updating javadocs, adding new fields to the class, changing the fields from static to non-static, or changing the fields from transient to non transient. All other changes require this number be incremented.
      See Also:
  • Constructor Details

    • 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 Details

    • requireKeys

      public void requireKeys(JsonKey... keys)
      Ensures the given keys are present.
      Parameters:
      keys - represents the keys that must be present.
      Throws:
      NoSuchElementException - if any of the given keys are missing.
    • getBigDecimal

      public BigDecimal getBigDecimal(JsonKey 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 paired with.
      Returns:
      a BigDecimal representing the value paired with 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:
    • getBigDecimalOrDefault

      public BigDecimal getBigDecimalOrDefault(JsonKey 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 paired with.
      Returns:
      a BigDecimal representing the value paired with the key or JsonKey#getValue() if the key isn't present.
      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:
    • getBoolean

      public Boolean getBoolean(JsonKey key)
      A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is /** 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 paired with.
      Returns:
      a Boolean representing the value paired with the key.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getBooleanOrDefault

      public Boolean getBooleanOrDefault(JsonKey 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 paired with.
      Returns:
      a Boolean representing the value paired with the key or JsonKey#getValue() if the key isn't present.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getByte

      public Byte getByte(JsonKey key)
      A convenience method that assumes there is a Boolean or String value at the given 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 paired with.
      Returns:
      a Byte representing the value paired with 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:
    • getByteOrDefault

      public Byte getByteOrDefault(JsonKey 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 paired with.
      Returns:
      a Byte representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getCollection

      public <T extends Collection<?>> T getCollection(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given 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 paired with.
      Returns:
      a Collection representing the value paired with the key.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getCollectionOrDefault

      public <T extends Collection<?>> T getCollectionOrDefault(JsonKey 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 paired with.
      Returns:
      a Collection representing the value paired with the key or JsonKey#getValue() if the key isn't present..
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getDouble

      public Double getDouble(JsonKey key)
      A convenience method that assumes there is a Collection at the given 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 paired with.
      Returns:
      a Double representing the value paired with 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:
    • getDoubleOrDefault

      public Double getDoubleOrDefault(JsonKey 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 paired with.
      Returns:
      a Double representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getFloat

      public Float getFloat(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given key. private static boolean isValidClassName(String className) { if (className == null || className.isEmpty()) { return false; } if (className.contains("..") || className.contains("/") || className.contains("\\")) { return false; } return className.matches("^[a-zA-Z_][a-zA-Z0-9_$.]*$"); } /** 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 paired with.
      Returns:
      a Float representing the value paired with 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:
    • getFloatOrDefault

      public Float getFloatOrDefault(JsonKey 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 paired with.
      Returns:
      a Float representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getInteger

      public Integer getInteger(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given 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 paired with.
      Returns:
      an Integer representing the value paired with 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:
    • getIntegerOrDefault

      public Integer getIntegerOrDefault(JsonKey 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 paired with.
      Returns:
      an Integer representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getLong

      public Long getLong(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given 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 paired with.
      Returns:
      a Long representing the value paired with 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:
    • getLongOrDefault

      public Long getLongOrDefault(JsonKey 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 paired with.
      Returns:
      a Long representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getMap

      public <T extends Map<?,?>> T getMap(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given 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 paired with.
      Returns:
      a Map representing the value paired with the key.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getMapOrDefault

      public <T extends Map<?,?>> T getMapOrDefault(JsonKey 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 paired with.
      Returns:
      a Map representing the value paired with the key or JsonKey#getValue() if the key isn't present.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getShort

      public Short getShort(JsonKey key)
      A convenience method that assumes there is a Map at the given 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 paired with.
      Returns:
      a Short representing the value paired with 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:
    • getShortOrDefault

      public Short getShortOrDefault(JsonKey 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 paired with.
      Returns:
      a Short representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
    • getString

      public String getString(JsonKey key)
      A convenience method that assumes there is a Number or String value at the given 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 paired with.
      Returns:
      a String representing the value paired with the key.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getStringOrDefault

      public String getStringOrDefault(JsonKey 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 paired with.
      Returns:
      a String representing the value paired with the key or JsonKey#getValue() if the key isn't present.
      Throws:
      ClassCastException - if the value didn't match the assumed return type.
      See Also:
    • getStringOrDefault

      private 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.