Packer extension type
A high-performance MessagePack serializer.
Packer uses a BinaryWriter from pro_binary to encode Dart objects
into the MessagePack binary format. It utilizes a pool-based buffer
strategy via BinaryWriterPool to minimize memory allocations.
Usage:
- Create a Packer (acquires a writer from the pool).
- Use pack to encode one or more objects.
- Call takeBytes to get the result and release the buffer back to the pool.
Example:
final packer = Packer();
packer.pack({'key': 'value', 'list': [1, 2, 3]});
final bytes = packer.takeBytes();
- on
-
- _PackerState
Constructors
Properties
- bytesWritten → int
-
The number of bytes currently written to the internal buffer.
no setter
Methods
-
appendRaw(
Uint8List bytes) → void -
Appends
bytesdirectly to the buffer without any encoding. -
dispose(
) → void - Releases internal resources back to the pool without returning any data.
-
pack(
dynamic value) → void -
Encodes
valueinto MessagePack format and writes it to the buffer. -
packArray(
Iterable? value) → void -
Packs a
valueas a MessagePack array. -
packArrayLength(
int count) → void -
Writes only the MessagePack array header for an array of
countelements (fixarray/array16/array32). -
packBinary(
Uint8List? value) → void -
Packs a binary
value. -
packBool(
bool? value) → void -
Packs a boolean
value. -
packDouble(
double? value) → void - Packs a double as a 64-bit float.
-
packExt(
int type, void builder(Packer)) → void - Packs a custom extension payload.
-
packFloat(
Float? value) → void - Packs a Float wrapper as a 32-bit float.
-
packInt(
int? value) → void -
Packs an integer
value. -
packMap(
Map? value) → void - Packs a Map as a MessagePack map.
-
packMapLength(
int count) → void -
Writes only the MessagePack map header for a map of
countentries (fixmap/map16/map32). -
packNull(
) → void -
Packs a
nullvalue. -
packRawExtension(
int type, Uint8List data) → void -
Writes a MessagePack ext format with the given
typeanddata. -
packString(
String? value) → void -
Packs a String
valueusing UTF-8 encoding. -
packTimestamp(
DateTime? value) → void -
Packs a DateTime
valueusing the standard MessagePack timestamp extension. -
takeBytes(
{bool copy = true, bool dispose = true}) → Uint8List - Returns the serialized bytes and releases the internal buffer back to the pool.
Static Methods
-
encode(
dynamic value, {EncodeExt? encodeExt, int initialBufferSize = 1024}) → Uint8List -
Encodes a single
valueto bytes, owning the full pool lifecycle. -
encodeAll(
Iterable values, {EncodeExt? encodeExt, int initialBufferSize = 1024}) → Uint8List -
Encodes a sequence of
valuesinto one buffer, owning the pool lifecycle.