decode<T extends CborObject<Object?>> static method

T decode<T extends CborObject<Object?>>({
  1. List<int>? cborBytes,
  2. CborObject<Object?>? cborObject,
  3. String? cborHex,
})

Decode raw CBOR data and return it as T.

Accepts:

  • cborBytes: CBOR bytes
  • cborObject: existing CBOR object
  • cborHex: CBOR data encoded as hex string

Implementation

static T decode<T extends CborObject>({
  List<int>? cborBytes,
  CborObject? cborObject,
  String? cborHex,
}) {
  assert(
    cborBytes != null || cborObject != null || cborHex != null,
    "Either cborBytes, cborHex or cborObject must be provided",
  );

  if (cborObject == null) {
    cborBytes ??= BytesUtils.tryFromHexString(cborHex);
    if (cborBytes == null) {
      throw CborSerializableException.missingArguments;
    }

    try {
      cborObject = CborObject.fromCbor(cborBytes);
    } catch (_) {
      throw CborSerializableException.invalidCborEncodingBytes;
    }
  }

  if (cborObject is! T) {
    throw CborSerializableException.castingFailed<T>(cborObject);
  }

  return cborObject;
}