aeson-combinators 0.0.5.0 → 0.1.0.0
raw patch · 6 files changed
+100/−22 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Aeson.Combinators.Decode: data Key
+ Data.Aeson.Combinators.Decode: data KeyMap v
+ Data.Aeson.Combinators.Decode: fromText :: Text -> Key
+ Data.Aeson.Combinators.Decode: keyMap :: Decoder a -> Decoder (KeyMap a)
+ Data.Aeson.Combinators.Decode: toHashMapText :: KeyMap v -> HashMap Text v
+ Data.Aeson.Combinators.Decode: toText :: Key -> Text
+ Data.Aeson.Combinators.Encode: data Key
+ Data.Aeson.Combinators.Encode: data KeyMap v
+ Data.Aeson.Combinators.Encode: fromText :: Text -> Key
+ Data.Aeson.Combinators.Encode: toHashMapText :: KeyMap v -> HashMap Text v
+ Data.Aeson.Combinators.Encode: toText :: Key -> Text
- Data.Aeson.Combinators.Decode: at :: [Text] -> Decoder a -> Decoder a
+ Data.Aeson.Combinators.Decode: at :: [Key] -> Decoder a -> Decoder a
- Data.Aeson.Combinators.Decode: key :: Text -> Decoder a -> Decoder a
+ Data.Aeson.Combinators.Decode: key :: Key -> Decoder a -> Decoder a
- Data.Aeson.Combinators.Encode: field :: Text -> Encoder b -> (a -> b) -> KeyValueEncoder a
+ Data.Aeson.Combinators.Encode: field :: Key -> Encoder b -> (a -> b) -> KeyValueEncoder a
- Data.Aeson.Combinators.Encode: field' :: Text -> Encoder a -> a -> (Text, Value)
+ Data.Aeson.Combinators.Encode: field' :: Key -> Encoder a -> a -> (Key, Value)
Files
- DocTest.hs +4/−2
- aeson-combinators.cabal +7/−8
- lib/Data/Aeson/Combinators/Compat.hs +67/−0
- lib/Data/Aeson/Combinators/Decode.hs +16/−7
- lib/Data/Aeson/Combinators/Encode.hs +6/−4
- test/JSONDecodeSpec.hs +0/−1
DocTest.hs view
@@ -1,6 +1,8 @@ import Test.DocTest-main = doctest [ "-isrc"- , "-XOverloadedStrings"+main = doctest [ "-XOverloadedStrings"+ , "-XCPP"+ , "-XLambdaCase"+ , "lib/Data/Aeson/Combinators/Compat.hs" , "lib/Data/Aeson/Combinators/Decode.hs" , "lib/Data/Aeson/Combinators/Encode.hs" ]
aeson-combinators.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: aeson-combinators-version: 0.0.5.0+version: 0.1.0.0 synopsis: Aeson combinators for dead simple JSON decoding description: Low overhead value space `Decoder`@@ -18,12 +18,11 @@ extra-source-files: README.md , CHANGELOG.md homepage: https://github.com/turboMaCk/aeson-combinators-tested-with: GHC == 8.4.4- , GHC == 8.6.5- , GHC == 8.8.3- , GHC == 8.8.4- , GHC == 8.10.1- , GHC == 8.10.3+tested-with: GHC == 8.8.4+ , GHC == 8.10.7+ , GHC == 9.0.2+ , GHC == 9.2.1+ , GHC == 9.2.2 , GHCJS == 8.6.0.1 Flag doctest@@ -33,7 +32,7 @@ library exposed-modules: Data.Aeson.Combinators.Decode , Data.Aeson.Combinators.Encode- -- other-modules:+ other-modules: Data.Aeson.Combinators.Compat -- other-extensions: build-depends: base >= 4 && < 5 , bytestring
+ lib/Data/Aeson/Combinators/Compat.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE CPP #-}+-- |+-- Module : Data.Aeson.Combinators.Decode+-- Copyright : (c) Marek Fajkus+-- License : BSD3+--+-- Maintainer : marek.faj@gmail.com+--+-- Aeson compatibility layer to support Aeson 2.0 and older versions.+-- Re-exposes 'Key' and 'KeyMap', together with suitable conversion functions.+-- For older aeson versions, we provide type definitions for 'Key' and+-- 'KeyMap'.+--+-- Users may use 'fromText' and 'toText' to write decoders/encoders for+-- forwards and backwards compatibility.+--+module Data.Aeson.Combinators.Compat (+-- * Aeson compatibility helpers+--+-- $doc+--+-- ** KeyMap+ KeyMap+ , toHashMapText+-- ** Key+ , Key+ , toText, fromText+) where++#if (MIN_VERSION_aeson(2,0,0))+import Data.Aeson.Key (Key, toText, fromText)+import Data.Aeson.KeyMap (KeyMap)+import Data.Aeson.KeyMap (toHashMapText)+#else+import qualified Data.HashMap.Lazy as HL+import Data.Text (Text)++-- | Forward compatible type-def for Aeson 2.0 'KeyMap' type.+type KeyMap a = HL.HashMap Text a++toHashMapText :: HL.HashMap Text a -> HL.HashMap Text a+toHashMapText = id++-- | Forward compatible type-def for Aeson 2.0 'Key' type.+type Key = Text++-- | Aeson 2.0 compatibility function for the 'Key' type.+fromText :: Text -> Text+fromText = id++-- | Aeson 2.0 compatibility function for the 'Key' type.+toText :: Text -> Text+toText = id+#endif++-- $doc+-- Aeson compatibility layer to support Aeson 2.0 and older versions.+-- Re-exposes 'Key' and 'KeyMap', together with suitable conversion functions.+-- For older aeson versions, we provide type definitions for 'Key' and+-- 'KeyMap'.+--+-- Users may use 'fromText' and 'toText' to write decoders/encoders for+-- forwards and backwards compatibility.+--+-- See [Key](https://hackage.haskell.org/package/aeson-2.0.3.0/docs/Data-Aeson-Key.html)+-- and [KeyMap](https://hackage.haskell.org/package/aeson-2.0.3.0/docs/Data-Aeson-KeyMap.html)+-- in [aeson >= 2.0](https://hackage.haskell.org/package/aeson) for more details.
lib/Data/Aeson/Combinators/Decode.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE LambdaCase #-} -- |--- Module : Data.Aeson.Cominators.Decode+-- Module : Data.Aeson.Combinators.Decode -- Copyright : (c) Marek Fajkus -- License : BSD3 --@@ -32,8 +32,8 @@ , nullable -- *** Sequences , list, vector--- *** Hashmap- , hashMapLazy, hashMapStrict+-- *** Hashmap and Map+ , hashMapLazy, hashMapStrict, keyMap -- *** Map , mapLazy, mapStrict -- * Combinators@@ -88,6 +88,7 @@ -- * Parsing (Running Decoders) , parseMaybe , parseEither+ , module Data.Aeson.Combinators.Compat ) where import Prelude hiding (either, fail, maybe)@@ -98,6 +99,7 @@ import Control.Monad.Fail (MonadFail (..)) import qualified Control.Monad.Fail as Fail +import Data.Aeson.Combinators.Compat import Data.Aeson.Internal (JSONPath, JSONPathElement (..)) import qualified Data.Aeson.Internal as AI import qualified Data.Aeson.Parser as Parser@@ -346,7 +348,7 @@ -- using provided 'Decoder'. hashMapLazy :: Decoder a -> Decoder (HL.HashMap Text a) hashMapLazy (Decoder d) = Decoder $ \case- Object xs -> traverse d xs+ Object xs -> toHashMapText <$> traverse d xs val -> typeMismatch "Array" val {-# INLINE hashMapLazy #-} @@ -355,10 +357,17 @@ -- using provided 'Decoder'. hashMapStrict :: Decoder a -> Decoder (HS.HashMap Text a) hashMapStrict (Decoder d) = Decoder $ \case- Object xs -> traverse d xs+ Object xs -> toHashMapText <$> traverse d xs val -> typeMismatch "Array" val {-# INLINE hashMapStrict #-} +-- | Decode JSON object to 'KeyMap' with 'Key' key+-- using provided 'Decoder'.+keyMap :: Decoder a -> Decoder (KeyMap a)+keyMap (Decoder d) = Decoder $ \case+ Object xs -> traverse d xs+ val -> typeMismatch "Array" val+{-# INLINE keyMap #-} -- | Decode JSON object to 'ML.Map' with 'Data.Text' key -- using provided 'Decoder'.@@ -399,7 +408,7 @@ -- -- >>> decode (key "data" int) "{\"data\": 42}" -- Just 42-key :: Text -> Decoder a -> Decoder a+key :: Key -> Decoder a -> Decoder a key t (Decoder d) = Decoder $ \case Object v -> d =<< v .: t val -> typeMismatch "Object" val@@ -410,7 +419,7 @@ -- -- >>> decode (at ["data", "value"] int) "{\"data\": {\"value\": 42}}" -- Just 42-at :: [Text] -> Decoder a -> Decoder a+at :: [Key] -> Decoder a -> Decoder a at pth d = foldr key d pth {-# INLINE at #-}
lib/Data/Aeson/Combinators/Encode.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE CPP #-} -- |--- Module : Data.Aeson.Cominators.Encode+-- Module : Data.Aeson.Combinators.Encode -- Copyright : (c) Marek Fajkus -- License : BSD3 --@@ -21,7 +21,7 @@ -- * Importing -- $importing --- * Aleternative to using 'Encode' Combinators+-- * Alternative to using 'Encode' Combinators -- $alternative -- * Example Usage@@ -71,6 +71,7 @@ -- * Evaluating Encoders , encode , toEncoding+ , module Data.Aeson.Combinators.Compat ) where import Control.Applicative@@ -79,6 +80,7 @@ import Data.Aeson (ToJSON, Value (..)) import qualified Data.Aeson as Aeson+import Data.Aeson.Combinators.Compat import qualified Data.Aeson.Encoding as E import Data.Aeson.Types (Pair) import qualified Data.ByteString.Lazy as BS@@ -285,7 +287,7 @@ {-| Define object field -}-field :: Text -> Encoder b -> (a -> b) -> KeyValueEncoder a+field :: Key -> Encoder b -> (a -> b) -> KeyValueEncoder a field name (Encoder enc) get v = (name, enc $ get v) {-# INLINE field #-} @@ -322,7 +324,7 @@ {-| Define object field (alternative) -}-field' :: Text -> Encoder a -> a -> (Text, Value)+field' :: Key -> Encoder a -> a -> (Key, Value) field' name (Encoder enc) val = (name, enc val) {-# INLINE field' #-}
test/JSONDecodeSpec.hs view
@@ -10,7 +10,6 @@ import Data.Aeson.Types (FromJSON (..)) import Data.ByteString.Lazy import Data.ByteString.Lazy.UTF8 (fromString)-import Data.Monoid ((<>)) import Data.Text import GHC.Generics import Test.Hspec