Package rife.json

Class JsonObject

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

public class JsonObject extends LinkedHashMap<String,Object>
A JsonObject represents a JSON object as a regular ordered map, with additional fluent construction methods and typed value retrieval.

The values that are stored are plain Java instances: String, Long, Double, Boolean, null, JsonObject and JsonArray.

All the mutation methods convert maps, collections and arrays to JsonObject and JsonArray instances, while values that are assigned through map entries bypass this conversion.

The typed retrieval methods are lenient. Strings will be parsed into the requested numeric or boolean types and numbers will be truncated when narrower types are requested. Members that are absent or that are null return the provided default value or a zero-like value.

For instance:

 var json = new JsonObject()
     .set("name", "my-app")
     .set("version", "1.0.0")
     .object("dependencies", d -> d
         .set("com.uwyn.rife2:rife2", "1.9.1"))
     .array("authors", a -> a
         .append("gbevin"));
 
Since:
1.10
See Also:
  • Constructor Details

    • JsonObject

      public JsonObject()
      Creates an empty JSON object.

      Members can afterwards be added with the regular map methods or with the fluent construction methods.

      Since:
      1.10
      See Also:
    • JsonObject

      public JsonObject(Map<String,?> members)
      Creates a JSON object with the members of a map.

      This constructor will copy over the entries of the provided map in the map's iteration order, while nested maps, collections and arrays are converted to JsonObject and JsonArray instances.

      Parameters:
      members - the map whose members become the members of this JSON object, in the map's iteration order
      Since:
      1.10
      See Also:
  • Method Details

    • parse

      public static JsonObject parse(String json)
      Parses a JSON document that is expected to be a JSON object.

      This method does the same as Json.parseObject(String) and additionally enables RIFE2's standard conversions to convert strings to JsonObject instances.

      Parameters:
      json - the JSON document to parse
      Returns:
      the parsed JsonObject
      Throws:
      JsonParseException - when the document couldn't be parsed or isn't a JSON object
      Since:
      1.10
      See Also:
    • set

      public JsonObject set(String name, Object value)
      Sets a member of this JSON object.

      Maps, collections and arrays are converted to JsonObject and JsonArray instances so that the typed retrieval methods work on them too.

      Parameters:
      name - the name of the member
      value - the value of the member
      Returns:
      this JsonObject instance
      Since:
      1.10
      See Also:
    • put

      public Object put(String name, Object value)
      Specified by:
      put in interface Map<String,Object>
      Overrides:
      put in class HashMap<String,Object>
    • putAll

      public void putAll(Map<? extends String,?> members)
      Specified by:
      putAll in interface Map<String,Object>
      Overrides:
      putAll in class HashMap<String,Object>
    • putIfAbsent

      public Object putIfAbsent(String name, Object value)
      Specified by:
      putIfAbsent in interface Map<String,Object>
      Overrides:
      putIfAbsent in class HashMap<String,Object>
    • replace

      public Object replace(String name, Object value)
      Specified by:
      replace in interface Map<String,Object>
      Overrides:
      replace in class HashMap<String,Object>
    • replace

      public boolean replace(String name, Object oldValue, Object newValue)
      Specified by:
      replace in interface Map<String,Object>
      Overrides:
      replace in class HashMap<String,Object>
    • replaceAll

      public void replaceAll(BiFunction<? super String,? super Object,?> function)
      Specified by:
      replaceAll in interface Map<String,Object>
      Overrides:
      replaceAll in class LinkedHashMap<String,Object>
    • merge

      public Object merge(String name, Object value, BiFunction<? super Object,? super Object,?> remappingFunction)
      Specified by:
      merge in interface Map<String,Object>
      Overrides:
      merge in class HashMap<String,Object>
    • compute

      public Object compute(String name, BiFunction<? super String,? super Object,?> remappingFunction)
      Specified by:
      compute in interface Map<String,Object>
      Overrides:
      compute in class HashMap<String,Object>
    • computeIfAbsent

      public Object computeIfAbsent(String name, Function<? super String,?> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<String,Object>
      Overrides:
      computeIfAbsent in class HashMap<String,Object>
    • computeIfPresent

      public Object computeIfPresent(String name, BiFunction<? super String,? super Object,?> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<String,Object>
      Overrides:
      computeIfPresent in class HashMap<String,Object>
    • object

      public JsonObject object(String name, JsonObjectAction action)
      Sets a member of this JSON object to a new nested JSON object that is constructed by the provided action.

      This method will create the nested JsonObject for you and hand it to the action so that you can populate it inline, after which it is set as the member with the provided name.

      Parameters:
      name - the name of the member
      action - the action that constructs the nested object
      Returns:
      this JsonObject instance
      Since:
      1.10
      See Also:
    • array

      public JsonObject array(String name, JsonArrayAction action)
      Sets a member of this JSON object to a new nested JSON array that is constructed by the provided action.

      This method will create the nested JsonArray for you and hand it to the action so that you can populate it inline, after which it is set as the member with the provided name.

      Parameters:
      name - the name of the member
      action - the action that constructs the nested array
      Returns:
      this JsonObject instance
      Since:
      1.10
      See Also:
    • getString

      public String getString(String name)
      Retrieves a member as a string.
      Parameters:
      name - the name of the member
      Returns:
      the member value as a string; or

      null if the member is absent or null

      Since:
      1.10
      See Also:
    • getString

      public String getString(String name, String defaultValue)
      Retrieves a member as a string, falling back to a default value in case the member isn't there.
      Parameters:
      name - the name of the member
      defaultValue - the value to return when the member is absent or null
      Returns:
      the member value as a string; or

      the default value if the member is absent or null

      Since:
      1.10
      See Also:
    • getInt

      public int getInt(String name)
      Retrieves a member as an int.

      Strings will be parsed and wider numbers will be truncated so that they fit into an int.

      Parameters:
      name - the name of the member
      Returns:
      the member value as an int; or

      0 if the member is absent or null

      Throws:
      NumberFormatException - when the value is a string that can't be parsed into this type
      Since:
      1.10
      See Also:
    • getInt

      public int getInt(String name, int defaultValue)
      Retrieves a member as an int, falling back to a default value in case the member isn't there.
      Parameters:
      name - the name of the member
      defaultValue - the value to return when the member is absent or null
      Returns:
      the member value as an int; or

      the default value if the member is absent or null

      Since:
      1.10
      See Also:
    • getLong

      public long getLong(String name)
      Retrieves a member as a long.

      Strings will be parsed and floating point numbers will be truncated so that they fit into a long.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a long; or

      0L if the member is absent or null

      Throws:
      NumberFormatException - when the value is a string that can't be parsed into this type
      Since:
      1.10
      See Also:
    • getLong

      public long getLong(String name, long defaultValue)
      Retrieves a member as a long, falling back to a default value in case the member isn't there.
      Parameters:
      name - the name of the member
      defaultValue - the value to return when the member is absent or null
      Returns:
      the member value as a long; or

      the default value if the member is absent or null

      Since:
      1.10
      See Also:
    • getDouble

      public double getDouble(String name)
      Retrieves a member as a double.

      Strings will be parsed so that they can be used as a double too.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a double; or

      0.0 if the member is absent or null

      Throws:
      NumberFormatException - when the value is a string that can't be parsed into this type
      Since:
      1.10
      See Also:
    • getDouble

      public double getDouble(String name, double defaultValue)
      Retrieves a member as a double, falling back to a default value in case the member isn't there.
      Parameters:
      name - the name of the member
      defaultValue - the value to return when the member is absent or null
      Returns:
      the member value as a double; or

      the default value if the member is absent or null

      Since:
      1.10
      See Also:
    • getBoolean

      public boolean getBoolean(String name)
      Retrieves a member as a boolean.

      Strings will be parsed so that they can be used as a boolean too.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a boolean; or

      false if the member is absent or null

      Since:
      1.10
      See Also:
    • getBoolean

      public boolean getBoolean(String name, boolean defaultValue)
      Retrieves a member as a boolean, falling back to a default value in case the member isn't there.
      Parameters:
      name - the name of the member
      defaultValue - the value to return when the member is absent or null
      Returns:
      the member value as a boolean; or

      the default value if the member is absent or null

      Since:
      1.10
      See Also:
    • getDate

      public Date getDate(String name)
      Retrieves a member as a date.

      ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a Date; or

      null if the member is absent or null

      Throws:
      IllegalArgumentException - when the value can't be converted into this type
      Since:
      1.10
      See Also:
    • getInstant

      public Instant getInstant(String name)
      Retrieves a member as an instant.

      ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.

      Parameters:
      name - the name of the member
      Returns:
      the member value as an Instant; or

      null if the member is absent or null

      Throws:
      IllegalArgumentException - when the value can't be converted into this type
      Since:
      1.10
      See Also:
    • getLocalDate

      public LocalDate getLocalDate(String name)
      Retrieves a member as a local date.

      ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalDate; or

      null if the member is absent or null

      Throws:
      IllegalArgumentException - when the value can't be converted into this type
      Since:
      1.10
      See Also:
    • getLocalDateTime

      public LocalDateTime getLocalDateTime(String name)
      Retrieves a member as a local date and time.

      ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalDateTime; or

      null if the member is absent or null

      Throws:
      IllegalArgumentException - when the value can't be converted into this type
      Since:
      1.10
      See Also:
    • getLocalTime

      public LocalTime getLocalTime(String name)
      Retrieves a member as a local time.

      ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalTime; or

      null if the member is absent or null

      Throws:
      IllegalArgumentException - when the value can't be converted into this type
      Since:
      1.10
      See Also:
    • getObject

      public JsonObject getObject(String name)
      Retrieves a member as a nested JSON object.
      Parameters:
      name - the name of the member
      Returns:
      the nested JsonObject; or

      null if the member is absent or null

      Throws:
      ClassCastException - when the value is of another type
      Since:
      1.10
      See Also:
    • getArray

      public JsonArray getArray(String name)
      Retrieves a member as a nested JSON array.
      Parameters:
      name - the name of the member
      Returns:
      the nested JsonArray; or

      null if the member is absent or null

      Throws:
      ClassCastException - when the value is of another type
      Since:
      1.10
      See Also:
    • toBean

      public <T> T toBean(Class<T> beanClass)
      Fills a new bean instance with the members of this JSON object.

      This method does the same as Json.toBean(JsonObject, Class), where you'll find a detailed description of how the members are matched up with the properties of the bean.

      Parameters:
      beanClass - the class of the bean to fill
      Returns:
      the filled bean instance
      Since:
      1.10
      See Also:
    • toString

      public String toString()
      Serializes this JSON object into its compact string representation.
      Overrides:
      toString in class AbstractMap<String,Object>
      Returns:
      the JSON string
      Since:
      1.10
      See Also:
    • toPrettyString

      public String toPrettyString()
      Serializes this JSON object into its multi-line indented string representation.
      Returns:
      the pretty-printed JSON string
      Since:
      1.10
      See Also:
    • print

      public void print(Writer writer) throws IOException
      Serializes this JSON object in its compact representation to a writer.

      The output is streamed so that no intermediate string has to be built up in memory.

      Parameters:
      writer - the writer to serialize to
      Throws:
      IOException - when an error occurred while writing
      Since:
      1.10
      See Also:
    • prettyPrint

      public void prettyPrint(Writer writer) throws IOException
      Serializes this JSON object in its multi-line indented representation to a writer.

      The output is streamed so that no intermediate string has to be built up in memory.

      Parameters:
      writer - the writer to serialize to
      Throws:
      IOException - when an error occurred while writing
      Since:
      1.10
      See Also: