Class JsonObject
- All Implemented Interfaces:
Serializable,Cloneable,Map<String,Object>
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty JSON object.JsonObject(Map<String, ?> members) Creates a JSON object with the members of a map. -
Method Summary
Modifier and TypeMethodDescriptionarray(String name, JsonArrayAction action) Sets a member of this JSON object to a new nested JSON array that is constructed by the provided action.compute(String name, BiFunction<? super String, ? super Object, ?> remappingFunction) computeIfAbsent(String name, Function<? super String, ?> mappingFunction) computeIfPresent(String name, BiFunction<? super String, ? super Object, ?> remappingFunction) Retrieves a member as a nested JSON array.booleangetBoolean(String name) Retrieves a member as a boolean.booleangetBoolean(String name, boolean defaultValue) Retrieves a member as a boolean.Retrieves a member as a date.doubleRetrieves a member as a double.doubleRetrieves a member as a double.getInstant(String name) Retrieves a member as an instant.intRetrieves a member as an int.intRetrieves a member as an int.getLocalDate(String name) Retrieves a member as a local date.getLocalDateTime(String name) Retrieves a member as a local date and time.getLocalTime(String name) Retrieves a member as a local time.longRetrieves a member as a long.longRetrieves a member as a long.Retrieves a member as a nested JSON object.Retrieves a member as a string.Retrieves a member as a string.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.static JsonObjectParses a JSON document that is expected to be a JSON object.voidprettyPrint(Writer writer) Serializes this JSON object in its multi-line indented representation to a writer.voidSerializes this JSON object in its compact representation to a writer.voidputIfAbsent(String name, Object value) booleanvoidreplaceAll(BiFunction<? super String, ? super Object, ?> function) Sets a member of this JSON object.<T> TFills a new bean instance with the members of this JSON object.Serializes this JSON object into its multi-line indented string representation.toString()Serializes this JSON object into its compact string representation.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, valuesMethods inherited from class java.util.AbstractMap
equals, hashCode
-
Constructor Details
-
JsonObject
public JsonObject()Creates an empty JSON object.- Since:
- 1.10
-
JsonObject
Creates a JSON object with the members of a map.Nested maps, collections and arrays are converted to
JsonObjectandJsonArrayinstances.- 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
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 toJsonObjectinstances.- 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
Sets a member of this JSON object.Maps, collections and arrays are converted to
JsonObjectandJsonArrayinstances so that the typed retrieval methods work on them.- Parameters:
name- the name of the membervalue- the value of the member- Returns:
- this
JsonObjectinstance - Since:
- 1.10
-
put
-
putAll
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<String,Object> - Overrides:
putIfAbsentin classHashMap<String,Object>
-
replace
-
replace
-
replaceAll
- Specified by:
replaceAllin interfaceMap<String,Object> - Overrides:
replaceAllin classLinkedHashMap<String,Object>
-
merge
-
compute
-
computeIfAbsent
- Specified by:
computeIfAbsentin interfaceMap<String,Object> - Overrides:
computeIfAbsentin classHashMap<String,Object>
-
computeIfPresent
public Object computeIfPresent(String name, BiFunction<? super String, ? super Object, ?> remappingFunction) - Specified by:
computeIfPresentin interfaceMap<String,Object> - Overrides:
computeIfPresentin classHashMap<String,Object>
-
object
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 memberaction- the action that constructs the nested object- Returns:
- this
JsonObjectinstance - Since:
- 1.10
-
array
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 memberaction- the action that constructs the nested array- Returns:
- this
JsonObjectinstance - Since:
- 1.10
-
getString
Retrieves a member as a string.- Parameters:
name- the name of the member- Returns:
- the member value as a string; or
nullwhen absent or null - Since:
- 1.10
-
getString
Retrieves a member as a string.- Parameters:
name- the name of the memberdefaultValue- 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
Retrieves a member as an int.- Parameters:
name- the name of the member- Returns:
- the member value as an int; or
0when absent or null - Since:
- 1.10
-
getInt
Retrieves a member as an int.- Parameters:
name- the name of the memberdefaultValue- 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
Retrieves a member as a long.- Parameters:
name- the name of the member- Returns:
- the member value as a long; or
0Lwhen absent or null - Since:
- 1.10
-
getLong
Retrieves a member as a long.- Parameters:
name- the name of the memberdefaultValue- 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
Retrieves a member as a double.- Parameters:
name- the name of the member- Returns:
- the member value as a double; or
0.0when absent or null - Since:
- 1.10
-
getDouble
Retrieves a member as a double.- Parameters:
name- the name of the memberdefaultValue- 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
Retrieves a member as a boolean.- Parameters:
name- the name of the member- Returns:
- the member value as a boolean; or
falsewhen absent or null - Since:
- 1.10
-
getBoolean
Retrieves a member as a boolean.- Parameters:
name- the name of the memberdefaultValue- 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
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; ornullwhen absent or null - Since:
- 1.10
-
getInstant
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; ornullwhen absent or null - Since:
- 1.10
-
getLocalDate
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; ornullwhen absent or null - Since:
- 1.10
-
getLocalDateTime
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; ornullwhen absent or null - Since:
- 1.10
-
getLocalTime
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; ornullwhen absent or null - Since:
- 1.10
-
getObject
Retrieves a member as a nested JSON object.- Parameters:
name- the name of the member- Returns:
- the nested
JsonObject; ornullwhen absent or null - Since:
- 1.10
-
getArray
Retrieves a member as a nested JSON array.- Parameters:
name- the name of the member- Returns:
- the nested
JsonArray; ornullwhen absent or null - Since:
- 1.10
-
toBean
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
Serializes this JSON object into its compact string representation.- Overrides:
toStringin classAbstractMap<String,Object> - Returns:
- the JSON string
- Since:
- 1.10
-
toPrettyString
Serializes this JSON object into its multi-line indented string representation.- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
-
print
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
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
-