Skip to content

Commit

Permalink
Support aeson-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonascarpay committed Oct 13, 2021
1 parent 71e4aba commit 967316a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions aeson-commit.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ description:
category: Text, Web, JSON
build-type: Simple
extra-source-files:
README.md
CHANGELOG.md
README.md

source-repository head
type: git
Expand All @@ -30,7 +30,7 @@ library
default-language: Haskell2010
ghc-options: -Wall -Wno-name-shadowing
build-depends:
aeson >=1.4 && <2
aeson >=1.4 && <2.1
, base >=4.10 && <5
, mtl >=2.2 && <3
, text >=1.2 && <2
Expand All @@ -42,7 +42,7 @@ test-suite tasty
default-language: Haskell2010
ghc-options: -Wall -Wno-name-shadowing
build-depends:
aeson >=1.4 && <2
aeson >=1.4 && <2.1
, aeson-commit
, aeson-qq >=0.8 && <1
, base >=4.10 && <5
Expand Down
12 changes: 11 additions & 1 deletion src/Data/Aeson/Commit.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE CPP #-}

{-|
Commitment mechanism for aeson 'Parser'.
Expand Down Expand Up @@ -40,8 +41,12 @@ module Data.Aeson.Commit
import Control.Applicative (Alternative (..))
import Control.Monad.Except
import Data.Aeson.Types
import Data.Text (Text)
import Data.Void (Void, absurd)
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key as Key
#else
import Data.Text (Text)
#endif

-- | A 'Parser' that has _two_ failure modes; recoverable and non-recoverable.
-- The default, recoverable failure is the equivalent to aeson's default 'Parser' behavior.
Expand Down Expand Up @@ -87,7 +92,12 @@ runCommit (Commit f) = runExceptT f >>= either handleErrors pure

-- | 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.

#if MIN_VERSION_aeson(2,0,0)
(.:>) :: FromJSON a => Object -> Key.Key -> (a -> Parser b) -> Commit b
#else
(.:>) :: FromJSON a => Object -> Text -> (a -> Parser b) -> Commit b
#endif
(o .:> k) cont = commit (o .: k) (\v -> cont v <?> Key k)

-- | Turn a 'Parser' into a 'Commit'
Expand Down

0 comments on commit 967316a

Please sign in to comment.