Skip to content

Deserialization isn't working properly #997

Open
@Nicocchi

Description

I've followed the example serialization code, and serialization works, but when I try to deserialize, if more than 2 ports are added, it just returns:
Uncaught TypeError: Cannot read porperties of null (reading 'getID') at DefaultNodeModel.ts:114:17
I'm not using any custom nodes or links, etc just DefaultNodeModel.

Stack:
React 18.2 + Vite 4.1 + Electron 23.2

Serialization

const data = currentModel.serialize();
// Send data to ipcRender to save to file
try {
    const res = await window.ipcRenderer.sendSync("save-project", {data });
} catch (error) {
    console.error(error);
}

Deserializaton

var model2 = new DiagramModel();
// I tried this since I saw a comment with looking at the programmatic demo
let obj: ReturnType<DiagramModel['serialize']> = JSON.parse(res);
model2.deserializeModel(obj, engine);
engine.setModel(model2);

// just sets my state's current model
setCurrentModel(model2);

Adding ports

 _.forEach(
    currentModel.getSelectedEntities(),

    (item: BaseModel<any>) => {
        item.addInPort(`${fname}`, true);
        item.addOutPort(`out-${item.getOutPorts().length + 1}`, true);
    }
);

engine.repaintCanvas();

I am also saving and opening the data to a JSON file

Saving

const data = JSON.stringify(arg.data);
fs.writeFile(`${result.filePath}.json`, data, (err) => {
    if (err) {
        console.log(err);
        return (_.returnValue = false);
    } else {
        console.log("File written successfully\n");
        console.log("The written has the following contents:");
        console.log(fs.readFileSync(`${result.filePath}.json`, "utf-8"));
    }
})

Opening

fs.readFile(res.filePaths[0], "utf-8", (err, data) => {
    if (err) {
        console.log(err);
        return (_.returnValue = null);
    } else {
        console.log("Contents of the readFile are:\n");
        console.log(data);
        return (_.returnValue = data);
    }
})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions