d_bincode library

A Dart implementation of the Bincode binary serialization format, designed for efficient, compact, and optionally schema-less data exchange.

This library provides tools for encoding Dart objects into Bincode byte sequences and decoding those bytes back into Dart objects. It aims for high performance and compatibility with the Bincode specification found across other languages (like Rust's bincode crate).

Core Components & Utilities:

  • BincodeWriter: Serializes Dart types into Bincode bytes. Offers methods for various primitive types, strings, collections, optionals, and nested objects.
  • BincodeReader: Deserializes Bincode bytes back into Dart types, providing corresponding read methods for all supported types.
  • BincodeCodable, BincodeEncodable, BincodeDecodable: Interfaces for making custom classes serializable and deserializable.
  • BincodeWriterPool: Optimizes performance in high-frequency serialization scenarios by managing reusable BincodeWriter instances.
  • BitMask: A utility class for easily managing up to 8 boolean flags packed into a single byte (u8), useful for status fields or options.

Basic Usage

To serialize custom objects, implement the BincodeCodable interface on your class, defining the encode and decode methods. Then use BincodeWriter to serialize instances and BincodeReader to deserialize the resulting bytes.

import 'package:d_bincode/d_bincode.dart';

// 1. Implement BincodeCodable
class Point implements BincodeCodable {
  double x, y;

  Point(this.x, this.y);
  Point.empty() : x = 0, y = 0; // Needed for common decoding patterns

  @override
  void encode(BincodeWriter w) => w..writeF64(x)..writeF64(y);

  @override
  void decode(BincodeReader r) {
    x = r.readF64();
    y = r.readF64();
  }

  @override String toString() => 'Point($x, $y)';
}

void main() {
  // 2. Encode
  final writer = BincodeWriter();
  final originalPoint = Point(1.0, -2.5);
  originalPoint.encode(writer);
  final bytes = writer.toBytes();
  print('Encoded: $bytes');

  // 3. Decode
  final decodedPoint = Point.empty(); // Create instance
  decodedPoint.decode(BincodeReader(bytes)); // Populate from bytes
  print('Decoded: $decodedPoint'); // Output: Point(1.0, -2.5)
}

See individual classes (BincodeWriter, BincodeReader, BitMask, etc.) and interface documentation (BincodeCodable) for detailed information on supported types, configuration options, and advanced usage patterns.

Classes

BincodeCodable
Represents a type that can be both serialized to and deserialized from the Bincode binary format.
BincodeDecodable
Defines the contract for types that can be deserialized from the Bincode binary format using a BincodeReader.
BincodeEncodable
Defines the contract for types that can be serialized into the Bincode binary format using a BincodeWriter.
BincodeReader
A high-performance binary deserializer for Rust-compatible Bincode data.
BincodeWriter
A fast, flexible binary serializer for Rust-compatible Bincode data.
BincodeWriterPool
Manages reusable BincodeWriter instances to optimize frequent serialization.
BitMask
A utility class to manage 8 individual boolean flags packed within a single byte (u8).