Decoder
Decoder<
TTo
> =FixedSizeDecoder
<TTo
> |VariableSizeDecoder
<TTo
>
An object that can decode a byte array into a value of type TTo.
An Decoder
can be either:
- A FixedSizeDecoder, where all byte arrays have the same fixed size.
- A VariableSizeDecoder, where byte arrays can vary in size.
Type Parameters
Type Parameter | Description |
---|---|
TTo | The type of the decoded value. |
Examples
Getting the decoded value from a byte array.
Reading the decoded value from a byte array at a specific offset and getting the offset of the next byte to read.
Remarks
You may create Decoders
manually using the createDecoder function but it is more common
to compose multiple Decoders
together using the various helpers of the @solana/codecs
package.
For instance, here's how you might create an Decoder
for a Person
object type that contains
a name
string and an age
number:
Note that composed Decoder
types are clever enough to understand whether
they are fixed-size or variable-size. In the example above, getU32Decoder()
is
a fixed-size decoder, while addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())
is a variable-size decoder. This makes the final Person
decoder a variable-size decoder.