Skip to content

Commit

Permalink
Version 0.1.4.1. ToJSON and FromJSON instances. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemkondyukov authored and AlexKaneRUS committed Oct 31, 2018
1 parent 32625f8 commit d74f9b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.1.4.1] - 2018-10-31
### Added
- To/From JSON instances

## [0.1.4.0] - 2018-09-27

### Changed
Expand Down
3 changes: 2 additions & 1 deletion math-grads.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: math-grads
version: 0.1.4.0
version: 0.1.4.1
synopsis: Library containing graph data structures and graph algorithms
description: Library containing graph data structures and graph algorithms
.
Expand Down Expand Up @@ -37,6 +37,7 @@ library
, Math.Grads.Drawing.Internal.Sampling
, Math.Grads.Drawing.Internal.Utils
build-depends: base >= 4.7 && < 5
, aeson
, array
, containers
, either
Expand Down
16 changes: 13 additions & 3 deletions src/Math/Grads/GenericGraph.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ViewPatterns #-}

module Math.Grads.GenericGraph
( GenericGraph (..)
Expand All @@ -18,20 +20,28 @@ module Math.Grads.GenericGraph
) where

import Control.Arrow (first)
import Data.Aeson (FromJSON (..), ToJSON (..), defaultOptions,
genericParseJSON, genericToJSON)
import qualified Data.Array as A
import Data.List (find, groupBy, sortBy)
import Data.Map.Strict (Map, mapKeys, member, (!))
import qualified Data.Map.Strict as M
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.Set as S

import GHC.Generics (Generic)
import Math.Grads.Graph (Graph (..))

data GenericGraph v e = GenericGraph {
gIndex :: A.Array Int v,
gRevIndex :: Map v Int,
gAdjacency :: A.Array Int [(Int, e)]
}
} deriving (Generic)

instance (Ord v, Eq e, ToJSON v, ToJSON e) => ToJSON (GenericGraph v e) where
toJSON (toList -> l) = genericToJSON defaultOptions l

instance (Ord v, Eq e, FromJSON v, FromJSON e) => FromJSON (GenericGraph v e) where
parseJSON v = fromList <$> genericParseJSON defaultOptions v

instance Graph GenericGraph where
fromList :: (Ord v, Eq v) => ([v], [(Int, Int, e)]) -> GenericGraph v e
Expand Down

0 comments on commit d74f9b6

Please sign in to comment.