The endianness to apply, or a function that return the endianness based on the instance value.
Optional
opt: Partial<PrePostOptions>Optional configution.
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)
@Relation(PrimitiveSymbol.u16)
little_endian: number
@Relation(PrimitiveSymbol.u32)
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 {
@Relation(PrimitiveSymbol.u32)
foo: number
@Relation(PrimitiveSymbol.u32)
bar: number
}
class Protocol {
@Endian(BinaryCursorEndianness.LittleEndian)
@Relation(SubProtocol)
sub_type: SubProtocol
}
Set the endianness of an entire type definition
It's also possible to apply @Endian
decorator on top of a class
declaration to change the endianness of all its properties.
In the following example, the Protocol
type definition that
include two unsigned 32bits integer: foo
and bar
, will use big-endian
byte order.
@Endian(BinaryCursorEndianness.BigEndian)
class Protocol {
@Relation(PrimitiveSymbol.u32)
foo: number
@Relation(PrimitiveSymbol.u32)
bar: 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
@Relation(PrimitiveSymbol.u32)
foo: number
@Relation(PrimitiveSymbol.u32)
bar: number
constructor(value: number) {
this._value = value
}
}
@Endian
decorator change the endianness of the decorated property or class. It allows you to control how binary data is serialized or deserialized, based endianness that define the byte order of 16 and 32 bits integers.