Encoder
Encoder<
TFrom
> =FixedSizeEncoder
<TFrom
> |VariableSizeEncoder
<TFrom
>
An object that can encode a value of type TFrom into a ReadonlyUint8Array.
An Encoder
can be either:
- A FixedSizeEncoder, where all encoded values have the same fixed size.
- A VariableSizeEncoder, where encoded values can vary in size.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
Examples
Encoding a value into a new byte array.
Writing the encoded value into an existing byte array.
Remarks
You may create Encoders
manually using the createEncoder function but it is more common
to compose multiple Encoders
together using the various helpers of the @solana/codecs
package.
For instance, here's how you might create an Encoder
for a Person
object type that contains
a name
string and an age
number:
Note that composed Encoder
types are clever enough to understand whether
they are fixed-size or variable-size. In the example above, getU32Encoder()
is
a fixed-size encoder, while addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())
is a variable-size encoder. This makes the final Person
encoder a variable-size encoder.