public abstract class CborMap extends CborObject
This class is used to represent data items of major type 5 (CborMajorType.MAP).
| Modifier and Type | Method and Description |
|---|---|
boolean |
areAllKeysStrings()
Determines if all of the keys in this map are
CborTextString instances. |
void |
clear() |
boolean |
containsKey(String key)
Convenience test method to allow the use of standard
String objects when the keys in
the map are all CborTextString. |
CborMap |
copy()
Creates an independent, deep copy of this
CborMap. |
static CborMap |
create()
Creates an empty, untagged
CborMap object. |
static CborMap |
create(int tag)
Creates an empty, tagged
CborMap object. |
static CborMap |
create(Map<CborObject,CborObject> map)
Creates a
CborMap object populated with the keys and values from map. |
static CborMap |
create(Map<CborObject,CborObject> map,
int tag)
Creates a tagged
CborMap object populated with the keys and values from map
. |
static CborMap |
create(Map<CborObject,CborObject> map,
int tag,
boolean isIndefiniteLength)
Creates a tagged
CborMap object populated with the keys and values from map
. |
static CborMap |
createFromCborByteArray(byte[] input)
Parses the given CBOR byte array into a
CborMap object. |
static CborMap |
createFromCborByteArray(byte[] input,
int offset,
int length)
Parses the given CBOR byte array into a
CborMap object. |
static CborMap |
createFromJavaObject(Map<?,?> obj)
|
static CborMap |
createFromJSONObject(org.json.JSONObject obj)
Creates a untagged
CborMap object from the given JSONObject. |
Set<Map.Entry<CborObject,CborObject>> |
entrySet() |
boolean |
equals(Object obj) |
CborObject |
get(CborObject key) |
CborObject |
get(String key)
Convenience getter method to allow the use of standard
String objects when the keys
in the map are all CborTextString. |
int |
getAdditionalInformation()
Returns the object's "additional information" value.
|
int |
getMajorType()
Returns the object's "Major Type", as defined by RFC7049.
|
int |
hashCode() |
boolean |
isEmpty() |
abstract boolean |
isIndefiniteLength() |
boolean |
isValidJson()
Indicates if this data item can be losslessly converted to a JSON fragment.
|
Set<CborObject> |
keySet() |
Set<String> |
keySetAsStrings()
Returns the key set as standard
String objects instead of CborObject
instances. |
abstract Map<CborObject,CborObject> |
mapValue() |
CborObject |
put(String key,
CborObject value)
Convenience setter method to allow the use of standard
String objects when the keys
in the map are all CborTextString. |
CborObject |
remove(CborObject key) |
CborObject |
remove(String key)
Convenience removal method to allow the use of standard
String objects when the keys
in the map are all CborTextString. |
int |
size() |
Map<Object,Object> |
toJavaObject()
Converts the given data item into a standard java object representation.
|
<T> T |
toJavaObject(Class<T> clazz)
Attempts to convert this data item to a java object with a specific class.
|
String |
toJsonString()
Returns a representation of this data item as JSON text.
|
Map<String,Object> |
toNormalMap()
|
String |
toString(int indentLevel)
Describes this data item using CBOR diagnostic notation at the given indentation level.
|
createFromJavaObject, getTag, toCborByteArray, toStringpublic static CborMap createFromCborByteArray(byte[] input, int offset, int length) throws CborParseException
CborMap object. If the data in the byte array
is corrupted or if the type of object represented in the byte array isn't a CBOR Map, CborParseException is thrown. This method is similar to CborObject.createFromCborByteArray(byte[], int, int), except that it is CborMap-specific.input - CBOR-encoded byte array. Must be a CBOR Map.offset - Offset into input to start parsing.length - the number of bytes to parseCborMap objectCborParseException - if the input data is corrupt or if the input data doesn't
represent a CborMap.IndexOutOfBoundsException - if offset ois out of boundscreateFromCborByteArray(byte[]),
CborObject.createFromCborByteArray(byte[], int, int)public static CborMap createFromCborByteArray(byte[] input) throws CborParseException
CborMap object. If the data in the byte array
is corrupted or if the type of object represented in the byte array isn't a CBOR Map, CborParseException is thrown. This method is similar to CborObject.createFromCborByteArray(byte[]), except that it is CborMap-specific.input - CBOR-encoded byte array. Must be a CBOR Map.CborMap objectCborParseException - if the input data is corrupt or if the input data doesn't
represent a CborMap.createFromCborByteArray(byte[], int, int),
CborObject.createFromCborByteArray(byte[])public static CborMap createFromJavaObject(Map<?,?> obj) throws CborConversionException
Map into a CborMap object. If the map contains keys
or values that cannot be converted to CborObject instances, then CborConversionException is thrown. The returned CborMap will be untagged.obj - the map to convert to a CborMap.CborMap objectCborConversionException - if any of the map's keys or values cannot be represented as a
CborObject.public static CborMap create(Map<CborObject,CborObject> map, int tag)
CborMap object populated with the keys and values from map
. The keys and values from map are used directly (they are not
deep-copied).public static CborMap create(Map<CborObject,CborObject> map, int tag, boolean isIndefiniteLength)
CborMap object populated with the keys and values from map
. The keys and values from map are used directly (they are not
deep-copied).public static CborMap create(Map<CborObject,CborObject> map)
CborMap object populated with the keys and values from map.
The keys and values from map are used directly (they are not deep-copied).public static CborMap createFromJSONObject(org.json.JSONObject obj)
CborMap object from the given JSONObject.public static CborMap create()
CborMap object.CborMap objectpublic abstract Map<CborObject,CborObject> mapValue()
public int size()
public boolean isEmpty()
@CanIgnoreReturnValue public CborObject remove(CborObject key)
public void clear()
public Set<Map.Entry<CborObject,CborObject>> entrySet()
public Set<CborObject> keySet()
public CborObject get(CborObject key)
public final CborObject get(String key)
String objects when the keys
in the map are all CborTextString. The given key WILL NOT match tagged CborTextString objects, only untagged objects will match.key - the string value to use as a key. i * @return The CborObject value, if
found. null otherwise.put(String, CborObject),
containsKey(String),
remove(String),
keySetAsStrings()public final boolean containsKey(String key)
String objects when the keys in
the map are all CborTextString. The given key WILL NOT match tagged CborTextString objects, only untagged objects will match.key - the string value to use as a key.CborTextString object, false otherwise.put(String, CborObject),
get(String),
remove(String),
keySetAsStrings()@CanIgnoreReturnValue public final CborObject put(String key, CborObject value)
String objects when the keys
in the map are all CborTextString. The given key WILL NOT match tagged CborTextString objects, only untagged objects will match.key - the string value to use as a key.value - the CborObject to associate with this key.get(String),
containsKey(String),
remove(String),
keySetAsStrings()@CanIgnoreReturnValue public final CborObject remove(String key)
String objects when the keys
in the map are all CborTextString. The given key WILL NOT match tagged CborTextString objects, only untagged objects will match.key - the string value to use as a key.get(String),
containsKey(String),
keySetAsStrings()public final Set<String> keySetAsStrings() throws CborConversionException
String objects instead of CborObject
instances. areAllKeysStrings() must return true in order for this method to be
usable.CborConversionException - if there are keys in this map which are not CborTextString instances.put(String, CborObject),
get(String),
containsKey(String)public final boolean areAllKeysStrings()
CborTextString instances.CborTextString instances or if there
are no entries in this map; false otherwise.keySetAsStrings()public final Map<String,Object> toNormalMap() throws CborConversionException
CborMap into a standard Java Map<String,Object> that is
guaranteed to use String objects for keys. This method differs from CborObject.toJavaObject() in that this method is guaranteed to return a map that is keyed
only with String objects.CborConversionException - if not all of the keys are CborTextString objects.public final int getMajorType()
CborObjectgetMajorType in class CborObjectCborMajorType,
CborObject.getAdditionalInformation()public final int getAdditionalInformation()
CborObjectgetAdditionalInformation in class CborObjectCborObject.getMajorType()public abstract boolean isIndefiniteLength()
public final Map<Object,Object> toJavaObject()
CborObjectTags are largely ignored, except in cases where the tag itself has defined behavior when
converting to JSON, like CborTag.EXPECTED_BASE16 and CborTag.EXPECTED_BASE64.
The resulting object type is dependent on the data item:
CborMap → Map<Object,Object>
CborArray → Collection<Object>
CborFloat → Float/Double
CborInteger → Integer/Long
CborTextString → String
CborByteString → byte[]
CborSimple.TRUE/CborSimple.FALSE → Boolean
CborSimple values → null
The returned value is a wholly independent copy.
toJavaObject in class CborObjectCborObject.createFromJavaObject(Object)public <T> T toJavaObject(Class<T> clazz) throws CborConversionException
CborObjectUnlike CborObject.toJavaObject(), this method will introspect into tags to figure out
conversions to BigInteger and URI. Unknown tags are ignored.
This method will return the most expressive result that satisfies the clazz
parameter. For example, if the data item is a CborByteString tagged CborTag.BIGNUM_POS, calling this method with different classes will lead to different
results:
toJavaObject(byte[].class would return a byte[]
toJavaObject(Object.class would return a BigInteger
toJavaObject(BigInteger.class would return a BigInteger
toJavaObject(URI.class would throw CborConversionException
Whereas this method called an untagged CborByteString would behave like this:
toJavaObject(byte[].class would return a byte[]
toJavaObject(Object.class would return a byte[]
toJavaObject(BigInteger.class would throw CborConversionException
toJavaObject(URI.class would throw CborConversionException
toJavaObject in class CborObjectclazz - The desired class of the returned objectclazz or null if this data item was
CborSimple.NULL or CborSimple.UNDEFINED.CborConversionException - if the underlying data item could not be represented as an
instance of clazz.public final CborMap copy()
CborMap.copy in class CborObjectCborMap object.public final boolean isValidJson()
CborObjectNote that, with few exceptions, tags are not considered in this determination, since CborObject.toJsonString()
will strip out tags anyway.
isValidJson in class CborObjecttrue if this object could be rendered as JSON, false otherwise.public final String toJsonString()
CborObjectThe result is guaranteed to be syntactically-valid RFC8259 JSON text. In the event that there is
no JSON text representation for the underlying data item, its value will be replaced with a
placeholder JSON value (usually "null"). This condition can be detected beforehand
using CborObject.isValidJson().
Note that, in some pre-RFC8259 contexts, this method could be described as returning a
"JSON fragment" unless the data item was of the types CborMap (for a JSON
object) or CborArray (for a JSON array).
toJsonString in class CborObjectString containing representation of this data item as JSON textCborObject.isValidJson(),
RFC8259public String toString(int indentLevel)
CborObjectThe output is similar to that of CborObject.toJsonString() except that it also includes tag
and encoding information that is CBOR-specific.
The returned string is intended as a diagnostic aid and not intended to be machine-readable.
toString in class CborObjectindentLevel - the indentation level of the string to output. A negative value
indicates no newlines or whitespace should be added.String describing the data item.CborObject.toString()Copyright © 2018–2023. All rights reserved.