Skip to content

Save & Restore

Save the flow - nodes, edges, viewport and custom node data - as a versioned JSON document and load it back: to local storage with the canvas's ToJson and LoadJsonAsync, to a file with BlowSerializer and a BlowDocument. Loading never throws: every problem comes back with a code and the place in the file, and a React Flow object can be brought in and sent back.

Local storage FileDownload React Flow ExportEdit
Order placed
Pack
owner: ana, 2 h
Shipped
bit Blow

Try it

  • Move a node or press Add node, then Save; change the graph again and press Restore to put the saved graph and viewport back. Restore with nothing saved says so.
  • The preview under the canvas is the file Download would write: pan, drag or select and watch it change. Upload reads a file back - one you downloaded, or one you broke by hand.
  • Pick a Sample and press Load sample. The version 0 file is upgraded as it loads (its job node becomes a task with an owner); the others are refused, and the panel lists every issue with its severity, code and JSON path. A refused load leaves the graph as it was.
  • Import an object reads a React Flow toObject(); Export downloads the graph in that shape for a React Flow front end.
  • Add node with unmapped data adds a node carrying a DateTime, which no mapping says how to save: the preview shows the exception, the two downloads switch off and Save reports it. Select the node and press Delete to be able to save again.
  • Tick Save the selection, select a node and look for "selected": true in the preview; loading such a file selects it again.

How it works

  • ToJson(options) saves the graph and viewport as JSON, and LoadJsonAsync(json, options) reads it back: it replaces the graph in place, adopts the document's viewport when it has one, and records an undo step when the built-in history is on - but only when the document was read without errors.
  • Download builds a BlowDocument with ToDocument(), gives it an Id and Metadata of its own (a title and an author) and writes it with BlowSerializer.Serialize(document, options). Upload reads it with BlowSerializer.DeserializeAsync(stream, options), which stops at MaxBytes, and hands document.ToSnapshot() to RestoreAsync. The serializer needs no canvas, so the same code runs on a server.
  • Only persistent state is written, in a fixed order with defaults left out, so saving the same graph twice gives the same text. WriteIndented lays it out for reading, and IncludeSelection (off by default) adds which elements are selected.
  • Node data is written as it is when it is JSON already, a string, a number or a boolean; anything else needs a contract. MapNodeData("task", SaveRestoreJsonContext.Default.TaskInfo) registers a source-generated one for task nodes. Saving data of a type nothing maps throws a BlowSerializationException naming the element.
  • Reading never throws. A BlowImportResult holds the Document (null when nothing could be read), the Issues - each a BlowImportCodes value such as missing-property, invalid-value, data-not-read or unknown-property, with a Severity and a JSON Path - and Succeeded, true when there is a document and no error. Duplicate ids and edges to missing nodes are warnings, since the canvas can draw round them.
  • BlowImportLimits on options.Limits bound what is read. This page sets them far below the defaults - MaxStringLength 1,000 characters, MaxPortsPerNode 8, MaxDepth 16, MaxBytes 2 MB, MaxNodes 5,000, MaxEdges 10,000 - so the samples can trip them (string-too-long, too-many, too-deep).
  • CurrentVersion = 1 is this application's document version. A BlowMigration(0, ...) rewrites a version 0 document's JSON (renaming job nodes to task) before it is read, and SourceVersion says what the file was; a file from a newer version is refused with newer-version.
  • BlowReactFlowFormat.Import(json, options) reads { nodes, edges, viewport } into the same BlowImportResult, taking data.label as the label and style objects as CSS; BlowReactFlowFormat.Export(document, options) writes it back.
An unhandled error has occurred. Reload 🗙