1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 21:16:12 +03:00

Define toEncoding for Graph via a JSONEdge newtype.

This commit is contained in:
Rob Rix 2018-05-11 12:34:09 -04:00
parent 0287e7a36a
commit 3763f49eb8

View File

@ -47,5 +47,11 @@ instance Ord vertex => Ord (Graph vertex) where
instance (Ord vertex, ToJSON vertex) => ToJSON (Graph vertex) where instance (Ord vertex, ToJSON vertex) => ToJSON (Graph vertex) where
toJSON (Graph graph) = object [ "vertices" .= G.vertexList graph, "edges" .= (toEdge <$> G.edgeList graph) ] toJSON (Graph graph) = object ["vertices" .= G.vertexList graph, "edges" .= (JSONEdge <$> G.edgeList graph)]
where toEdge (a, b) = object [ "source" .= a, "target" .= b ] toEncoding (Graph graph) = pairs ("vertices" .= G.vertexList graph <> "edges" .= (JSONEdge <$> G.edgeList graph))
newtype JSONEdge vertex = JSONEdge (vertex, vertex)
instance ToJSON vertex => ToJSON (JSONEdge vertex) where
toJSON (JSONEdge (a, b)) = object ["source" .= a, "target" .= b]
toEncoding (JSONEdge (a, b)) = pairs ("source" .= a <> "target" .= b)