    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE ViewPatterns #-}
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE CPP #-}
    module Data.Aeson.Config.Parser (
      Parser
    , runParser

    , typeMismatch
    , withObject
    , withText
    , withString
    , withArray
    , withNumber
    , withBool

    , explicitParseField
    , explicitParseFieldMaybe

    , Aeson.JSONPathElement(..)
    , (<?>)

    , Value(..)
    , Object
    , Array

    , liftParser

    , fromAesonPath
    , formatPath

    , markDeprecated
    ) where

    import           Imports

    import qualified Control.Monad.Fail as Fail
    import           Control.Monad.Trans.Class
    import           Control.Monad.Trans.Writer
    import           Data.Scientific
    import           Data.Set (Set, notMember)
    import qualified Data.Set as Set
    import qualified Data.Text as T
    import qualified Data.Vector as V
    import           Data.Aeson.Config.Key (Key)
    import qualified Data.Aeson.Config.Key as Key
    import qualified Data.Aeson.Config.KeyMap as KeyMap
    import           Data.Aeson.Types (Value(..), Object, Array)
    import qualified Data.Aeson.Types as Aeson
    import           Data.Aeson.Internal (IResult(..), iparse)
    #if !MIN_VERSION_aeson(1,4,5)
    import qualified Data.Aeson.Internal as Aeson
    #endif

    -- This is needed so that we have an Ord instance for aeson < 1.2.4.
    data JSONPathElement = Key Text | Index Int
      deriving (JSONPathElement -> JSONPathElement -> Bool
    (JSONPathElement -> JSONPathElement -> Bool)
    -> (JSONPathElement -> JSONPathElement -> Bool)
    -> Eq JSONPathElement
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: JSONPathElement -> JSONPathElement -> Bool
    == :: JSONPathElement -> JSONPathElement -> Bool
    $c/= :: JSONPathElement -> JSONPathElement -> Bool
    /= :: JSONPathElement -> JSONPathElement -> Bool
    Eq, Int -> JSONPathElement -> ShowS
    JSONPath -> ShowS
    JSONPathElement -> [Char]
    (Int -> JSONPathElement -> ShowS)
    -> (JSONPathElement -> [Char])
    -> (JSONPath -> ShowS)
    -> Show JSONPathElement
    forall a.
    (Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> JSONPathElement -> ShowS
    showsPrec :: Int -> JSONPathElement -> ShowS
    $cshow :: JSONPathElement -> [Char]
    show :: JSONPathElement -> [Char]
    $cshowList :: JSONPath -> ShowS
    showList :: JSONPath -> ShowS
    Show, Eq JSONPathElement
    Eq JSONPathElement
    -> (JSONPathElement -> JSONPathElement -> Ordering)
    -> (JSONPathElement -> JSONPathElement -> Bool)
    -> (JSONPathElement -> JSONPathElement -> Bool)
    -> (JSONPathElement -> JSONPathElement -> Bool)
    -> (JSONPathElement -> JSONPathElement -> Bool)
    -> (JSONPathElement -> JSONPathElement -> JSONPathElement)
    -> (JSONPathElement -> JSONPathElement -> JSONPathElement)
    -> Ord JSONPathElement
    JSONPathElement -> JSONPathElement -> Bool
    JSONPathElement -> JSONPathElement -> Ordering
    JSONPathElement -> JSONPathElement -> JSONPathElement
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: JSONPathElement -> JSONPathElement -> Ordering
    compare :: JSONPathElement -> JSONPathElement -> Ordering
    $c< :: JSONPathElement -> JSONPathElement -> Bool
    < :: JSONPathElement -> JSONPathElement -> Bool
    $c<= :: JSONPathElement -> JSONPathElement -> Bool
    <= :: JSONPathElement -> JSONPathElement -> Bool
    $c> :: JSONPathElement -> JSONPathElement -> Bool
    > :: JSONPathElement -> JSONPathElement -> Bool
    $c>= :: JSONPathElement -> JSONPathElement -> Bool
    >= :: JSONPathElement -> JSONPathElement -> Bool
    $cmax :: JSONPathElement -> JSONPathElement -> JSONPathElement
    max :: JSONPathElement -> JSONPathElement -> JSONPathElement
    $cmin :: JSONPathElement -> JSONPathElement -> JSONPathElement
    min :: JSONPathElement -> JSONPathElement -> JSONPathElement
    Ord)

    type JSONPath = [JSONPathElement]

    data Path = Consumed JSONPath | Deprecated JSONPath JSONPath
      deriving (Path -> Path -> Bool
    (Path -> Path -> Bool) -> (Path -> Path -> Bool) -> Eq Path
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: Path -> Path -> Bool
    == :: Path -> Path -> Bool
    $c/= :: Path -> Path -> Bool
    /= :: Path -> Path -> Bool
    Eq, Eq Path
    Eq Path
    -> (Path -> Path -> Ordering)
    -> (Path -> Path -> Bool)
    -> (Path -> Path -> Bool)
    -> (Path -> Path -> Bool)
    -> (Path -> Path -> Bool)
    -> (Path -> Path -> Path)
    -> (Path -> Path -> Path)
    -> Ord Path
    Path -> Path -> Bool
    Path -> Path -> Ordering
    Path -> Path -> Path
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: Path -> Path -> Ordering
    compare :: Path -> Path -> Ordering
    $c< :: Path -> Path -> Bool
    < :: Path -> Path -> Bool
    $c<= :: Path -> Path -> Bool
    <= :: Path -> Path -> Bool
    $c> :: Path -> Path -> Bool
    > :: Path -> Path -> Bool
    $c>= :: Path -> Path -> Bool
    >= :: Path -> Path -> Bool
    $cmax :: Path -> Path -> Path
    max :: Path -> Path -> Path
    $cmin :: Path -> Path -> Path
    min :: Path -> Path -> Path
    Ord, Int -> Path -> ShowS
    [Path] -> ShowS
    Path -> [Char]
    (Int -> Path -> ShowS)
    -> (Path -> [Char]) -> ([Path] -> ShowS) -> Show Path
    forall a.
    (Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> Path -> ShowS
    showsPrec :: Int -> Path -> ShowS
    $cshow :: Path -> [Char]
    show :: Path -> [Char]
    $cshowList :: [Path] -> ShowS
    showList :: [Path] -> ShowS
    Show)

    fromAesonPath :: Aeson.JSONPath -> JSONPath
    fromAesonPath :: JSONPath -> JSONPath
    fromAesonPath = JSONPath -> JSONPath
    forall a. [a] -> [a]
    reverse (JSONPath -> JSONPath)
    -> (JSONPath -> JSONPath) -> JSONPath -> JSONPath
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (JSONPathElement -> JSONPathElement) -> JSONPath -> JSONPath
    forall a b. (a -> b) -> [a] -> [b]
    map JSONPathElement -> JSONPathElement
    fromAesonPathElement

    fromAesonPathElement :: Aeson.JSONPathElement -> JSONPathElement
    fromAesonPathElement :: JSONPathElement -> JSONPathElement
    fromAesonPathElement JSONPathElement
    e = case JSONPathElement
    e of
      Aeson.Key Key
    k -> Text -> JSONPathElement
    Key (Key -> Text
    Key.toText Key
    k)
      Aeson.Index Int
    n -> Int -> JSONPathElement
    Index Int
    n

    newtype Parser a = Parser {forall a. Parser a -> WriterT (Set Path) Parser a
    unParser :: WriterT (Set Path) Aeson.Parser a}
      deriving ((forall a b. (a -> b) -> Parser a -> Parser b)
    -> (forall a b. a -> Parser b -> Parser a) -> Functor Parser
    forall a b. a -> Parser b -> Parser a
    forall a b. (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall a b. (a -> b) -> Parser a -> Parser b
    fmap :: forall a b. (a -> b) -> Parser a -> Parser b
    $c<$ :: forall a b. a -> Parser b -> Parser a
    <$ :: forall a b. a -> Parser b -> Parser a
    Functor, Functor Parser
    Functor Parser
    -> (forall a. a -> Parser a)
    -> (forall a b. Parser (a -> b) -> Parser a -> Parser b)
    -> (forall a b c.
        (a -> b -> c) -> Parser a -> Parser b -> Parser c)
    -> (forall a b. Parser a -> Parser b -> Parser b)
    -> (forall a b. Parser a -> Parser b -> Parser a)
    -> Applicative Parser
    forall a. a -> Parser a
    forall a b. Parser a -> Parser b -> Parser a
    forall a b. Parser a -> Parser b -> Parser b
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
    forall (f :: * -> *).
    Functor f
    -> (forall a. a -> f a)
    -> (forall a b. f (a -> b) -> f a -> f b)
    -> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
    -> (forall a b. f a -> f b -> f b)
    -> (forall a b. f a -> f b -> f a)
    -> Applicative f
    $cpure :: forall a. a -> Parser a
    pure :: forall a. a -> Parser a
    $c<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
    <*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
    $cliftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
    liftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
    $c*> :: forall a b. Parser a -> Parser b -> Parser b
    *> :: forall a b. Parser a -> Parser b -> Parser b
    $c<* :: forall a b. Parser a -> Parser b -> Parser a
    <* :: forall a b. Parser a -> Parser b -> Parser a
    Applicative, Applicative Parser
    Applicative Parser
    -> (forall a. Parser a)
    -> (forall a. Parser a -> Parser a -> Parser a)
    -> (forall a. Parser a -> Parser [a])
    -> (forall a. Parser a -> Parser [a])
    -> Alternative Parser
    forall a. Parser a
    forall a. Parser a -> Parser [a]
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *).
    Applicative f
    -> (forall a. f a)
    -> (forall a. f a -> f a -> f a)
    -> (forall a. f a -> f [a])
    -> (forall a. f a -> f [a])
    -> Alternative f
    $cempty :: forall a. Parser a
    empty :: forall a. Parser a
    $c<|> :: forall a. Parser a -> Parser a -> Parser a
    <|> :: forall a. Parser a -> Parser a -> Parser a
    $csome :: forall a. Parser a -> Parser [a]
    some :: forall a. Parser a -> Parser [a]
    $cmany :: forall a. Parser a -> Parser [a]
    many :: forall a. Parser a -> Parser [a]
    Alternative, Applicative Parser
    Applicative Parser
    -> (forall a b. Parser a -> (a -> Parser b) -> Parser b)
    -> (forall a b. Parser a -> Parser b -> Parser b)
    -> (forall a. a -> Parser a)
    -> Monad Parser
    forall a. a -> Parser a
    forall a b. Parser a -> Parser b -> Parser b
    forall a b. Parser a -> (a -> Parser b) -> Parser b
    forall (m :: * -> *).
    Applicative m
    -> (forall a b. m a -> (a -> m b) -> m b)
    -> (forall a b. m a -> m b -> m b)
    -> (forall a. a -> m a)
    -> Monad m
    $c>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
    >>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
    $c>> :: forall a b. Parser a -> Parser b -> Parser b
    >> :: forall a b. Parser a -> Parser b -> Parser b
    $creturn :: forall a. a -> Parser a
    return :: forall a. a -> Parser a
    Monad, Monad Parser
    Monad Parser -> (forall a. [Char] -> Parser a) -> MonadFail Parser
    forall a. [Char] -> Parser a
    forall (m :: * -> *).
    Monad m -> (forall a. [Char] -> m a) -> MonadFail m
    $cfail :: forall a. [Char] -> Parser a
    fail :: forall a. [Char] -> Parser a
    Fail.MonadFail)

    liftParser :: Aeson.Parser a -> Parser a
    liftParser :: forall a. Parser a -> Parser a
    liftParser = WriterT (Set Path) Parser a -> Parser a
    forall a. WriterT (Set Path) Parser a -> Parser a
    Parser (WriterT (Set Path) Parser a -> Parser a)
    -> (Parser a -> WriterT (Set Path) Parser a)
    -> Parser a
    -> Parser a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Parser a -> WriterT (Set Path) Parser a
    forall (m :: * -> *) a. Monad m => m a -> WriterT (Set Path) m a
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
    (MonadTrans t, Monad m) =>
    m a -> t m a
    lift

    runParser :: (Value -> Parser a) -> Value -> Either String (a, [String], [(String, String)])
    runParser :: forall a.
    (Value -> Parser a)
    -> Value -> Either [Char] (a, [[Char]], [([Char], [Char])])
    runParser Value -> Parser a
    p Value
    v = case (Value -> Parser (a, Set Path)) -> Value -> IResult (a, Set Path)
    forall a b. (a -> Parser b) -> a -> IResult b
    iparse (WriterT (Set Path) Parser a -> Parser (a, Set Path)
    forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
    runWriterT (WriterT (Set Path) Parser a -> Parser (a, Set Path))
    -> (Parser a -> WriterT (Set Path) Parser a)
    -> Parser a
    -> Parser (a, Set Path)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Parser a -> WriterT (Set Path) Parser a
    forall a. Parser a -> WriterT (Set Path) Parser a
    unParser (Parser a -> Parser (a, Set Path))
    -> (Value -> Parser a) -> Value -> Parser (a, Set Path)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Value -> Parser a
    p) Value
    v of
      IError JSONPath
    path [Char]
    err -> [Char] -> Either [Char] (a, [[Char]], [([Char], [Char])])
    forall a b. a -> Either a b
    Left ([Char]
    "Error while parsing " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ JSONPath -> [Char]
    formatPath (JSONPath -> JSONPath
    fromAesonPath JSONPath
    path) [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    " - " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    err)
      ISuccess (a
    a, Set Path
    paths) -> (a, [[Char]], [([Char], [Char])])
    -> Either [Char] (a, [[Char]], [([Char], [Char])])
    forall a b. b -> Either a b
    Right (a
    a, (JSONPath -> [Char]) -> [JSONPath] -> [[Char]]
    forall a b. (a -> b) -> [a] -> [b]
    map JSONPath -> [Char]
    formatPath (Set Path -> Value -> [JSONPath]
    determineUnconsumed Set Path
    paths Value
    v), [(JSONPath -> [Char]
    formatPath JSONPath
    name, JSONPath -> [Char]
    formatPath JSONPath
    substitute) | Deprecated JSONPath
    name JSONPath
    substitute <- Set Path -> [Path]
    forall a. Set a -> [a]
    Set.toList Set Path
    paths])

    formatPath :: JSONPath -> String
    formatPath :: JSONPath -> [Char]
    formatPath = [Char] -> JSONPath -> [Char]
    go [Char]
    "$" (JSONPath -> [Char])
    -> (JSONPath -> JSONPath) -> JSONPath -> [Char]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . JSONPath -> JSONPath
    forall a. [a] -> [a]
    reverse
      where
        go :: String -> JSONPath -> String
        go :: [Char] -> JSONPath -> [Char]
    go [Char]
    acc JSONPath
    path = case JSONPath
    path of
          [] -> [Char]
    acc
          Index Int
    n : JSONPath
    xs -> [Char] -> JSONPath -> [Char]
    go ([Char]
    acc [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    "[" [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Char]
    forall a. Show a => a -> [Char]
    show Int
    n [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    "]") JSONPath
    xs
          Key Text
    key : JSONPath
    xs -> [Char] -> JSONPath -> [Char]
    go ([Char]
    acc [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    "." [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Text -> [Char]
    T.unpack Text
    key) JSONPath
    xs

    determineUnconsumed :: Set Path -> Value -> [JSONPath]
    determineUnconsumed :: Set Path -> Value -> [JSONPath]
    determineUnconsumed ((Set Path -> Set Path -> Set Path
    forall a. Semigroup a => a -> a -> a
    <> Path -> Set Path
    forall a. a -> Set a
    Set.singleton (JSONPath -> Path
    Consumed [])) -> Set Path
    consumed) = Set JSONPath -> [JSONPath]
    forall a. Set a -> [a]
    Set.toList (Set JSONPath -> [JSONPath])
    -> (Value -> Set JSONPath) -> Value -> [JSONPath]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Writer (Set JSONPath) () -> Set JSONPath
    forall w a. Writer w a -> w
    execWriter (Writer (Set JSONPath) () -> Set JSONPath)
    -> (Value -> Writer (Set JSONPath) ()) -> Value -> Set JSONPath
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . JSONPath -> Value -> Writer (Set JSONPath) ()
    go []
      where
        go :: JSONPath -> Value -> Writer (Set JSONPath) ()
        go :: JSONPath -> Value -> Writer (Set JSONPath) ()
    go JSONPath
    path Value
    value
          | JSONPath -> Path
    Consumed JSONPath
    path Path -> Set Path -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `notMember` Set Path
    consumed = Set JSONPath -> Writer (Set JSONPath) ()
    forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
    tell (JSONPath -> Set JSONPath
    forall a. a -> Set a
    Set.singleton JSONPath
    path)
          | Bool
    otherwise = case Value
    value of
              Number Scientific
    _ -> () -> Writer (Set JSONPath) ()
    forall a. a -> WriterT (Set JSONPath) Identity a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
              String Text
    _ -> () -> Writer (Set JSONPath) ()
    forall a. a -> WriterT (Set JSONPath) Identity a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
              Bool Bool
    _ -> () -> Writer (Set JSONPath) ()
    forall a. a -> WriterT (Set JSONPath) Identity a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
              Value
    Null -> () -> Writer (Set JSONPath) ()
    forall a. a -> WriterT (Set JSONPath) Identity a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
              Object Object
    o -> do
                [(Key, Value)]
    -> ((Key, Value) -> Writer (Set JSONPath) ())
    -> Writer (Set JSONPath) ()
    forall (t :: * -> *) (m :: * -> *) a b.
    (Foldable t, Monad m) =>
    t a -> (a -> m b) -> m ()
    forM_ (Object -> [(Key, Value)]
    forall v. KeyMap v -> [(Key, v)]
    KeyMap.toList Object
    o) (((Key, Value) -> Writer (Set JSONPath) ())
     -> Writer (Set JSONPath) ())
    -> ((Key, Value) -> Writer (Set JSONPath) ())
    -> Writer (Set JSONPath) ()
    forall a b. (a -> b) -> a -> b
    $ \ (Key -> Text
    Key.toText -> Text
    k, Value
    v) -> do
                  Bool -> Writer (Set JSONPath) () -> Writer (Set JSONPath) ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Text
    "_" Text -> Text -> Bool
    `T.isPrefixOf` Text
    k) (Writer (Set JSONPath) () -> Writer (Set JSONPath) ())
    -> Writer (Set JSONPath) () -> Writer (Set JSONPath) ()
    forall a b. (a -> b) -> a -> b
    $ do
                    JSONPath -> Value -> Writer (Set JSONPath) ()
    go (Text -> JSONPathElement
    Key Text
    k JSONPathElement -> JSONPath -> JSONPath
    forall a. a -> [a] -> [a]
    : JSONPath
    path) Value
    v
              Array Array
    xs -> do
                [(Int, Value)]
    -> ((Int, Value) -> Writer (Set JSONPath) ())
    -> Writer (Set JSONPath) ()
    forall (t :: * -> *) (m :: * -> *) a b.
    (Foldable t, Monad m) =>
    t a -> (a -> m b) -> m ()
    forM_ ([Int] -> [Value] -> [(Int, Value)]
    forall a b. [a] -> [b] -> [(a, b)]
    zip [Int
    0..] ([Value] -> [(Int, Value)]) -> [Value] -> [(Int, Value)]
    forall a b. (a -> b) -> a -> b
    $ Array -> [Value]
    forall a. Vector a -> [a]
    V.toList Array
    xs) (((Int, Value) -> Writer (Set JSONPath) ())
     -> Writer (Set JSONPath) ())
    -> ((Int, Value) -> Writer (Set JSONPath) ())
    -> Writer (Set JSONPath) ()
    forall a b. (a -> b) -> a -> b
    $ \ (Int
    n, Value
    v) -> do
                  JSONPath -> Value -> Writer (Set JSONPath) ()
    go (Int -> JSONPathElement
    Index Int
    n JSONPathElement -> JSONPath -> JSONPath
    forall a. a -> [a] -> [a]
    : JSONPath
    path) Value
    v

    (<?>) :: Parser a -> Aeson.JSONPathElement -> Parser a
    <?> :: forall a. Parser a -> JSONPathElement -> Parser a
    (<?>) (Parser (WriterT Parser (a, Set Path)
    p)) JSONPathElement
    e = do
      WriterT (Set Path) Parser a -> Parser a
    forall a. WriterT (Set Path) Parser a -> Parser a
    Parser (Parser (a, Set Path) -> WriterT (Set Path) Parser a
    forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
    WriterT (Parser (a, Set Path)
    p Parser (a, Set Path) -> JSONPathElement -> Parser (a, Set Path)
    forall a. Parser a -> JSONPathElement -> Parser a
    Aeson.<?> JSONPathElement
    e)) Parser a -> Parser () -> Parser a
    forall a b. Parser a -> Parser b -> Parser a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* JSONPathElement -> Parser ()
    markConsumed (JSONPathElement -> JSONPathElement
    fromAesonPathElement JSONPathElement
    e)

    markConsumed :: JSONPathElement -> Parser ()
    markConsumed :: JSONPathElement -> Parser ()
    markConsumed JSONPathElement
    e = do
      JSONPath
    path <- Parser JSONPath
    getPath
      WriterT (Set Path) Parser () -> Parser ()
    forall a. WriterT (Set Path) Parser a -> Parser a
    Parser (WriterT (Set Path) Parser () -> Parser ())
    -> WriterT (Set Path) Parser () -> Parser ()
    forall a b. (a -> b) -> a -> b
    $ Set Path -> WriterT (Set Path) Parser ()
    forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
    tell (Path -> Set Path
    forall a. a -> Set a
    Set.singleton (Path -> Set Path) -> (JSONPath -> Path) -> JSONPath -> Set Path
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . JSONPath -> Path
    Consumed (JSONPath -> Set Path) -> JSONPath -> Set Path
    forall a b. (a -> b) -> a -> b
    $ JSONPathElement
    e JSONPathElement -> JSONPath -> JSONPath
    forall a. a -> [a] -> [a]
    : JSONPath
    path)

    markDeprecated :: Key -> Key -> Parser ()
    markDeprecated :: Key -> Key -> Parser ()
    markDeprecated (Key -> Text
    Key.toText -> Text
    name) (Key -> Text
    Key.toText -> Text
    substitute) = do
      JSONPath
    path <- Parser JSONPath
    getPath
      WriterT (Set Path) Parser () -> Parser ()
    forall a. WriterT (Set Path) Parser a -> Parser a
    Parser (WriterT (Set Path) Parser () -> Parser ())
    -> WriterT (Set Path) Parser () -> Parser ()
    forall a b. (a -> b) -> a -> b
    $ Set Path -> WriterT (Set Path) Parser ()
    forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
    tell (Path -> Set Path
    forall a. a -> Set a
    Set.singleton (Path -> Set Path) -> Path -> Set Path
    forall a b. (a -> b) -> a -> b
    $ JSONPath -> JSONPath -> Path
    Deprecated (Text -> JSONPathElement
    Key Text
    name JSONPathElement -> JSONPath -> JSONPath
    forall a. a -> [a] -> [a]
    : JSONPath
    path) (Text -> JSONPathElement
    Key Text
    substitute JSONPathElement -> JSONPath -> JSONPath
    forall a. a -> [a] -> [a]
    : JSONPath
    path))

    getPath :: Parser JSONPath
    getPath :: Parser JSONPath
    getPath = Parser JSONPath -> Parser JSONPath
    forall a. Parser a -> Parser a
    liftParser (Parser JSONPath -> Parser JSONPath)
    -> Parser JSONPath -> Parser JSONPath
    forall a b. (a -> b) -> a -> b
    $ Parser JSONPath
    -> (JSONPath -> [Char] -> Parser JSONPath) -> Parser JSONPath
    forall a. Parser a -> (JSONPath -> [Char] -> Parser a) -> Parser a
    Aeson.parserCatchError Parser JSONPath
    forall a. Parser a
    forall (f :: * -> *) a. Alternative f => f a
    empty ((JSONPath -> [Char] -> Parser JSONPath) -> Parser JSONPath)
    -> (JSONPath -> [Char] -> Parser JSONPath) -> Parser JSONPath
    forall a b. (a -> b) -> a -> b
    $ \ JSONPath
    path [Char]
    _ -> JSONPath -> Parser JSONPath
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (JSONPath -> JSONPath
    fromAesonPath JSONPath
    path)

    explicitParseField :: (Value -> Parser a) -> Object -> Key -> Parser a
    explicitParseField :: forall a. (Value -> Parser a) -> Object -> Key -> Parser a
    explicitParseField Value -> Parser a
    p Object
    o Key
    key = case Key -> Object -> Maybe Value
    forall v. Key -> KeyMap v -> Maybe v
    KeyMap.lookup Key
    key Object
    o of
      Maybe Value
    Nothing -> [Char] -> Parser a
    forall a. [Char] -> Parser a
    forall (m :: * -> *) a. MonadFail m => [Char] -> m a
    fail ([Char] -> Parser a) -> [Char] -> Parser a
    forall a b. (a -> b) -> a -> b
    $ [Char]
    "key " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Key -> [Char]
    forall a. Show a => a -> [Char]
    show Key
    key [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    " not present"
      Just Value
    v  -> Value -> Parser a
    p Value
    v Parser a -> JSONPathElement -> Parser a
    forall a. Parser a -> JSONPathElement -> Parser a
    <?> Key -> JSONPathElement
    Aeson.Key Key
    key

    explicitParseFieldMaybe :: (Value -> Parser a) -> Object -> Key -> Parser (Maybe a)
    explicitParseFieldMaybe :: forall a. (Value -> Parser a) -> Object -> Key -> Parser (Maybe a)
    explicitParseFieldMaybe Value -> Parser a
    p Object
    o Key
    key = case Key -> Object -> Maybe Value
    forall v. Key -> KeyMap v -> Maybe v
    KeyMap.lookup Key
    key Object
    o of
      Maybe Value
    Nothing -> Maybe a -> Parser (Maybe a)
    forall a. a -> Parser a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Maybe a
    forall a. Maybe a
    Nothing
      Just Value
    v  -> a -> Maybe a
    forall a. a -> Maybe a
    Just (a -> Maybe a) -> Parser a -> Parser (Maybe a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Value -> Parser a
    p Value
    v Parser a -> JSONPathElement -> Parser a
    forall a. Parser a -> JSONPathElement -> Parser a
    <?> Key -> JSONPathElement
    Aeson.Key Key
    key

    typeMismatch :: String -> Value -> Parser a
    typeMismatch :: forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    expected = Parser a -> Parser a
    forall a. Parser a -> Parser a
    liftParser (Parser a -> Parser a) -> (Value -> Parser a) -> Value -> Parser a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    Aeson.typeMismatch [Char]
    expected

    withObject :: (Object -> Parser a) -> Value -> Parser a
    withObject :: forall a. (Object -> Parser a) -> Value -> Parser a
    withObject Object -> Parser a
    p (Object Object
    o) = Object -> Parser a
    p Object
    o
    withObject Object -> Parser a
    _ Value
    v = [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Object" Value
    v

    withText :: (Text -> Parser a) -> Value -> Parser a
    withText :: forall a. (Text -> Parser a) -> Value -> Parser a
    withText Text -> Parser a
    p (String Text
    s) = Text -> Parser a
    p Text
    s
    withText Text -> Parser a
    _ Value
    v = [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "String" Value
    v

    withString :: (String -> Parser a) -> Value -> Parser a
    withString :: forall a. ([Char] -> Parser a) -> Value -> Parser a
    withString [Char] -> Parser a
    p = (Text -> Parser a) -> Value -> Parser a
    forall a. (Text -> Parser a) -> Value -> Parser a
    withText ([Char] -> Parser a
    p ([Char] -> Parser a) -> (Text -> [Char]) -> Text -> Parser a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> [Char]
    T.unpack)

    withArray :: (Array -> Parser a) -> Value -> Parser a
    withArray :: forall a. (Array -> Parser a) -> Value -> Parser a
    withArray Array -> Parser a
    p (Array Array
    xs) = Array -> Parser a
    p Array
    xs
    withArray Array -> Parser a
    _ Value
    v = [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Array" Value
    v

    withNumber :: (Scientific -> Parser a) -> Value -> Parser a
    withNumber :: forall a. (Scientific -> Parser a) -> Value -> Parser a
    withNumber Scientific -> Parser a
    p (Number Scientific
    n) = Scientific -> Parser a
    p Scientific
    n
    withNumber Scientific -> Parser a
    _ Value
    v = [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Number" Value
    v

    withBool :: (Bool -> Parser a) -> Value -> Parser a
    withBool :: forall a. (Bool -> Parser a) -> Value -> Parser a
    withBool Bool -> Parser a
    p (Bool Bool
    b) = Bool -> Parser a
    p Bool
    b
    withBool Bool -> Parser a
    _ Value
    v = [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Boolean" Value
    v