{-# LANGUAGE CPP #-}
module Dhall.JSON.Compat (
objectFromList
, mapToAscList
, filterObject
, lookupObject
, traverseObjectWithKey
, objectKeys
, textToKey
) where
import Data.Aeson (Object, Value)
import Data.Text (Text)
#if MIN_VERSION_aeson(2,0,0)
import Data.Aeson.Key (Key)
import qualified Data.Aeson.Key as Key
import Data.Aeson.KeyMap (KeyMap)
import qualified Data.Aeson.KeyMap as KeyMap
import Data.Bifunctor (first)
#else
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List as List
#endif
objectFromList :: [(Text, Value)] -> Object
#if MIN_VERSION_aeson(2,0,0)
objectFromList :: [(Text, Value)] -> Object
objectFromList = forall v. [(Key, v)] -> KeyMap v
KeyMap.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Key
Key.fromText)
#else
objectFromList = HashMap.fromList
#endif
filterObject :: (Value -> Bool) -> Object -> Object
#if MIN_VERSION_aeson(2,0,0)
filterObject :: (Value -> Bool) -> Object -> Object
filterObject = forall v. (v -> Bool) -> KeyMap v -> KeyMap v
KeyMap.filter
#else
filterObject = HashMap.filter
#endif
#if MIN_VERSION_aeson(2,0,0)
mapToAscList :: KeyMap a -> [(Text, a)]
mapToAscList :: forall a. KeyMap a -> [(Text, a)]
mapToAscList = forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Key -> Text
Key.toText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [(Key, v)]
KeyMap.toAscList
#else
mapToAscList :: HashMap Text a -> [(Text, a)]
mapToAscList = List.sortOn fst . HashMap.toList
#endif
lookupObject :: Text -> Object -> Maybe Value
#if MIN_VERSION_aeson(2,0,0)
lookupObject :: Text -> Object -> Maybe Value
lookupObject Text
k = forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup (Text -> Key
Key.fromText Text
k)
#else
lookupObject = HashMap.lookup
#endif
objectKeys :: Object -> [Text]
#if MIN_VERSION_aeson(2,0,0)
objectKeys :: Object -> [Text]
objectKeys = forall a b. (a -> b) -> [a] -> [b]
map (Key -> Text
Key.toText) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v. KeyMap v -> [Key]
KeyMap.keys
#else
objectKeys = HashMap.keys
#endif
#if MIN_VERSION_aeson(2,0,0)
textToKey :: Text -> Key
textToKey :: Text -> Key
textToKey = Text -> Key
Key.fromText
#else
textToKey :: Text -> Text
textToKey = id
#endif
#if MIN_VERSION_aeson(2,0,0)
traverseObjectWithKey :: Applicative f => (Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseObjectWithKey :: forall (f :: * -> *) v1 v2.
Applicative f =>
(Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseObjectWithKey = forall (f :: * -> *) v1 v2.
Applicative f =>
(Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
KeyMap.traverseWithKey
#else
traverseObjectWithKey :: Applicative f => (Text -> v1 -> f v2) -> HashMap Text v1 -> f (HashMap Text v2)
traverseObjectWithKey = HashMap.traverseWithKey
#endif