Add JSON replacer/reviver functions
This commit is contained in:
1 file changed
+17
-10
+17
-10
@@ -77,15 +77,22 @@ export class Graph<vData, eData> {
|
|||||||
return new Path<vData, eData>(this, start)
|
return new Path<vData, eData>(this, start)
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON(
|
||||||
|
vertexReplacer: (this: Graph<vData, eData>, data: vData) => unknown = data => data,
|
||||||
|
edgeReplacer: (this: Graph<vData, eData>, data: eData) => unknown = data => data
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
"@graph": Graph.signature,
|
"@graph": Graph.signature,
|
||||||
v: Array.from(this.vertices.values()).map(val => val.representAsJSON()),
|
v: Array.from(this.vertices.values()).map(v => v.toJSON(vertexReplacer)),
|
||||||
e: Array.from(this.edges.values()).map(val => val.representAsJSON())
|
e: Array.from(this.edges.values()).map(e => e.toJSON(edgeReplacer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromJSON<vData, eData>(json: unknown) {
|
static fromJSON<vData, eData>(
|
||||||
|
json: unknown,
|
||||||
|
vertexReviver: (this: Graph<vData, eData>, json: unknown) => vData = data => data as vData,
|
||||||
|
edgeReviver: (this: Graph<vData, eData>, json: unknown) => eData = data => data as eData
|
||||||
|
) {
|
||||||
const G = new Graph<vData, eData>()
|
const G = new Graph<vData, eData>()
|
||||||
|
|
||||||
if (!json || typeof json !== 'object') throw Error("Failed to parse: Unexpected object!")
|
if (!json || typeof json !== 'object') throw Error("Failed to parse: Unexpected object!")
|
||||||
@@ -100,7 +107,7 @@ export class Graph<vData, eData> {
|
|||||||
if (!Array.isArray(V)) throw Error("Failed to parse: One of the vertices isn't a tuple!")
|
if (!Array.isArray(V)) throw Error("Failed to parse: One of the vertices isn't a tuple!")
|
||||||
const [uuid, data] = V as [UUID, vData]
|
const [uuid, data] = V as [UUID, vData]
|
||||||
if (typeof uuid != 'string') throw Error("Failed to parse: Unexpected UUID!")
|
if (typeof uuid != 'string') throw Error("Failed to parse: Unexpected UUID!")
|
||||||
G.createVertex(data, uuid)
|
G.createVertex(vertexReviver.call(G, data), uuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const E of obj.e) {
|
for (const E of obj.e) {
|
||||||
@@ -116,7 +123,7 @@ export class Graph<vData, eData> {
|
|||||||
if (!U) throw Error(`Vertex ${U} doesn't exist!`)
|
if (!U) throw Error(`Vertex ${U} doesn't exist!`)
|
||||||
if (!V) throw Error(`Vertex ${V} doesn't exist!`)
|
if (!V) throw Error(`Vertex ${V} doesn't exist!`)
|
||||||
|
|
||||||
G.createEdge(U, V, data, {
|
G.createEdge(U, V, edgeReviver.call(G, data), {
|
||||||
id: uuid,
|
id: uuid,
|
||||||
directed: isDirected
|
directed: isDirected
|
||||||
})
|
})
|
||||||
@@ -149,8 +156,8 @@ export class Vertex<VertexData, EdgeData> {
|
|||||||
|
|
||||||
delete() { this.graph.deleteVertex(this) }
|
delete() { this.graph.deleteVertex(this) }
|
||||||
|
|
||||||
representAsJSON() {
|
toJSON(replacer: (this: Graph<VertexData, EdgeData>, data: VertexData) => unknown = data => data) {
|
||||||
return [this.id, this.data]
|
return [this.id, replacer.call(this.graph, this.data)]
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
@@ -201,14 +208,14 @@ export class Edge<vData, eData> {
|
|||||||
|
|
||||||
delete() { this.graph.deleteEdge(this) }
|
delete() { this.graph.deleteEdge(this) }
|
||||||
|
|
||||||
representAsJSON() {
|
toJSON(replacer: (this: Graph<vData, eData>, data: eData) => unknown = data => data) {
|
||||||
// tuple!
|
// tuple!
|
||||||
return [
|
return [
|
||||||
this.id,
|
this.id,
|
||||||
this.u.id,
|
this.u.id,
|
||||||
this.v.id,
|
this.v.id,
|
||||||
this.directed,
|
this.directed,
|
||||||
this.data
|
replacer.call(this.graph, this.data)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user