Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 785ba4d

Browse files
author
Patrick Thomson
committed
Include vertex IDs in JSON graph output and fix their format.
As reported in #189, the `edges` field of JSON graph output refers to information not reflected in the rest of the output, specifically the vertex IDs. This patch adds that information to the `ToJSON` instance for `ControlFlowVertex`. It also includes a `toEncoding` instance for a free speed boost. During this patch, I realized that, because `hash` tends to return a large number (since `Int` is 64-bit), we may run into errors when decoding JSON. One example hash is `3500157981503982114`; passing that to a JS engine's `Number.isSafeInteger` function returns false. The correct thing to do here is return ids as strings, which I have done. This is backwards-incompatible, but since this information was never properly exposed, the impact is negligable.
1 parent b4792db commit 785ba4d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Data/Graph.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,5 @@ instance (Ord vertex, ToJSON vertex, VertexTag vertex) => ToJSON (Graph vertex)
111111
newtype Edge vertex = Edge (vertex, vertex)
112112

113113
instance (ToJSON vertex, VertexTag vertex) => ToJSON (Edge vertex) where
114-
toJSON (Edge (a, b)) = object ["source" .= uniqueTag a, "target" .= uniqueTag b]
115-
toEncoding (Edge (a, b)) = pairs ("source" .= uniqueTag a <> "target" .= uniqueTag b)
114+
toJSON (Edge (a, b)) = object ["source" .= show (uniqueTag a), "target" .= show (uniqueTag b)]
115+
toEncoding (Edge (a, b)) = pairs ("source" .= show (uniqueTag a) <> "target" .= show (uniqueTag b))

src/Data/Graph/ControlFlowVertex.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ instance Lower ControlFlowVertex where lowerBound = Package ""
8686
instance VertexTag ControlFlowVertex where uniqueTag = hash . vertexIdentifier
8787

8888
instance ToJSON ControlFlowVertex where
89-
toJSON v = object [ "name" .= vertexIdentifier v, "type" .= vertexToType v ]
89+
toJSON v = object [ "name" .= vertexIdentifier v
90+
, "type" .= vertexToType v
91+
, "id" .= show (uniqueTag v)
92+
]
93+
toEncoding v = pairs $ mconcat [ "name" .= vertexIdentifier v
94+
, "type" .= vertexToType v
95+
, "id" .= show (uniqueTag v)
96+
]
9097

9198
-- TODO: This is potentially valuable just to get name's out of declarable things.
9299
-- Typeclasses to create 'ControlFlowVertex's from 'Term's. Also extracts

0 commit comments

Comments
 (0)