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.
Press Tab to move between the nodes and edges of the diagram.
Press Enter or Space to select it, Shift with Enter to add it to the selection, the arrow keys to move the selected nodes, Delete to remove them and Escape to clear the selection. Press C to start a connection from it.
Press Enter or Space to select it, Delete to remove it and Escape to clear the selection. Press C to move its end to another node.
bit BlowTry 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
jobnode becomes ataskwith 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": truein the preview; loading such a file selects it again.
How it works
ToJson(options)saves the graph and viewport as JSON, andLoadJsonAsync(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
BlowDocumentwithToDocument(), gives it anIdandMetadataof its own (a title and an author) and writes it withBlowSerializer.Serialize(document, options). Upload reads it withBlowSerializer.DeserializeAsync(stream, options), which stops atMaxBytes, and handsdocument.ToSnapshot()toRestoreAsync. 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.
WriteIndentedlays it out for reading, andIncludeSelection(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 fortasknodes. Saving data of a type nothing maps throws aBlowSerializationExceptionnaming the element. - Reading never throws. A
BlowImportResultholds theDocument(null when nothing could be read), theIssues- each aBlowImportCodesvalue such asmissing-property,invalid-value,data-not-readorunknown-property, with aSeverityand a JSONPath- andSucceeded, 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. BlowImportLimitsonoptions.Limitsbound what is read. This page sets them far below the defaults -MaxStringLength1,000 characters,MaxPortsPerNode8,MaxDepth16,MaxBytes2 MB,MaxNodes5,000,MaxEdges10,000 - so the samples can trip them (string-too-long,too-many,too-deep).CurrentVersion = 1is this application's document version. ABlowMigration(0, ...)rewrites a version 0 document's JSON (renamingjobnodes totask) before it is read, andSourceVersionsays what the file was; a file from a newer version is refused withnewer-version.BlowReactFlowFormat.Import(json, options)reads{ nodes, edges, viewport }into the sameBlowImportResult, takingdata.labelas the label and style objects as CSS;BlowReactFlowFormat.Export(document, options)writes it back.