mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 13:02:11 +03:00
970d69edd4
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4271 GitOrigin-RevId: 6990010bff622a424ca0bb9d24579bf121819fb0
67 lines
2.0 KiB
Haskell
67 lines
2.0 KiB
Haskell
module Data.Aeson.Extended
|
|
( FromJSONKeyValue (..),
|
|
ToJSONKeyValue (..),
|
|
FromJSONWithContext (..),
|
|
mapWithJSONPath,
|
|
encodeToStrictText,
|
|
(.=?),
|
|
|
|
-- * Re-exports
|
|
module Data.Aeson,
|
|
)
|
|
where
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
import Data.Aeson
|
|
import Data.Aeson.Text (encodeToTextBuilder)
|
|
import Data.Aeson.Types (JSONPathElement (..), Parser)
|
|
import Data.Functor.Const (getConst)
|
|
import Data.Text.Lazy (toStrict)
|
|
import Data.Text.Lazy.Builder (toLazyText)
|
|
import Hasura.Prelude
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
class ToJSONKeyValue a where
|
|
toJSONKeyValue :: a -> (Text, Value)
|
|
|
|
class FromJSONKeyValue a where
|
|
parseJSONKeyValue :: (Text, Value) -> Parser a
|
|
|
|
instance ToJSONKeyValue Void where
|
|
toJSONKeyValue = absurd
|
|
|
|
instance ToJSONKeyValue a => ToJSONKeyValue (Const a b) where
|
|
toJSONKeyValue = toJSONKeyValue . getConst
|
|
|
|
-- | Similar to 'FromJSON', except the parser can also source data with which
|
|
-- to construct 'a' from a context 'ctx'.
|
|
--
|
|
-- This can be useful if the 'a' value contains some data that is not from the
|
|
-- current piece of JSON (the 'Value'). For example, some data from higher
|
|
-- up in the overall JSON graph, or from some system context.
|
|
class FromJSONWithContext ctx a | a -> ctx where
|
|
parseJSONWithContext :: ctx -> Value -> Parser a
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- | An optional key-value pair for encoding a JSON object.
|
|
--
|
|
-- @
|
|
-- object $ ["foo" .= 0] <> catMaybes [ "bar" .=? Nothing, "baz" .=? 2 ]
|
|
-- @
|
|
(.=?) :: (ToJSON v, KeyValue kv) => Text -> Maybe v -> Maybe kv
|
|
(.=?) k = fmap (k .=)
|
|
{-# INLINE (.=?) #-}
|
|
|
|
infixr 8 .=?
|
|
|
|
-- | 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
|
|
|
|
encodeToStrictText :: ToJSON a => a -> Text
|
|
encodeToStrictText = toStrict . toLazyText . encodeToTextBuilder
|