Interface JSONObject
- All Superinterfaces:
Formattable, Iterable<JSONValue>, JSONValue
- All Known Implementing Classes:
JSONObjectImpl
The definition of a JSON object.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: JSONObject.java 1258 2026-06-04 18:33:06Z tquadrat $
- Since:
- 0.25.0
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.jsonbuilder.JSONObject"
-
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if a specified member is present as a child of this object.Returns the value of the member with the specified name in this object.default JSONArrayConvenience method that returns the value with the specified name as aJSONArray.default BigDecimalgetBigDecimal(String name, BigDecimal defaultValue) Convenience method that returns the value with the specified name as aBigDecimal.default BigIntegergetBigInteger(String name, BigInteger defaultValue) Convenience method that returns the value with the specified name as aBigInteger.default booleangetBoolean(String name, boolean defaultValue) Convenience method that returns the value with the specified name as aboolean.default doubleConvenience method that returns the value with the specified name as adouble.default floatConvenience method that returns the value with the specified name as afloat.default intConvenience method that returns the value with the specified name as aint.default longConvenience method that returns the value with the specified name as along.default JSONObjectgetObject(String name, JSONObject defaultValue) Convenience method that returns the value with the specified name as aJSONObject.default StringConvenience method that returns the value with the specified name as aString.booleanisEmpty()Checks whether this object has members.merge(JSONObject object) Copies all members of the specified object into this object.names()Returns the names of the members of this object in document order.Removes a member with the specified name from this object.default JSONObjectSets the value of the member with the specified name to the JSON representation of the specifiedbooleanvalue.Sets the value of the member with the specified name to the JSON representation of the specifieddoublevalue.Sets the value of the member with the specified name to the JSON representation of the specifiedfloatvalue.Sets the value of the member with the specified name to the JSON representation of the specifiedintvalue.Sets the value of the member with the specified name to the JSON representation of the specifiedlongvalue.Sets the value of the member with the specified name to the JSON representation of the specifiedDoublevalue.Sets the value of the member with the specified name to the JSON representation of the specifiedFloatvalue.Sets the value of the member with the specified name to the JSON representation of the specifiedIntegervalue.Sets the value of the member with the specified name to the JSON representation of the specifiedLongvalue.Sets the value of the member with the specified name to the JSON representation of the specifiedStringvalue.set(String name, BigDecimal value) Sets the value of the member with the specified name to the JSON representation of the specifiedBigDecimalvalue.set(String name, BigInteger value) Sets the value of the member with the specified name to the JSON representation of the specifiedBigIntegervalue.Sets the value of the member with the specified name to the specifiedJSONValuevalue.<T extends Dimension>
JSONObjectset(String name, DimensionedValue<T> value, T targetUnit) Sets the value of the member with the specified name to the JSON representation of the specifiedDimensionedValuevalue.Sets a new empty instance ofJSONArrayto this object and returns that.Sets a new empty instance ofJSONObjectto this object and returns that.intsize()Returns the number of members (name/value pairs) in this object.Methods inherited from interface Iterable
forEach, iterator, spliterator
-
Method Details
-
contains
Checks if a specified member is present as a child of this object. This will not test if that child is the JSON literal
null, This needs to be tested separately.- Parameters:
name- The name of the member to check for.- Returns:
trueif there is a member with the given name,falseif not.
-
get
-
getArray
Convenience method that returns the value with the specified name as a
JSONArray.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a JSON Array.
-
getBigDecimal
default BigDecimal getBigDecimal(String name, BigDecimal defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
BigDecimal.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validBigDecimal.
-
getBigInteger
default BigInteger getBigInteger(String name, BigInteger defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
BigInteger.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validBigInteger.
-
getBoolean
Convenience method that returns the value with the specified name as a
boolean.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a boolean.
-
getDouble
default double getDouble(String name, double defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
double.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validdouble.
-
getFloat
default float getFloat(String name, float defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
float.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validfloat.
-
getInt
default int getInt(String name, int defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
int.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validint.
-
getLong
default long getLong(String name, long defaultValue) throws IllegalStateException, NumberFormatException Convenience method that returns the value with the specified name as a
long.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a number.NumberFormatException- The member exists, and it is a number, but cannot be parsed to a validlong.
-
getObject
Convenience method that returns the value with the specified name as a
JSONObject.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not a JSON Object.
-
getString
Convenience method that returns the value with the specified name as a
String.- Parameters:
name- The name of the member whose value is to be returned.defaultValue- The value to be returned if the requested member is missing; can benull.- Returns:
- The value of the member with the specified name, or the given default value if this object does not contain a member with that name.
- Throws:
IllegalStateException- The member exists, but it is not aString.
-
isEmpty
boolean isEmpty()Checks whether this object has members.- Returns:
trueif the object does not have any members,falseotherwise.
-
merge
Copies all members of the specified object into this object. When the specified object contains members with names that also exist in this object, the existing values in this object will be replaced by the corresponding values in the specified object.
- Parameters:
object- The object to merge.- Returns:
- This object itself, to enable method chaining.
-
names
Returns the names of the members of this object in document order.
The return value is backed by this object and will reflect subsequent changes. It cannot be used to modify this object. Attempts to modify the returned data structure will result in an exception.
- Returns:
- The names.
-
remove
Removes a member with the specified name from this object. If this object does not contain a member with the specified name, the object is not modified.
- Parameters:
name- The name of the member to remove.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
BigDecimalvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
BigIntegervalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
booleanvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
DimensionedValuevalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Type Parameters:
T- The type of the dimension for the value.- Parameters:
name- The name of the member to replace or to add.value- The value.targetUnit- The dimension for the output.- Returns:
- This object itself, to enable method chaining.
- See Also:
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
doublevalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
Doublevalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
floatvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
Floatvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
intvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
Integervalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the specified
JSONValuevalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
longvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
Longvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
set
Sets the value of the member with the specified name to the JSON representation of the specified
Stringvalue.If this object does not contain a member with this name, a new member is added at the end of the object.
- Parameters:
name- The name of the member to replace or to add.value- The value to set to the member.- Returns:
- This object itself, to enable method chaining.
-
setArray
-
setObject
Sets a new empty instance of
JSONObjectto this object and returns that.- Parameters:
name- The name of the member.- Returns:
- The freshly created
JSONObject.
-
size
int size()Returns the number of members (name/value pairs) in this object.
- Returns:
- The number of members in this object.
-
