-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Parse Aeson data with commitment -- -- Aeson parsers backtracks too much for some use cases. The commit -- parser forbids backtracking for already committed parses. @package aeson-commit @version 1.1 -- | Commitment mechanism for aeson parsers. This is comes up when you e.g. -- want to make a distinction between missing keys and malformed keys. As -- an example, this parser will look for a key "nested", and if -- present try to read the embedded "value", or alternatively -- look for a top level "value": -- --
-- parse o = (o .:> "nested") (withObject "nestedObj" (.: "value")) -- <|> tryParser (o .: "value") ---- --
-- { value: "foo", otherField: "bar" }
-- -> Right "foo"
--
-- { value: "foo", nested: { value: "bar" } }
-- -> Right "bar"
--
-- { value: "foo", nested: { bar: 9 } }
-- -> Left "Error in $.nested: key \"value\" not found"
--
-- { value: "foo", nested: 9 }
-- -> Left "Error in $.nested: parsing nestedObj failed, expected Object, but encountered Number"
--
-- {}
-- -> Left
-- "Error in $: No match,
-- - key \"value\" not found"
-- - key \"nested\" not found"
--
module Data.Aeson.Commit
-- | A Parser that has _two_ failure modes; recoverable and
-- non-recoverable. The default, recoverable failure is the equivalent to
-- aeson's default Parser behavior. The non-recoverable failure
-- mode is used to commit to a branch; to commit means that every
-- subsequent failure is non-recoverable.
--
-- You turn a commit back into a normal Parser using
-- runCommit. As an additional benefit, if no commit succeeded the
-- parser error message will contain all encountered errors.
--
-- The implementation works by capturing failure in either the
-- ExceptT or in the underlying Parser. The derived
-- Alternative instance will only recover from failures in the
-- ExceptT. This means that as soon as we successfully construct a
-- Right value, the Alternative considers the Commit
-- a success, even though the underlying parser might have failed. The
-- Void represents the guarantee that we only collect error
-- values.
newtype Commit a
Commit :: ExceptT [Parser Void] Parser a -> Commit a
[unCommit] :: Commit a -> ExceptT [Parser Void] Parser a
-- | Construct a commit. If the first parser succeeds, the Commit is
-- a success, and any failures in the inner action will be preserved.
commit :: Parser a -> (a -> Parser b) -> Commit b
-- | Recommended way of turning a Commit back into a regular
-- Parser.
runCommit :: Commit a -> Parser a
-- | Convenience wrapper around commit for when the commit is
-- checking whether a key is present in some object. If it is, it will
-- commit and append the key to the JSONPath of the inner context through
-- <?>, which will give nicer error messages.
(.:>) :: FromJSON a => Object -> Text -> (a -> Parser b) -> Commit b
-- | Try to parse with a Parser and commit if it parses
-- successfully. Unlike liftParser, the parser's failure is
-- recoverable.
--
-- -- tryParser empty <|> p = p --tryParser :: Parser a -> Commit a -- | Turn a Parser into a Commit. Unlike tryParser, -- the parser's failure is _not_ recoverable, i.e. the parse is always -- committed. -- --
-- liftParser empty <|> p = empty --liftParser :: Parser a -> Commit a instance GHC.Base.Alternative Data.Aeson.Commit.Commit instance GHC.Base.Applicative Data.Aeson.Commit.Commit instance GHC.Base.Functor Data.Aeson.Commit.Commit instance GHC.Base.Monad Data.Aeson.Commit.Commit