AI-generated Key Takeaways
-
Serialization allows you to save the current state of your Blockly workspace (blocks, variables, etc.) into a text-based format, typically JSON, for later retrieval.
-
You can save the serialized workspace data to a storage location like local storage and then load it back into a workspace to restore its previous state.
-
Blockly provides APIs (
Blockly.serialization.workspaces.save
andBlockly.serialization.workspaces.load
) for easily serializing and deserializing workspace states.
Serialization is saving the state of your workspace so that it can be loaded back into the workspace later. You convert all of the data you need to save into a text-based format for easy storage.
We recommend serializing your workspace to JSON.
For more information, see Serialization.
Save
The following code snippet shows how to convert the state of your workspace to JSON for saving:
// Serialize the state.
const state = Blockly.serialization.workspaces.save(myWorkspace);
// Then you save the state, e.g. to local storage.
localStorage.setItem('workspace-state', state);
Load
The following code snippet shows how to load some saved state into a workspace:
// Get your saved state from somewhere, e.g. local storage.
const state = localStorage.getItem('workspace-state');
// Deserialize the state.
Blockly.serialization.workspaces.load(state, myWorkspace);
This creates all of your saved blocks, variables, and other elements in the workspace.