Class JsonObject
- All Implemented Interfaces:
Serializable,Cloneable,Map<String,Object>
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:
-
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, falling back to a default value in case the member isn't there.Retrieves a member as a date.doubleRetrieves a member as a double.doubleRetrieves a member as a double, falling back to a default value in case the member isn't there.getInstant(String name) Retrieves a member as an instant.intRetrieves a member as an int.intRetrieves a member as an int, falling back to a default value in case the member isn't there.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, falling back to a default value in case the member isn't there.Retrieves a member as a nested JSON object.Retrieves a member as a string.Retrieves a member as a string, falling back to a default value in case the member isn't there.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.Members can afterwards be added with the regular map methods or with the fluent construction methods.
- Since:
- 1.10
- See Also:
-
JsonObject
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
JsonObjectandJsonArrayinstances.- 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
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 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
- See Also:
-
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 too.- Parameters:
name- the name of the membervalue- the value of the member- Returns:
- this
JsonObjectinstance - Since:
- 1.10
- See Also:
-
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.This method will create the nested
JsonObjectfor 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 memberaction- the action that constructs the nested object- Returns:
- this
JsonObjectinstance - Since:
- 1.10
- See Also:
-
array
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
JsonArrayfor 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 memberaction- the action that constructs the nested array- Returns:
- this
JsonObjectinstance - Since:
- 1.10
- See Also:
-
getString
Retrieves a member as a string.- Parameters:
name- the name of the member- Returns:
- the member value as a string; or
nullif the member is absent or null - Since:
- 1.10
- See Also:
-
getString
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 memberdefaultValue- 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
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
0if 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
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 memberdefaultValue- 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
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
0Lif 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
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 memberdefaultValue- 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
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.0if 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
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 memberdefaultValue- 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
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
falseif the member is absent or null - Since:
- 1.10
- See Also:
-
getBoolean
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 memberdefaultValue- 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
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; ornullif the member is absent or null - Throws:
IllegalArgumentException- when the value can't be converted into this type- Since:
- 1.10
- See Also:
-
getInstant
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; ornullif the member is absent or null - Throws:
IllegalArgumentException- when the value can't be converted into this type- Since:
- 1.10
- See Also:
-
getLocalDate
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; ornullif the member is absent or null - Throws:
IllegalArgumentException- when the value can't be converted into this type- Since:
- 1.10
- See Also:
-
getLocalDateTime
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; ornullif the member is absent or null - Throws:
IllegalArgumentException- when the value can't be converted into this type- Since:
- 1.10
- See Also:
-
getLocalTime
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; ornullif the member is absent or null - Throws:
IllegalArgumentException- when the value can't be converted into this type- Since:
- 1.10
- See Also:
-
getObject
Retrieves a member as a nested JSON object.- Parameters:
name- the name of the member- Returns:
- the nested
JsonObject; ornullif the member is absent or null - Throws:
ClassCastException- when the value is of another type- Since:
- 1.10
- See Also:
-
getArray
Retrieves a member as a nested JSON array.- Parameters:
name- the name of the member- Returns:
- the nested
JsonArray; ornullif the member is absent or null - Throws:
ClassCastException- when the value is of another type- Since:
- 1.10
- See Also:
-
toBean
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
Serializes this JSON object into its compact string representation.- Overrides:
toStringin classAbstractMap<String,Object> - Returns:
- the JSON string
- Since:
- 1.10
- See Also:
-
toPrettyString
Serializes this JSON object into its multi-line indented string representation.- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
- See Also:
-
print
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
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:
-