BC-DCBOR TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Interface CborDecodable<T>

    Interface for types that can be decoded from CBOR.

    This interface serves as a marker to indicate that a type supports being created from CBOR data.

    // Custom type that can be decoded from CBOR
    class Person implements CborDecodable<Person> {
    constructor(public name: string = '', public age: number = 0) {}

    static fromCbor(cbor: Cbor): Person {
    if (cbor.type !== MajorType.Map) {
    throw new Error('Expected a CBOR map');
    }
    const map = cbor.value as CborMap;
    const name = extractCbor(map.get(cbor('name'))!) as string;
    const age = extractCbor(map.get(cbor('age'))!) as number;
    return new Person(name, age);
    }

    tryFromCbor(cbor: Cbor): Person {
    return Person.fromCbor(cbor);
    }
    }

    // Parse from CBOR
    const cborMap = ...; // some CBOR map
    const person = Person.fromCbor(cborMap);
    interface CborDecodable<T> {
        tryFromCbor(cbor: Cbor): T;
    }

    Type Parameters

    • T

      The type being decoded

    Hierarchy (View Summary)

    Index

    Methods

    Methods

    • Try to create an instance from a CBOR value.

      Parameters

      • cbor: Cbor

        CBOR value to decode

      Returns T

      Decoded instance

      Error if decoding fails