Package rife.json

Class JsonObject

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

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

Values 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, values that are assigned through map entries bypass this conversion.

The typed retrieval methods are lenient: strings are parsed into the requested numeric or boolean types, numbers are truncated when narrower types are requested, and absent or null members return the provided default 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.
      Since:
      1.10
    • JsonObject

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

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

    • parse

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

      This is equivalent to Json.parseObject(String) and also 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
    • 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.

      Parameters:
      name - the name of the member
      value - the value of the member
      Returns:
      this JsonObject instance
      Since:
      1.10
    • 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.
      Parameters:
      name - the name of the member
      action - the action that constructs the nested object
      Returns:
      this JsonObject instance
      Since:
      1.10
    • 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.
      Parameters:
      name - the name of the member
      action - the action that constructs the nested array
      Returns:
      this JsonObject instance
      Since:
      1.10
    • 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 when absent or null
      Since:
      1.10
    • getString

      public String getString(String name, String defaultValue)
      Retrieves a member as a string.
      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
      Since:
      1.10
    • getInt

      public int getInt(String name)
      Retrieves a member as an int.
      Parameters:
      name - the name of the member
      Returns:
      the member value as an int; or 0 when absent or null
      Since:
      1.10
    • getInt

      public int getInt(String name, int defaultValue)
      Retrieves a member as an int.
      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
      Since:
      1.10
    • getLong

      public long getLong(String name)
      Retrieves a member as a long.
      Parameters:
      name - the name of the member
      Returns:
      the member value as a long; or 0L when absent or null
      Since:
      1.10
    • getLong

      public long getLong(String name, long defaultValue)
      Retrieves a member as a long.
      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
      Since:
      1.10
    • getDouble

      public double getDouble(String name)
      Retrieves a member as a double.
      Parameters:
      name - the name of the member
      Returns:
      the member value as a double; or 0.0 when absent or null
      Since:
      1.10
    • getDouble

      public double getDouble(String name, double defaultValue)
      Retrieves a member as a double.
      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
      Since:
      1.10
    • getBoolean

      public boolean getBoolean(String name)
      Retrieves a member as a boolean.
      Parameters:
      name - the name of the member
      Returns:
      the member value as a boolean; or false when absent or null
      Since:
      1.10
    • getBoolean

      public boolean getBoolean(String name, boolean defaultValue)
      Retrieves a member as a boolean.
      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
      Since:
      1.10
    • getDate

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

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

      Parameters:
      name - the name of the member
      Returns:
      the member value as a Date; or null when absent or null
      Since:
      1.10
    • getInstant

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

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

      Parameters:
      name - the name of the member
      Returns:
      the member value as an Instant; or null when absent or null
      Since:
      1.10
    • getLocalDate

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

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

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalDate; or null when absent or null
      Since:
      1.10
    • getLocalDateTime

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

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

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalDateTime; or null when absent or null
      Since:
      1.10
    • getLocalTime

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

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

      Parameters:
      name - the name of the member
      Returns:
      the member value as a LocalTime; or null when absent or null
      Since:
      1.10
    • 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 when absent or null
      Since:
      1.10
    • 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 when absent or null
      Since:
      1.10
    • toBean

      public <T> T toBean(Class<T> beanClass)
      Fills a new bean instance with the members of this JSON object.
      Parameters:
      beanClass - the class of the bean to fill
      Returns:
      the filled bean instance
      Since:
      1.10
    • 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
    • toPrettyString

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

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

      The output is streamed, no intermediate string is built.

      Parameters:
      writer - the writer to serialize to
      Throws:
      IOException - when an error occurred while writing
      Since:
      1.10
    • 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, no intermediate string is built.

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