{-|
Module      : Functions for encoding Hasklepias Feature data
Description : Defines ToJSON instances for Features.
Copyright   : (c) NoviSci, Inc 2020
License     : BSD3
Maintainer  : bsaul@novisci.com
-}

{-# LANGUAGE OverloadedStrings #-}

module Hasklepias.Types.Feature.Aeson(
) where

import IntervalAlgebra              ( Interval, Intervallic(end, begin) )
import Hasklepias.Types.Feature     ( Feature(..)
                                    , MissingReason
                                    , FeatureData(..) )
import Data.Aeson                   ( object, KeyValue((.=)), ToJSON(toJSON) )

instance (ToJSON a, Ord a, Show a)=> ToJSON (Interval a) where
    toJSON :: Interval a -> Value
toJSON Interval a
x = [Pair] -> Value
object [Text
"begin" Text -> a -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
begin Interval a
x, Text
"end" Text -> a -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
end Interval a
x]

instance ToJSON MissingReason 

instance (ToJSON d)=> ToJSON (FeatureData d) where
    toJSON :: FeatureData d -> Value
toJSON  FeatureData d
x = case FeatureData d -> Either MissingReason d
forall d. FeatureData d -> Either MissingReason d
getFeatureData FeatureData d
x of 
      (Left MissingReason
l)  -> MissingReason -> Value
forall a. ToJSON a => a -> Value
toJSON MissingReason
l
      (Right d
r) -> d -> Value
forall a. ToJSON a => a -> Value
toJSON d
r

instance (Show b, ToJSON b, ToJSON d) => ToJSON (Feature b d) where
    toJSON :: Feature b d -> Value
toJSON Feature b d
x = [Pair] -> Value
object [ Text
"name"   Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Feature b d -> Text
forall b d. Show b => Feature b d -> Text
getName Feature b d
x
                       , Text
"attrs" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= b -> Value
forall a. ToJSON a => a -> Value
toJSON (Feature b d -> b
forall b d. Show b => Feature b d -> b
getAttr Feature b d
x)
                       , Text
"data"  Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= FeatureData d -> Value
forall a. ToJSON a => a -> Value
toJSON (Feature b d -> FeatureData d
forall b d. Show b => Feature b d -> FeatureData d
getData Feature b d
x) ]