2019-04-17 12:48:41 +03:00
|
|
|
module Data.Aeson.Extended
|
2021-09-24 01:56:37 +03:00
|
|
|
( module J,
|
|
|
|
encodeToStrictText,
|
|
|
|
ToJSONKeyValue (..),
|
|
|
|
FromJSONKeyValue (..),
|
2021-11-11 18:55:32 +03:00
|
|
|
mapWithJSONPath,
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
|
|
|
where
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson as J
|
|
|
|
import Data.Aeson.Text (encodeToTextBuilder)
|
2021-11-11 18:55:32 +03:00
|
|
|
import Data.Aeson.Types (JSONPathElement (..), Parser)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Functor.Const
|
|
|
|
import Data.Text.Lazy (toStrict)
|
|
|
|
import Data.Text.Lazy.Builder (toLazyText)
|
|
|
|
import Hasura.Prelude
|
2019-04-17 12:48:41 +03:00
|
|
|
|
|
|
|
encodeToStrictText :: (ToJSON a) => a -> Text
|
2021-08-17 13:21:56 +03:00
|
|
|
encodeToStrictText = toStrict . toLazyText . encodeToTextBuilder
|
2021-03-25 20:50:08 +03:00
|
|
|
|
|
|
|
class ToJSONKeyValue a where
|
|
|
|
toJSONKeyValue :: a -> (Text, J.Value)
|
|
|
|
|
|
|
|
class FromJSONKeyValue a where
|
|
|
|
parseJSONKeyValue :: (Text, J.Value) -> Parser a
|
|
|
|
|
|
|
|
instance ToJSONKeyValue Void where
|
|
|
|
toJSONKeyValue = absurd
|
|
|
|
|
|
|
|
instance ToJSONKeyValue a => ToJSONKeyValue (Const a b) where
|
|
|
|
toJSONKeyValue = toJSONKeyValue . getConst
|
2021-11-11 18:55:32 +03:00
|
|
|
|
|
|
|
-- | map a 'Parser' over a list, keeping the JSONPath context
|
|
|
|
mapWithJSONPath :: (a -> Parser b) -> [a] -> Parser [b]
|
|
|
|
mapWithJSONPath parser xs =
|
|
|
|
traverse (\(idx, item) -> parser item <?> Index idx) $ zip [0 ..] xs
|