public abstract class CborObject extends Object
CborArray,
CborFloat,
CborInteger,
CborNumber,
CborMap,
CborByteString,
CborSimple,
CborTextString,
RFC7049| Modifier and Type | Method and Description |
|---|---|
abstract CborObject |
copy()
Creates an independent, deep copy of this data item.
|
static CborObject |
createFromCborByteArray(byte[] input)
Creates a new
CborObject from a CBOR-encoded byte array. |
static CborObject |
createFromCborByteArray(byte[] input,
int offset,
int length)
Creates a new
CborObject from a CBOR-encoded byte array starting at a given offset. |
static CborObject |
createFromJavaObject(@Nullable Object obj)
Creates a new
CborObject based on the given Java object. |
abstract int |
getAdditionalInformation()
Returns the object's "additional information" value.
|
abstract int |
getMajorType()
Returns the object's "Major Type", as defined by RFC7049.
|
int |
getTag()
Returns the tag for this object.
|
abstract boolean |
isValidJson()
Indicates if this data item can be losslessly converted to a JSON fragment.
|
byte[] |
toCborByteArray()
Convenience method to quickly obtaining a CBOR byte-encoding of this data item.
|
abstract @Nullable Object |
toJavaObject()
Converts the given data item into a standard java object representation.
|
abstract <T> T |
toJavaObject(Class<T> clazz)
Attempts to convert this data item to a java object with a specific class.
|
abstract String |
toJsonString()
Returns a representation of this data item as JSON text.
|
String |
toString()
Describes this data item using CBOR diagnostic notation.
|
abstract String |
toString(int indentLevel)
Describes this data item using CBOR diagnostic notation at the given indentation level.
|
public static CborObject createFromCborByteArray(byte[] input, int offset, int length) throws CborParseException
CborObject from a CBOR-encoded byte array starting at a given offset.
Provided as convenient alternative to using CborReader.
input - byte array to parseoffset - index of first byte to start parsinglength - the number of bytes to parseCborParseException - if the input data could not be parsed correctly or if there was
extra data present at the end of inputIndexOutOfBoundsException - if offset is out of boundscreateFromCborByteArray(byte[]),
CborReaderpublic static CborObject createFromCborByteArray(byte[] input) throws CborParseException
CborObject from a CBOR-encoded byte array.
Provided as convenient alternative to using CborReader.
input - byte array to parseCborParseException - if the input data could not be parsed correctlycreateFromCborByteArray(byte[], int, int),
CborReaderpublic static CborObject createFromJavaObject(@Nullable Object obj) throws CborConversionException
CborObject based on the given Java object. Supported Java class types
include:
null → CborSimple.NULL
Float/Double → CborFloat
Integer/Long/Short → CborInteger
String → CborTextString
Boolean → CborSimple.TRUE/CborSimple.FALSE
Map → CborMap
Iterable → CborArray
URI → CborTag.URI + CborTextString
BigInteger → CborTag.BIGNUM_POS/CborTag.BIGNUM_NEG + CborByteString
byte[] → CborByteString
int[]/short[]/long[] → CborArray
float[]/double[] → CborArray
String[] → CborArray
obj - the Java object to convert. May be null.CborObject representing objCborConversionException - if the object cannot be convertedtoJavaObject()public int getTag()
If the object is not tagged, returns CborTag.UNTAGGED.
CborTag.UNTAGGED if
untagged.CborTagpublic abstract int getMajorType()
CborMajorType,
getAdditionalInformation()public abstract int getAdditionalInformation()
getMajorType()public abstract boolean isValidJson()
Note that, with few exceptions, tags are not considered in this determination, since toJsonString()
will strip out tags anyway.
true if this object could be rendered as JSON, false otherwise.public abstract String toJsonString()
The 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 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).
String containing representation of this data item as JSON textisValidJson(),
RFC8259public abstract @Nullable Object toJavaObject()
Tags 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.
createFromJavaObject(Object)public abstract <T> T toJavaObject(Class<T> clazz) throws CborConversionException
Unlike 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
clazz - 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 abstract CborObject copy()
CborObjectpublic final byte[] toCborByteArray()
Depending on your needs it may be more appropriate to directly use CborWriter,
which allows for the use of OutputStream and ByteBuffer objects.
CborWriterpublic String toString()
No additional whitespace is added for readability.
The output is similar to that of 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 ObjectString describing the data item.toString(int)public abstract String toString(int indentLevel)
The output is similar to that of 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.
indentLevel - the indentation level of the string to output. A negative value
indicates no newlines or whitespace should be added.String describing the data item.toString()Copyright © 2018–2023. All rights reserved.