• @Endian decorator change the endianness dynamically of the decorated property or class and then set it back to the previous endianness once the decorated property/class has been fully processed.

    Type Parameters

    • This

      The type of the class the decorator is applied to.

    Parameters

    • endianness: BinaryCursorEndianness | (instance: This) => BinaryCursorEndianness

      The endianness to apply, or a function that return the endianness based on the instance value.

    • Optionalopt: Partial<PrePostOptions>

      Optional configution.

    Returns ClassAndPropertyDecoratorType<This>

    The class or property decorator function.

    Changing Endianness for individual properties

    Use the @Endian decorator over a PrimitiveSymbol property to change it's endianness.

    class Protocol {
    @Endian(BinaryCursorEndianness.LittleEndian)
    @Uint16
    little_endian: number

    @Uint32
    big_endian: number
    }

    Changing Endianness for subtypes

    By using the @Endian decorator over a subtype relation property to change the endianness of the entire sub-type

    class SubProtocol {
    @Uint32
    foo: number

    @Uint32
    bar: number
    }

    class Protocol {
    @Endian(BinaryCursorEndianness.LittleEndian)
    @Relation(SubProtocol)
    sub_type: SubProtocol

    @Uint32
    big_endian: number // Because the default endianness is big endian this value will be read as a big endian number
    }

    Runtime Conditional Endianness:

    If you need to set the endianness dynamically at runtime based on a class property, you can pass a function to the @Endian decorator.

    In the following example, the @Endian decorator uses the value of the _value property passed as an argument of Protocol constructor whether little-endian or big-endian byte order should be applied to the protocol.

    @Endian(_ => _value > 0 ? BinaryCursorEndianness.LittleEndian : BinaryCursorEndianness.BigEndian)
    class Protocol {
    _value: number

    @Uint32
    foo: number

    @Uint32
    bar: number

    constructor(value: number) {
    this._value = value
    }
    }
    • The @Endian decorator is used to change endianness dynamically or based on values known at runtime. If you want to describe the endianness of a protocol that won't change use LittleEndian and BigEndian.

    • Because of the current architecture of the library, if multiple @Endian that are based on values known at runtime are run in parallel it could potentially mess up the result because they will all populate the 'post' property metadata.