public class JsonObject extends JsonElement
A strongly-typed map from key type string
to value type JsonElement
?, inheriting from JsonElement
rather than the usual MapBase
.
Uses the property and function naming conventions of the JavaScript Map object.
Modifier and Type | Class and Description |
---|---|
static class |
JsonObject.Entry
A key/value pair for map entries.
|
static class |
JsonObject.EntryList
A list of item type
JsonObject.Entry . |
static class |
JsonObject.FieldOrdering
Comparer for JSON object field ordering.
|
Modifier and Type | Field and Description |
---|---|
static JsonObject |
empty |
TYPE_ARRAY, TYPE_BOOLEAN, TYPE_NUMBER, TYPE_OBJECT, TYPE_STRING
Constructor and Description |
---|
JsonObject()
See JsonObject(int).
|
JsonObject(int capacity)
Construct a new map with
JsonObject.size of zero and optional initial capacity . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all entries from this map.
|
boolean |
delete(java.lang.String key)
Delete the entry with the specified
key (if found). |
JsonObject.EntryList |
entries()
Return a list of the entries (key/value pairs) in this map.
|
static JsonObject |
fromElement(JsonElement value)
Return the 'value' cast as a
JsonObject . |
JsonElement |
get(java.lang.String key)
Return (nullable) The value from the entry with the specified
key (if found), otherwise null . |
JsonArray |
getArray(java.lang.String name)
Return a field of this object cast as a
JsonArray . |
boolean |
getBoolean(java.lang.String name)
Return a field of this object cast as a
boolean . |
JsonObject.FieldOrdering |
getCustomOrdering()
Return (nullable) Ordering constraints for JSON formatting.
|
JsonArray |
getNullableArray(java.lang.String name)
Return (nullable) A field of this object cast as a nullable
JsonArray . |
java.lang.Boolean |
getNullableBoolean(java.lang.String name)
Return (nullable) A field of this object cast as a nullable
string . |
JsonObject |
getNullableObject(java.lang.String name)
Return (nullable) A field of this object cast as a nullable
JsonObject . |
java.lang.String |
getNullableString(java.lang.String name)
Return (nullable) A field of this object cast as a nullable
string . |
JsonObject |
getObject(java.lang.String name)
Return a field of this object cast as a
JsonObject . |
JsonObject.EntryList |
getOptimizedEntries()
Return set for objects parsed with
JsonElement.parseOptimized , to include all json field/value pairs where the field name does not contain "@". |
JsonElement |
getRequired(java.lang.String key)
Return (nullable) The value from the entry with the specified
key (if found). |
JsonElement |
getRequiredField(java.lang.String field)
Return (nullable) The value of a required field of this object.
|
java.lang.String |
getString(java.lang.String name)
Return a field of this object cast as a
string . |
int |
getType()
Return type of JSON element.
|
com.sap.cloud.server.odata.core.UntypedMap |
getUntypedMap()
Return the underlying untyped map of objects.
|
boolean |
has(java.lang.String key)
Return
true if this map has an entry with the specified key , otherwise false . |
StringList |
keys()
Return a list of the entry keys in this map.
|
void |
set(java.lang.String key,
JsonElement value)
Add or replace an entry with the specified
key and value . |
void |
setArray(java.lang.String name,
JsonArray value)
Set a field of this object to a JSON array.
|
void |
setBoolean(java.lang.String name,
boolean value)
Set a field of this object to a JSON boolean.
|
void |
setCustomOrdering(JsonObject.FieldOrdering value)
Set ordering constraints for JSON formatting.
|
void |
setNumber(java.lang.String name,
java.lang.String value)
Set a field of this object to a JSON number.
|
void |
setObject(java.lang.String name,
JsonObject value)
Set a field of this object to a JSON object.
|
void |
setOptimizedEntries(JsonObject.EntryList value)
Set set for objects parsed with
JsonElement.parseOptimized , to include all json field/value pairs where the field name does not contain "@". |
void |
setString(java.lang.String name,
java.lang.String value)
Set a field of this object to a JSON string.
|
JsonObject |
setThis(java.lang.String key,
JsonElement value)
Add or replace an entry with the specified
key and value . |
int |
size()
Return the number of items in this map.
|
JsonObject.EntryList |
sortedEntries()
Return the entries of this map sorted by key.
|
StringList |
sortedKeys()
Return the sorted keys of this map.
|
JsonArray |
sortedValues()
Return the sorted values of this map.
|
java.lang.String |
toString()
Return the object in JSON format.
|
JsonArray |
values()
Return a list of the entry values in this map.
|
JsonObject |
withVersion(int version)
Sets
JsonObject.customOrdering according to the specified version . |
applyVersion, format, parse, parseArray, parseObject, parseOptimized
public static final JsonObject empty
public JsonObject()
public JsonObject(int capacity)
Construct a new map with JsonObject.size
of zero and optional initial capacity
.
A map can expand in size beyond its initial capacity, but best performance
will be obtained if the initial capacity is close to the map's maximum size.
capacity
- Optional initial capacity.public void clear()
Remove all entries from this map.
public boolean delete(java.lang.String key)
Delete the entry with the specified key
(if found).
key
- Entry key.true
if an entry with the specified key
was found (and deleted).public JsonObject.EntryList entries()
Return a list of the entries (key/value pairs) in this map.
public static JsonObject fromElement(JsonElement value)
Return the 'value' cast as a JsonObject
.
JsonException
if value
is not an instanceof of JsonObject
.
value
- (nullable) Element value.JsonObject
.public JsonElement get(java.lang.String key)
Return (nullable) The value from the entry with the specified key
(if found), otherwise null
.
key
- Entry key.key
(if found), otherwise null
.public JsonArray getArray(java.lang.String name)
Return a field of this object cast as a JsonArray
.
name
- Field name.JsonArray
.public boolean getBoolean(java.lang.String name)
Return a field of this object cast as a boolean
.
name
- Field name.boolean
.public JsonObject.FieldOrdering getCustomOrdering()
Return (nullable) Ordering constraints for JSON formatting. By default, fields may appear in arbitrary order.
public JsonArray getNullableArray(java.lang.String name)
Return (nullable) A field of this object cast as a nullable JsonArray
.
name
- Field name.JsonArray
.public java.lang.Boolean getNullableBoolean(java.lang.String name)
Return (nullable) A field of this object cast as a nullable string
.
name
- Field name.string
.public JsonObject getNullableObject(java.lang.String name)
Return (nullable) A field of this object cast as a nullable JsonObject
.
name
- Field name.JsonObject
.public java.lang.String getNullableString(java.lang.String name)
Return (nullable) A field of this object cast as a nullable string
.
name
- Field name.string
.public JsonObject getObject(java.lang.String name)
Return a field of this object cast as a JsonObject
.
name
- Field name.JsonObject
.public JsonObject.EntryList getOptimizedEntries()
Return set for objects parsed with JsonElement.parseOptimized
, to include all json field/value pairs where the field name does not contain "@". Otherwise empty.
The optimization is that adding entries to a list can be more efficient than adding them to a map, so long as the list consumer doesn't require by-name lookup.
JsonElement.parseOptimized
, to include all json field/value pairs where the field name does not contain "@". Otherwise empty.public JsonElement getRequired(java.lang.String key)
Return (nullable) The value from the entry with the specified key
(if found).
MissingEntryException
if no entry is found for the specified key.
key
- Entry key.public JsonElement getRequiredField(java.lang.String field)
Return (nullable) The value of a required field of this object.
field
- Field name.public java.lang.String getString(java.lang.String name)
Return a field of this object cast as a string
.
name
- Field name.string
.public int getType()
Return type of JSON element.
constants.
getType
in class JsonElement
public com.sap.cloud.server.odata.core.UntypedMap getUntypedMap()
Return the underlying untyped map of objects. Use with care, avoiding the addition of objects with an incorrect item type.
public boolean has(java.lang.String key)
Return true
if this map has an entry with the specified key
, otherwise false
.
key
- Entry key.true
if this map has an entry with the specified key
, otherwise false
.public StringList keys()
Return a list of the entry keys in this map.
public void set(java.lang.String key, JsonElement value)
Add or replace an entry with the specified key
and value
.
value
- (nullable) Entry value.key
- Entry key.public void setArray(java.lang.String name, JsonArray value)
Set a field of this object to a JSON array.
name
- Field name.value
- Field value.public void setBoolean(java.lang.String name, boolean value)
Set a field of this object to a JSON boolean.
name
- Field name.value
- Field value.public void setCustomOrdering(JsonObject.FieldOrdering value)
Set ordering constraints for JSON formatting. By default, fields may appear in arbitrary order.
value
- Ordering constraints for JSON formatting. By default, fields may appear in arbitrary order.public void setNumber(java.lang.String name, java.lang.String value)
Set a field of this object to a JSON number.
name
- Field name.value
- Field value (expressed as a string to avoid lossy conversion).public void setObject(java.lang.String name, JsonObject value)
Set a field of this object to a JSON object.
name
- Field name.value
- Field value.public void setOptimizedEntries(JsonObject.EntryList value)
Set set for objects parsed with JsonElement.parseOptimized
, to include all json field/value pairs where the field name does not contain "@". Otherwise empty.
The optimization is that adding entries to a list can be more efficient than adding them to a map, so long as the list consumer doesn't require by-name lookup.
value
- Set for objects parsed with JsonElement.parseOptimized
, to include all json field/value pairs where the field name does not contain "@". Otherwise empty.public void setString(java.lang.String name, java.lang.String value)
Set a field of this object to a JSON string.
name
- Field name.value
- Field value.public JsonObject setThis(java.lang.String key, JsonElement value)
Add or replace an entry with the specified key
and value
.
key
- Entry key.value
- (nullable) Entry value.public int size()
Return the number of items in this map.
public JsonObject.EntryList sortedEntries()
Return the entries of this map sorted by key.
JsonObject.customOrdering
, which can be used to override the ordering.
public StringList sortedKeys()
Return the sorted keys of this map.
public JsonArray sortedValues()
Return the sorted values of this map.
public java.lang.String toString()
Return the object in JSON format.
toString
in class JsonElement
public JsonArray values()
Return a list of the entry values in this map.
public JsonObject withVersion(int version)
Sets JsonObject.customOrdering
according to the specified version
.
version
- Data version.