dependency-graph icon indicating copy to clipboard operation
dependency-graph copied to clipboard

Graph serialization and deserialization

Open huypham50 opened this issue 1 year ago • 1 comments

Are there any suggestions on how to implement these two helpers?

const graph = new DepGraph() // adding nodes a, b, c, d, etc

const serializedGraph: Record<string, unknown> = serialize(graph) // unknown could be a specific type

const insertedRow = database.insert({ serializedGraph }) // putting serialized graph into a database using something like prisma or sequelize

const const deserializedGraph: DepGraph<T> = deserialize(insertedRow.serializedGraph) // T could be a specific type as well

Great library!

huypham50 avatar Feb 25 '24 00:02 huypham50

I've been hesitant to add any sort of official serialization/deserialization support to this library as it's possible to add data to the graph that is not serializable.

See this comment chain for more information https://github.com/jriecken/dependency-graph/commit/c9238dceba22188f1c74d025af37fea20744bc03#commitcomment-137327165

Ah, this wasn't an intentional change (although there's never really been a promise that the graph is JSON-serializable)

The only thing I worry about adding an official toJSON() / fromJSON(string) is that it's possible for someone who is using this library to add a node that has data attached that is not JSON.stringifyable (e.g. as you've shown a Map, or a Function, etc) and it's not clear what the output should be (fromJSON(graph.toJSON()) would not return the same thing)

Someone else had luck using this lib to serialize the graph - https://github.com/yahoo/serialize-javascript

jriecken avatar Apr 01 '24 20:04 jriecken