- java.lang.Object
-
- io.github.jopenlibs.vault.json.JsonValue
-
- io.github.jopenlibs.vault.json.JsonObject
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<JsonObject.Member>
public class JsonObject extends JsonValue implements java.lang.Iterable<JsonObject.Member>
Represents a JSON object, a set of name/value pairs, where the names are strings and the values are JSON values.Members can be added using the
add(String, ...)
methods which accept instances ofJsonValue
, strings, primitive numbers, and boolean values. To modify certain values of an object, use theset(String, ...)
methods. Please note that theadd
methods are faster thanset
as they do not search for existing members. On the other hand, theadd
methods do not prevent adding multiple members with the same name. Duplicate names are discouraged but not prohibited by JSON.Members can be accessed by their name using
get(String)
. A list of all names can be obtained from the methodnames()
. This class also supports iterating over the members in document order using aniterator()
or an enhanced for loop:for (Member member : jsonObject) { String name = member.getName(); JsonValue value = member.getValue(); ... }
Even though JSON objects are unordered by definition, instances of this class preserve the order of members to allow processing in document order and to guarantee a predictable output.
Note that this class is not thread-safe. If multiple threads access a
JsonObject
instance concurrently, while at least one of these threads modifies the contents of this object, access to the instance must be synchronized externally. Failure to do so may lead to an inconsistent state.This class is not supposed to be extended by clients.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsonObject.Member
Represents a member of a JSON object, a pair of a name and a value.
-
Constructor Summary
Constructors Constructor Description JsonObject()
Creates a new empty JsonObject.JsonObject(JsonObject object)
Creates a new JsonObject, initialized with the contents of the specified JSON object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonObject
add(java.lang.String name, boolean value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedboolean
value.JsonObject
add(java.lang.String name, double value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifieddouble
value.JsonObject
add(java.lang.String name, float value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedfloat
value.JsonObject
add(java.lang.String name, int value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedint
value.JsonObject
add(java.lang.String name, int[] value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedint[]
value.JsonObject
add(java.lang.String name, long value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedlong
value.JsonObject
add(java.lang.String name, JsonValue value)
Appends a new member to the end of this object, with the specified name and the specified JSON value.JsonObject
add(java.lang.String name, java.lang.String value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specified string.JsonObject
addIfNotNull(java.lang.String name, java.lang.Object value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specified casted Object only if the value is not null.JsonObject
asObject()
Returns this JSON value asJsonObject
, assuming that this value represents a JSON object.boolean
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object)
.JsonValue
get(java.lang.String name)
Returns the value of the member with the specified name in this object.java.lang.Boolean
getBoolean(java.lang.String name)
Returns theBoolean
value of the member with the specified name in this object.boolean
getBoolean(java.lang.String name, boolean defaultValue)
Returns theboolean
value of the member with the specified name in this object.java.lang.Double
getDouble(java.lang.String name)
Returns theDouble
value of the member with the specified name in this object.double
getDouble(java.lang.String name, double defaultValue)
Returns thedouble
value of the member with the specified name in this object.java.lang.Float
getFloat(java.lang.String name)
Returns theFloat
value of the member with the specified name in this object.float
getFloat(java.lang.String name, float defaultValue)
Returns thefloat
value of the member with the specified name in this object.java.lang.Integer
getInt(java.lang.String name)
Returns theInteger
value of the member with the specified name in this object.int
getInt(java.lang.String name, int defaultValue)
Returns theint
value of the member with the specified name in this object.java.lang.Long
getLong(java.lang.String name)
Returns theLong
value of the member with the specified name in this object.long
getLong(java.lang.String name, long defaultValue)
Returns thelong
value of the member with the specified name in this object.java.lang.String
getString(java.lang.String name)
Returns theString
value of the member with the specified name in this object.java.lang.String
getString(java.lang.String name, java.lang.String defaultValue)
Returns theString
value of the member with the specified name in this object.int
hashCode()
boolean
isEmpty()
Returnstrue
if this object contains no members.boolean
isObject()
Detects whether this value represents a JSON object.java.util.Iterator<JsonObject.Member>
iterator()
Returns an iterator over the members of this object in document order.JsonObject
merge(JsonObject object)
Copies all members of the specified object into this object.java.util.List<java.lang.String>
names()
Returns a list of the names in this object in document order.JsonObject
remove(java.lang.String name)
Removes a member with the specified name from this object.JsonObject
set(java.lang.String name, boolean value)
Sets the value of the member with the specified name to the JSON representation of the specifiedboolean
value.JsonObject
set(java.lang.String name, double value)
Sets the value of the member with the specified name to the JSON representation of the specifieddouble
value.JsonObject
set(java.lang.String name, float value)
Sets the value of the member with the specified name to the JSON representation of the specifiedfloat
value.JsonObject
set(java.lang.String name, int value)
Sets the value of the member with the specified name to the JSON representation of the specifiedint
value.JsonObject
set(java.lang.String name, long value)
Sets the value of the member with the specified name to the JSON representation of the specifiedlong
value.JsonObject
set(java.lang.String name, JsonValue value)
Sets the value of the member with the specified name to the specified JSON value.JsonObject
set(java.lang.String name, java.lang.String value)
Sets the value of the member with the specified name to the JSON representation of the specified string.int
size()
Returns the number of members (name/value pairs) in this object.static JsonObject
unmodifiableObject(JsonObject object)
Returns an unmodifiable JsonObject for the specified one.-
Methods inherited from class io.github.jopenlibs.vault.json.JsonValue
asArray, asBoolean, asDouble, asFloat, asInt, asLong, asString, isArray, isBoolean, isFalse, isNull, isNumber, isString, isTrue, toString, toString, writeTo, writeTo
-
-
-
-
Constructor Detail
-
JsonObject
public JsonObject()
Creates a new empty JsonObject.
-
JsonObject
public JsonObject(JsonObject object)
Creates a new JsonObject, initialized with the contents of the specified JSON object.- Parameters:
object
- the JSON object to get the initial contents from, must not benull
-
-
Method Detail
-
unmodifiableObject
public static JsonObject unmodifiableObject(JsonObject object)
Returns an unmodifiable JsonObject for the specified one. This method allows to provide read-only access to a JsonObject.The returned JsonObject is backed by the given object and reflect changes that happen to it. Attempts to modify the returned JsonObject result in an
UnsupportedOperationException
.- Parameters:
object
- the JsonObject for which an unmodifiable JsonObject is to be returned- Returns:
- an unmodifiable view of the specified JsonObject
-
add
public JsonObject add(java.lang.String name, int value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedint
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, int[] value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedint[]
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, long value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedlong
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, float value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedfloat
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, double value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifieddouble
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, boolean value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specifiedboolean
value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, java.lang.String value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specified string.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
addIfNotNull
public JsonObject addIfNotNull(java.lang.String name, java.lang.Object value)
Appends a new member to the end of this object, with the specified name and the JSON representation of the specified casted Object only if the value is not null.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
add
public JsonObject add(java.lang.String name, JsonValue value)
Appends a new member to the end of this object, with the specified name and the specified JSON value.This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method
set(name, value)
instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.- Parameters:
name
- the name of the member to addvalue
- the value of the member to add, must not benull
- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, int value)
Sets the value of the member with the specified name to the JSON representation of the specifiedint
value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to replacevalue
- the value to set to the member- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, long value)
Sets the value of the member with the specified name to the JSON representation of the specifiedlong
value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to replacevalue
- the value to set to the member- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, float value)
Sets the value of the member with the specified name to the JSON representation of the specifiedfloat
value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, double value)
Sets the value of the member with the specified name to the JSON representation of the specifieddouble
value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, boolean value)
Sets the value of the member with the specified name to the JSON representation of the specifiedboolean
value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, java.lang.String value)
Sets the value of the member with the specified name to the JSON representation of the specified string. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to addvalue
- the value of the member to add- Returns:
- the object itself, to enable method chaining
-
set
public JsonObject set(java.lang.String name, JsonValue value)
Sets the value of the member with the specified name to the specified JSON value. If this object does not contain a member with this name, a new member is added at the end of the object. If this object contains multiple members with this name, only the last one is changed.This method should only be used to modify existing objects. To fill a new object with members, the method
add(name, value)
should be preferred which is much faster (as it does not need to search for existing members).- Parameters:
name
- the name of the member to addvalue
- the value of the member to add, must not benull
- Returns:
- the object itself, to enable method chaining
-
remove
public JsonObject remove(java.lang.String name)
Removes a member with the specified name from this object. If this object contains multiple members with the given name, only the last one is removed. 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:
- the object itself, to enable method chaining
-
merge
public JsonObject merge(JsonObject object)
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:
- the object itself, to enable method chaining
-
get
public JsonValue get(java.lang.String name)
Returns the value of the member with the specified name in this object. If this object contains multiple members with the given name, this method will return the last one.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or
null
if this object does not contain a member with that name
-
getInt
public java.lang.Integer getInt(java.lang.String name)
Returns theInteger
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javaint
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getInt
public int getInt(java.lang.String name, int defaultValue)
Returns theint
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javaint
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
getLong
public java.lang.Long getLong(java.lang.String name)
Returns theLong
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javalong
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getLong
public long getLong(java.lang.String name, long defaultValue)
Returns thelong
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javalong
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
getFloat
public java.lang.Float getFloat(java.lang.String name)
Returns theFloat
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javafloat
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getFloat
public float getFloat(java.lang.String name, float defaultValue)
Returns thefloat
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javafloat
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
getDouble
public java.lang.Double getDouble(java.lang.String name)
Returns theDouble
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javadouble
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getDouble
public double getDouble(java.lang.String name, double defaultValue)
Returns thedouble
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSON number or if it cannot be interpreted as Javadouble
, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.String name)
Returns theBoolean
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSONtrue
orfalse
value, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getBoolean
public boolean getBoolean(java.lang.String name, boolean defaultValue)
Returns theboolean
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one will be picked. If this member's value does not represent a JSONtrue
orfalse
value, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
getString
public java.lang.String getString(java.lang.String name)
Returns theString
value of the member with the specified name in this object. If this object does not contain a member with this name,null
is returned. If this object contains multiple members with the given name, the last one is picked. If this member's value does not represent a JSON string, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returned- Returns:
- the value of the last member with the specified name, or null if this object does not contain a member with that name
-
getString
public java.lang.String getString(java.lang.String name, java.lang.String defaultValue)
Returns theString
value of the member with the specified name in this object. If this object does not contain a member with this name, the given default value is returned. If this object contains multiple members with the given name, the last one is picked. If this member's value does not represent a JSON string, an exception is thrown.- Parameters:
name
- the name of the member whose value is to be returneddefaultValue
- the value to be returned if the requested member is missing- Returns:
- the value of the last member with the specified name, or the given default value if this object does not contain a member with that name
-
size
public int size()
Returns the number of members (name/value pairs) in this object.- Returns:
- the number of members in this object
-
isEmpty
public boolean isEmpty()
Returnstrue
if this object contains no members.- Returns:
true
if this object contains no members
-
names
public java.util.List<java.lang.String> names()
Returns a list of the names in this object in document order. The returned list is backed by this object and will reflect subsequent changes. It cannot be used to modify this object. Attempts to modify the returned list will result in an exception.- Returns:
- a list of the names in this object
-
iterator
public java.util.Iterator<JsonObject.Member> iterator()
Returns an iterator over the members of this object in document order. The returned iterator cannot be used to modify this object.- Specified by:
iterator
in interfacejava.lang.Iterable<JsonObject.Member>
- Returns:
- an iterator over the members of this object
-
isObject
public boolean isObject()
Description copied from class:JsonValue
Detects whether this value represents a JSON object. If this is the case, this value is an instance ofJsonObject
.
-
asObject
public JsonObject asObject()
Description copied from class:JsonValue
Returns this JSON value asJsonObject
, assuming that this value represents a JSON object. If this is not the case, an exception is thrown.
-
equals
public boolean equals(java.lang.Object obj)
Description copied from class:JsonValue
Indicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object)
.Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.
-
-