1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00

Define a ToJSON instance for Graph.

This commit is contained in:
Rob Rix 2018-05-11 12:22:11 -04:00
parent 38463e0712
commit 65259053f4

View File

@ -10,6 +10,7 @@ module Data.Graph
import qualified Algebra.Graph as G
import qualified Algebra.Graph.Class as Class
import Data.Aeson
import Data.Semilattice.Lower
import Prologue
@ -43,3 +44,10 @@ instance Ord vertex => Ord (Graph vertex) where
compare (Graph (G.Overlay _ _)) _ = LT
compare _ (Graph (G.Overlay _ _)) = GT
compare (Graph (G.Connect a1 a2)) (Graph (G.Connect b1 b2)) = (compare `on` Graph) a1 b1 <> (compare `on` Graph) a2 b2
instance (Ord vertex, ToJSON vertex) => ToJSON (Graph vertex) where
toJSON (Graph graph) = object [ "vertices" .= vertices, "edges" .= edges ]
where
vertices = toJSON (G.vertexList graph)
edges = fmap (\(a, b) -> object [ "source" .= a, "target" .= b ]) (G.edgeList graph)