Skip to content

Serialization

Drawings are vector documents, not pictures. Draw on the left, then save, reload and export on the right. The compact format stores a handwriting point in a few bytes; the readable format is the same data as plain JSON for diffing and inspection. Edit what was saved, or open a file, to see the loader validate it.

Empty drawing canvas.
Save the drawing to see how it is stored.

The API

Saving is one call and so is loading. For anything that came from outside your own code - a file a person opened, a request body - use TryDeserialize, which reports a message instead of throwing. The UTF-8 and stream overloads avoid a string the size of the document when it goes to a file or over the network.

string compact = BinkSerializer.Serialize(document);
string readable = BinkSerializer.Serialize(document, BinkSerializerOptions.Readable);
byte[] utf8 = BinkSerializer.SerializeToUtf8Bytes(document);
await BinkSerializer.SerializeAsync(document, stream);
BinkDocument fromStream = await BinkSerializer.DeserializeAsync(stream);

if (BinkSerializer.TryDeserialize(json, out BinkDocument? loaded, out string? error) && loaded is not null)
{
    _canvas.LoadDocument(loaded);   // resets history and selection
}

Your own information travels with it: metadata on the document and on any element, and a Description that is also what screen readers and the exported SVG report.

document.SetMetadata("case-id", "2026-114");
document.ReplaceElement(stroke with
{
    Description = "Signature of Jane Doe",
    Metadata = new Dictionary<string, string> { ["signer"] = "jane" },
});

The format

Versioned JSON, a stable contract rather than a projection of the library's classes: every element has a type and only documented fields. Stroke points are quantized to 64 steps a document unit and delta-encoded as varints, so a handwriting point costs a few bytes against 30 or more as a JSON array; pressure is one byte, and tilt and twist are only written when the device produced them. The encoding is deterministic, so the same drawing always saves to the same bytes, which is what "Round trip identical" checks.

A picture shown by several elements is written once, in an assets table keyed by a hash of its content - try Add a picture twice, then save. A file with that table is version 2, which older builds refuse with a message rather than opening without its pictures; every other file is version 1. ShareImageData = false always writes version 1, and IncludeEmbeddedImages = false leaves the pictures out for an application that stores them elsewhere.

var options = new BinkSerializerOptions
{
    ShareImageData = false,           // version 1 even with repeated pictures, for older readers
    IncludeEmbeddedImages = false,    // pictures load as BinkImageSource.Omitted, to be re-attached
};

foreach (BinkImageElement image in loaded.AllElements.OfType<BinkImageElement>())
{
    if (image.Source.IsOmitted) loaded.ReplaceElement(image with { Source = LoadStoredImage(image.Id) });
}

Untrusted input

Loading is a parser's attack surface, so everything is validated: the input size before parsing, the number of layers, elements and points, every coordinate, brush widths, picture URIs (SVG pictures are opt-in, remote ones can be refused), font family names and transforms. Malformed input is refused with a message that says what is wrong, and a hostile size field never makes the reader allocate for it. Strict limits loads with options like these:

var options = new BinkDeserializerOptions
{
    Limits = new BinkDocumentLimits { MaxElements = 25, MaxPointsPerStroke = 20_000 },
    AllowRemoteImages = false,
    AllowSvgImages = false,
};
bool ok = BinkSerializer.TryDeserialize(json, out BinkDocument? document, out string? error, options);

Compatibility

The format carries a schema version. A document written by a newer version is refused with an explanation rather than being partially read. Elements of unknown types are preserved untouched through a load and save (PreserveUnknownElements), and a brush kind this build does not know draws like a pen with the stored colour and width while keeping its kind, so an old build never silently destroys work made by a new one. Unknown fields are ignored, so adding a field is not a breaking change. The extending page shows both from the other side, with an element type and a brush of its own read by an application that does not know them.

Unsaved changes

The line above the drawing reads IsDirty: whether anything changed since MarkSaved, which this page calls when it saves. Any change counts, an undo included, so undoing back to the saved state still reads as changed. For a save that takes time, pass the revision that was serialized, so what changed while the bytes were on their way stays unsaved.

long revision = document.Revision;
await SaveToServerAsync(BinkSerializer.SerializeToUtf8Bytes(document));
document.MarkSaved(revision);
An unhandled error has occurred. Reload 🗙