{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Agda.Interaction.JSON
( module Export
, EncodeTCM(..)
, obj, kind, kind'
, (.=)
, (@=), (#=)
) where
import Control.Monad as Export ((>=>), (<=<))
import Data.Aeson as Export hiding (Result(..), (.=))
import qualified Data.Aeson
import Data.Aeson.Types ( Pair )
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key as Key
#endif
import Data.Text (Text)
import GHC.Int (Int32)
import Agda.TypeChecking.Monad
import Agda.Utils.Pretty
import qualified Agda.Utils.FileName as File
import qualified Agda.Utils.Maybe.Strict as Strict
#if MIN_VERSION_aeson(2,0,0)
toKey :: Text -> Key
toKey :: Text -> Key
toKey = Text -> Key
Key.fromText
#else
type Key = Text
toKey :: Text -> Key
toKey = id
#endif
class EncodeTCM a where
encodeTCM :: a -> TCM Value
default encodeTCM :: ToJSON a => a -> TCM Value
encodeTCM = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Value
toJSON
obj :: [TCM Pair] -> TCM Value
obj :: [TCM Pair] -> TCM Value
obj = ([Pair] -> Value
object forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
(.=) :: ToJSON a => Text -> a -> Pair
.= :: forall a. ToJSON a => Text -> a -> Pair
(.=) = forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
(Data.Aeson..=) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Key
toKey
(#=) :: (ToJSON a) => Text -> TCM a -> TCM Pair
#= :: forall a. ToJSON a => Text -> TCM a -> TCM Pair
(#=) Text
key TCM a
boxed = do
a
value <- TCM a
boxed
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Text
key forall a. ToJSON a => Text -> a -> Pair
.= forall a. ToJSON a => a -> Value
toJSON a
value
(@=) :: (EncodeTCM a) => Text -> a -> TCM Pair
@= :: forall a. EncodeTCM a => Text -> a -> TCM Pair
(@=) Text
key a
value = do
Value
encoded <- forall a. EncodeTCM a => a -> TCM Value
encodeTCM a
value
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Text
key forall a. ToJSON a => Text -> a -> Pair
.= Value
encoded
kind :: Text -> [TCM Pair] -> TCM Value
kind :: Text -> [TCM Pair] -> TCM Value
kind Text
k = [TCM Pair] -> TCM Value
obj forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text
"kind" forall a. EncodeTCM a => Text -> a -> TCM Pair
@= Text -> Value
String Text
k) forall a. a -> [a] -> [a]
:)
kind' :: Text -> [Pair] -> Value
kind' :: Text -> [Pair] -> Value
kind' Text
k = [Pair] -> Value
object forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text
"kind" forall a. ToJSON a => Text -> a -> Pair
.= Text -> Value
String Text
k) forall a. a -> [a] -> [a]
:)
encodeListTCM :: EncodeTCM a => [a] -> TCM Value
encodeListTCM :: forall a. EncodeTCM a => [a] -> TCM Value
encodeListTCM = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a. EncodeTCM a => a -> TCM Value
encodeTCM forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => [a] -> Value
toJSONList
instance EncodeTCM a => EncodeTCM [a] where
encodeTCM :: [a] -> TCM Value
encodeTCM = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a. EncodeTCM a => a -> TCM Value
encodeTCM forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => [a] -> Value
toJSONList
instance {-# OVERLAPPING #-} EncodeTCM String
instance EncodeTCM Bool where
instance EncodeTCM Int where
instance EncodeTCM Int32 where
instance EncodeTCM Value where
instance EncodeTCM Doc where
instance ToJSON Doc where
toJSON :: Doc -> Value
toJSON = forall a. ToJSON a => a -> Value
toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> String
render
instance EncodeTCM a => EncodeTCM (Maybe a) where
encodeTCM :: Maybe a -> TCM Value
encodeTCM Maybe a
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return Value
Null
encodeTCM (Just a
a) = forall a. EncodeTCM a => a -> TCM Value
encodeTCM a
a
instance ToJSON File.AbsolutePath where
toJSON :: AbsolutePath -> Value
toJSON (File.AbsolutePath Text
path) = forall a. ToJSON a => a -> Value
toJSON Text
path
#if !(MIN_VERSION_aeson(1,5,3))
instance ToJSON a => ToJSON (Strict.Maybe a) where
toJSON (Strict.Just a) = toJSON a
toJSON Strict.Nothing = Null
#endif