Package com.unboundid.util.json
Class JSONObject
java.lang.Object
com.unboundid.util.json.JSONValue
com.unboundid.util.json.JSONObject
- All Implemented Interfaces:
Serializable,Comparable<JSONValue>
@NotMutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public final class JSONObject
extends JSONValue
This class provides an implementation of a JSON value that represents an
object with zero or more name-value pairs. In each pair, the name is a JSON
string and the value is any type of JSON value (
The string representation of a JSON object is an open curly brace (U+007B) followed by a comma-delimited list of the name-value pairs that comprise the fields in that object and a closing curly brace (U+007D). Each name-value pair is represented as a JSON string followed by a colon and the appropriate string representation of the value. There must not be a comma between the last field and the closing curly brace. There may optionally be any amount of whitespace (where whitespace characters include the ASCII space, horizontal tab, line feed, and carriage return characters) after the open curly brace, on either or both sides of the colon separating a field name from its value, on either or both sides of commas separating fields, and before the closing curly brace. The order in which fields appear in the string representation is not considered significant.
The string representation returned by the
The normalized string representation will not include any optional spaces, and the normalized string representation of each field value will be obtained using that value's
null, true,
false, number, string, array, or object). Although the ECMA-404
specification does not explicitly forbid a JSON object from having multiple
fields with the same name, RFC 7159 section 4 states that field names should
be unique, and this implementation does not support objects in which multiple
fields have the same name. Note that this uniqueness constraint only applies
to the fields directly contained within an object, and does not prevent an
object from having a field value that is an object (or that is an array
containing one or more objects) that use a field name that is also in use
in the outer object. Similarly, if an array contains multiple JSON objects,
then there is no restriction preventing the same field names from being
used in separate objects within that array.
The string representation of a JSON object is an open curly brace (U+007B) followed by a comma-delimited list of the name-value pairs that comprise the fields in that object and a closing curly brace (U+007D). Each name-value pair is represented as a JSON string followed by a colon and the appropriate string representation of the value. There must not be a comma between the last field and the closing curly brace. There may optionally be any amount of whitespace (where whitespace characters include the ASCII space, horizontal tab, line feed, and carriage return characters) after the open curly brace, on either or both sides of the colon separating a field name from its value, on either or both sides of commas separating fields, and before the closing curly brace. The order in which fields appear in the string representation is not considered significant.
The string representation returned by the
toString() method (or
appended to the buffer provided to the toString(StringBuilder)
method) will include one space before each field name and one space before
the closing curly brace. There will not be any space on either side of the
colon separating the field name from its value, and there will not be any
space between a field value and the comma that follows it. The string
representation of each field name will use the same logic as the
JSONString.toString() method, and the string representation of each
field value will be obtained using that value's toString method.
The normalized string representation will not include any optional spaces, and the normalized string representation of each field value will be obtained using that value's
toNormalizedString method. Field names will be
treated in a case-sensitive manner, but all characters outside the LDAP
printable character set will be escaped using the \u-style
Unicode encoding. The normalized string representation will have fields
listed in lexicographic order.- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJSONObject(JSONField... fields) Creates a new JSON object with the provided fields.JSONObject(String stringRepresentation) Creates a new JSON object parsed from the provided string.JSONObject(Map<String, JSONValue> fields) Creates a new JSON object with the provided fields. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendToJSONBuffer(JSONBuffer buffer) Appends this value to the provided JSON buffer.voidappendToJSONBuffer(String fieldName, JSONBuffer buffer) Appends a field with the given name and this value to the provided JSON buffer.booleanequals(JSONObject o, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Indicates whether this JSON object is considered equal to the provided object, subject to the specified constraints.booleanIndicates whether this JSON value is considered equal to the provided JSON value, subject to the specified constraints.booleanIndicates whether the provided object is equal to this JSON value.Retrieves the value for the specified field.getFieldAsArray(String name) Retrieves a list of the elements in the specified array field.getFieldAsBigDecimal(String name) Retrieves the value of the specified field as a BigDecimal.getFieldAsBoolean(String name) Retrieves the value of the specified field as a Boolean.getFieldAsInteger(String name) Retrieves the value of the specified field as an integer.getFieldAsLong(String name) Retrieves the value of the specified field as a long.getFieldAsObject(String name) Retrieves the value of the specified field as a JSON object.getFieldAsString(String name) Retrieves the value of the specified field as a string.Retrieves the value for the specified field, treating the field name as case-insensitive.Retrieves the value for the specified field, treating the field name as case-insensitive.Retrieves a map of the fields contained in this JSON object.getFieldsIgnoreCase(String name) Retrieves a map of all fields with the specified name, treating the name as case-insensitive.booleanIndicates whether this JSON object has a field with the specified name.inthashCode()Retrieves a hash code for this JSON value.booleanhasNullField(String name) Indicates whether this JSON object has a null field with the specified name.Retrieves a user-friendly string representation of this JSON object that may be formatted across multiple lines for better readability.Retrieves a normalized string representation of this JSON object.toNormalizedString(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Retrieves a normalized string representation of this JSON object.voidtoNormalizedString(StringBuilder buffer) Appends a normalized string representation of this JSON object to the provided buffer.voidtoNormalizedString(StringBuilder buffer, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Appends a normalized string representation of this JSON object to the provided buffer.toNormalizedValue(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Retrieves a normalized representation of this value using the provided settings.Retrieves a single-line string representation of this JSON object.voidtoSingleLineString(StringBuilder buffer) Appends a single-line string representation of this JSON object to the provided buffer.toString()Retrieves a string representation of this JSON object.voidtoString(StringBuilder buffer) Appends a string representation of this JSON object to the provided buffer.
-
Field Details
-
EMPTY_OBJECT
A pre-allocated empty JSON object.
-
-
Constructor Details
-
JSONObject
Creates a new JSON object with the provided fields.- Parameters:
fields- The fields to include in this JSON object. It may benullor empty if this object should not have any fields.
-
JSONObject
Creates a new JSON object with the provided fields.- Parameters:
fields- The set of fields for this JSON object. It may benullor empty if there should not be any fields.
-
JSONObject
Creates a new JSON object parsed from the provided string.- Parameters:
stringRepresentation- The string to parse as a JSON object. It must represent exactly one JSON object.- Throws:
JSONException- If the provided string cannot be parsed as a valid JSON object.
-
-
Method Details
-
getFields
Retrieves a map of the fields contained in this JSON object.- Returns:
- A map of the fields contained in this JSON object.
-
getField
Retrieves the value for the specified field.- Parameters:
name- The name of the field for which to retrieve the value. It will be treated in a case-sensitive manner.- Returns:
- The value for the specified field, or
nullif the requested field is not present in the JSON object.
-
getFieldIgnoreCaseIgnoreConflict
Retrieves the value for the specified field, treating the field name as case-insensitive. If the object has multiple fields with different capitalizations of the specified name, then only the first one found will be returned and any subsequent fields will be ignored.- Parameters:
name- The name of the field for which to retrieve the value. It will be treated in a case-insensitive manner.- Returns:
- The value for the specified field, or
nullif the requested field is not present in the JSON object.
-
getFieldIgnoreCaseThrowOnConflict
@Nullable public JSONValue getFieldIgnoreCaseThrowOnConflict(@NotNull String name) throws JSONException Retrieves the value for the specified field, treating the field name as case-insensitive. If the object has multiple fields with different capitalizations of the first name, then an exception will be thrown.- Parameters:
name- The name of the field for which to retrieve the value. It will be treated in a case-insensitive manner.- Returns:
- The value for the specified field, or
nullif the requested field is not present in the JSON object. - Throws:
JSONException- If the object has multiple fields with different capitalizations of the provided name.
-
getFieldsIgnoreCase
Retrieves a map of all fields with the specified name, treating the name as case-insensitive.- Parameters:
name- The name of the field for which to retrieve the values. It will be treated in a case-insensitive manner.- Returns:
- A map of all fields with the specified name, or an empty map if there are no fields with the specified name.
-
getFieldAsString
Retrieves the value of the specified field as a string.- Parameters:
name- The name of the field for which to retrieve the string value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as a string, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not a string.
-
getFieldAsBoolean
Retrieves the value of the specified field as a Boolean.- Parameters:
name- The name of the field for which to retrieve the Boolean value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as a Boolean, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not a Boolean.
-
getFieldAsInteger
Retrieves the value of the specified field as an integer.- Parameters:
name- The name of the field for which to retrieve the integer value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as an integer, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not a number that can be exactly represented as an integer.
-
getFieldAsLong
Retrieves the value of the specified field as a long.- Parameters:
name- The name of the field for which to retrieve the long value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as a long, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not a number that can be exactly represented as a long.
-
getFieldAsBigDecimal
Retrieves the value of the specified field as a BigDecimal.- Parameters:
name- The name of the field for which to retrieve the BigDecimal value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as a BigDecimal, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not a number.
-
getFieldAsObject
Retrieves the value of the specified field as a JSON object.- Parameters:
name- The name of the field for which to retrieve the value. It will be treated in a case-sensitive manner.- Returns:
- The value of the specified field as a JSON object, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not an object.
-
getFieldAsArray
Retrieves a list of the elements in the specified array field.- Parameters:
name- The name of the field for which to retrieve the array values. It will be treated in a case-sensitive manner.- Returns:
- A list of the elements in the specified array field, or
nullif this JSON object does not have a field with the specified name, or if the value of that field is not an array.
-
hasNullField
Indicates whether this JSON object has a null field with the specified name.- Parameters:
name- The name of the field for which to make the determination. It will be treated in a case-sensitive manner.- Returns:
trueif this JSON object has a null field with the specified name, orfalseif this JSON object does not have a field with the specified name, or if the value of that field is not a null.
-
hasField
Indicates whether this JSON object has a field with the specified name.- Parameters:
fieldName- The name of the field for which to make the determination. It will be treated in a case-sensitive manner.- Returns:
trueif this JSON object has a field with the specified name, orfalseif not.
-
hashCode
Retrieves a hash code for this JSON value. -
equals
Indicates whether the provided object is equal to this JSON value. -
equals
public boolean equals(@NotNull JSONObject o, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Indicates whether this JSON object is considered equal to the provided object, subject to the specified constraints.- Parameters:
o- The object to compare against this JSON object. It must not benull.ignoreFieldNameCase- Indicates whether to ignore differences in capitalization in field names.ignoreValueCase- Indicates whether to ignore differences in capitalization in values that are JSON strings.ignoreArrayOrder- Indicates whether to ignore differences in the order of elements within an array.- Returns:
trueif this JSON object is considered equal to the provided object (subject to the specified constraints), orfalseif not.
-
equals
public boolean equals(@NotNull JSONValue v, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Indicates whether this JSON value is considered equal to the provided JSON value, subject to the specified constraints. Note that not all constraints will apply to all data types.- Specified by:
equalsin classJSONValue- Parameters:
v- The JSON value for which to make the determination. It must not benull.ignoreFieldNameCase- Indicates whether to ignore differences in the capitalization of JSON field names.ignoreValueCase- Indicates whether to ignore differences in the capitalization of JSON values that represent strings.ignoreArrayOrder- Indicates whether to ignore differences in the order of elements in JSON arrays.- Returns:
trueif this JSON value is considered equal to the provided JSON value (subject to the specified constraints), orfalseif not.
-
toString
Retrieves a string representation of this JSON object. If this object was decoded from a string, then the original string representation will be used. Otherwise, a single-line string representation will be constructed. -
toString
Appends a string representation of this JSON object to the provided buffer. If this object was decoded from a string, then the original string representation will be used. Otherwise, a single-line string representation will be constructed. -
toMultiLineString
Retrieves a user-friendly string representation of this JSON object that may be formatted across multiple lines for better readability. The last line will not include a trailing line break.- Returns:
- A user-friendly string representation of this JSON object that may be formatted across multiple lines for better readability.
-
toSingleLineString
Retrieves a single-line string representation of this JSON object.- Specified by:
toSingleLineStringin classJSONValue- Returns:
- A single-line string representation of this JSON object.
-
toSingleLineString
Appends a single-line string representation of this JSON object to the provided buffer.- Specified by:
toSingleLineStringin classJSONValue- Parameters:
buffer- The buffer to which the information should be appended.
-
toNormalizedString
Retrieves a normalized string representation of this JSON object. The normalized representation of the JSON object will have the following characteristics:- It will not include any line breaks.
- It will not include any spaces around the enclosing braces.
- It will not include any spaces around the commas used to separate fields.
- Field names will be treated in a case-sensitive manner and will not be altered.
- Field values will be normalized.
- Fields will be listed in lexicographic order by field name.
- Specified by:
toNormalizedStringin classJSONValue- Returns:
- A normalized string representation of this JSON object.
-
toNormalizedString
Appends a normalized string representation of this JSON object to the provided buffer. The normalized representation of the JSON object will have the following characteristics:- It will not include any line breaks.
- It will not include any spaces around the enclosing braces.
- It will not include any spaces around the commas used to separate fields.
- Field names will be treated in a case-sensitive manner and will not be altered.
- Field values will be normalized.
- Fields will be listed in lexicographic order by field name.
- Specified by:
toNormalizedStringin classJSONValue- Parameters:
buffer- The buffer to which the information should be appended.
-
toNormalizedString
@NotNull public String toNormalizedString(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Retrieves a normalized string representation of this JSON object. The normalized representation of the JSON object will have the following characteristics:- It will not include any line breaks.
- It will not include any spaces around the enclosing braces.
- It will not include any spaces around the commas used to separate fields.
- Case sensitivity of field names and values will be controlled by argument values.
- Fields will be listed in lexicographic order by field name.
- Specified by:
toNormalizedStringin classJSONValue- Parameters:
ignoreFieldNameCase- Indicates whether field names should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreValueCase- Indicates whether string field values should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreArrayOrder- Indicates whether the order of elements in an array should be considered significant (iffalse) or insignificant (iftrue).- Returns:
- A normalized string representation of this JSON object.
-
toNormalizedString
public void toNormalizedString(@NotNull StringBuilder buffer, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Appends a normalized string representation of this JSON object to the provided buffer. The normalized representation of the JSON object will have the following characteristics:- It will not include any line breaks.
- It will not include any spaces around the enclosing braces.
- It will not include any spaces around the commas used to separate fields.
- Field names will be treated in a case-sensitive manner and will not be altered.
- Field values will be normalized.
- Fields will be listed in lexicographic order by field name.
- Specified by:
toNormalizedStringin classJSONValue- Parameters:
buffer- The buffer to which the information should be appended.ignoreFieldNameCase- Indicates whether field names should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreValueCase- Indicates whether string field values should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreArrayOrder- Indicates whether the order of elements in an array should be considered significant (iffalse) or insignificant (iftrue).
-
toNormalizedValue
@NotNull public JSONObject toNormalizedValue(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder) Retrieves a normalized representation of this value using the provided settings.- Specified by:
toNormalizedValuein classJSONValue- Parameters:
ignoreFieldNameCase- Indicates whether field names should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreValueCase- Indicates whether string field values should be treated in a case-sensitive (iffalse) or case-insensitive (iftrue) manner.ignoreArrayOrder- Indicates whether the order of elements in an array should be considered significant (iffalse) or insignificant (iftrue).- Returns:
- A normalized representation of this value using the provided settings.
-
appendToJSONBuffer
Appends this value to the provided JSON buffer. This will not include a field name, so it should only be used for Boolean value elements in an array.- Specified by:
appendToJSONBufferin classJSONValue- Parameters:
buffer- The JSON buffer to which this value should be appended.
-
appendToJSONBuffer
Appends a field with the given name and this value to the provided JSON buffer.- Specified by:
appendToJSONBufferin classJSONValue- Parameters:
fieldName- The name to use for the field.buffer- The JSON buffer to which this value should be appended.
-