on master help sign in
Add (.:!) and partially revert d0414be92ee6bf5b4c057978955b89d111767dab
commit921b75948d22aa8d6f8d2f82340742e011b3910e authorOleg Grenrus <oleg.grenrus@iki.fi> dateSat 19 Sep 2015, 17:20 (11 years ago) parent4667ef1
5 files changed+23−9
Data/Aeson.hs+1 Data/Aeson/TH.hs+2−2 Data/Aeson/Types.hs+1 Data/Aeson/Types/Generic.hs+3−3 Data/Aeson/Types/Instances.hs+16−4
Data/Aeson.hs+1
@@ -78,6 +78,7 @@ module Data.Aeson
7878 , foldable
7979 , (.:)
8080 , (.:?)
81+ , (.:!)
8182 , (.!=)
8283 , object
8384 -- * Parsing
Data/Aeson/TH.hs+2−2
@@ -101,7 +101,7 @@ import Data.Aeson.Types ( Value(..), Parser
101101 , defaultTaggedObject
102102 )
103103import Data.Aeson.Types.Internal (Encoding(..))
104−import Control.Monad ( join, liftM2, return, mapM, fail )
104+import Control.Monad ( liftM2, return, mapM, fail )
105105import Data.Bool ( Bool(False, True), otherwise, (&&), not )
106106import Data.Either ( Either(Left, Right) )
107107import Data.Eq ( (==) )
@@ -960,7 +960,7 @@ instance OVERLAPPABLE_ (FromJSON a) => LookupField a where
960960 Just v -> parseJSON v
961961
962962instance (FromJSON a) => LookupField (Maybe a) where
963− lookupField _ _ obj key = join <$> obj .:? key
963+ lookupField _ _ = (.:?)
964964
965965unknownFieldFail :: String -> String -> String -> Parser fail
966966unknownFieldFail tName rec key =
Data/Aeson/Types.hs+1
@@ -55,6 +55,7 @@ module Data.Aeson.Types
5555 , foldable
5656 , (.:)
5757 , (.:?)
58+ , (.:!)
5859 , (.!=)
5960 , object
6061
Data/Aeson/Types/Generic.hs+3−3
@@ -22,7 +22,7 @@
2222module Data.Aeson.Types.Generic ( ) where
2323
2424import Control.Applicative ((<|>))
25−import Control.Monad ((<=<), join)
25+import Control.Monad ((<=<))
2626import Control.Monad.ST (ST)
2727import Data.Aeson.Encode.Builder (emptyArray_)
2828import Data.Aeson.Encode.Functions (builder)
@@ -665,8 +665,8 @@ instance (Selector s, GFromJSON a) => FromRecord (S1 s a) where
665665
666666instance OVERLAPPING_ (Selector s, FromJSON a) =>
667667 FromRecord (S1 s (K1 i (Maybe a))) where
668− parseRecord _ (Just lab) obj = (M1 . K1) . join <$> obj .:? lab
669− parseRecord opts Nothing obj = (M1 . K1) . join <$> obj .:? pack label
668+ parseRecord _ (Just lab) obj = (M1 . K1) <$> obj .:? lab
669+ parseRecord opts Nothing obj = (M1 . K1) <$> obj .:? pack label
670670 where
671671 label = fieldLabelModifier opts $
672672 selName (undefined :: t s (K1 i (Maybe a)) p)
Data/Aeson/Types/Instances.hs+16−4
@@ -52,6 +52,7 @@ module Data.Aeson.Types.Instances
5252 , ifromJSON
5353 , (.:)
5454 , (.:?)
55+ , (.:!)
5556 , (.!=)
5657 , tuple
5758 , (>*<)
@@ -1533,7 +1534,7 @@ ifromJSON = iparse parseJSON
15331534--
15341535-- This accessor is appropriate if the key and value /must/ be present
15351536-- in an object for it to be valid. If the key and value are
1536−-- optional, use '(.:?)' instead.
1537+-- optional, use '.:?' instead.
15371538(.:) :: (FromJSON a) => Object -> Text -> Parser a
15381539obj .: key = case H.lookup key obj of
15391540 Nothing -> fail $ "key " ++ show key ++ " not present"
@@ -1549,23 +1550,34 @@ obj .: key = case H.lookup key obj of
15491550--
15501551-- This accessor is most useful if the key and value can be absent
15511552-- from an object without affecting its validity. If the key and
1552−-- value are mandatory, use '(.:)' instead.
1553+-- value are mandatory, use '.:' instead.
15531554(.:?) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
15541555obj .:? key = case H.lookup key obj of
15551556 Nothing -> pure Nothing
15561557 Just v -> modifyFailure addKeyName
1557− $ Just <$> parseJSON v <?> Key key
1558+ $ parseJSON v <?> Key key
15581559 where
15591560 addKeyName = (("failed to parse field " <> unpack key <> ": ") <>)
15601561{-# INLINE (.:?) #-}
15611562
1563+-- | Like '.:?', but the resulting parser will fail,
1564+-- if the key is present but is 'Null'.
1565+(.:!) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
1566+obj .:! key = case H.lookup key obj of
1567+ Nothing -> pure Nothing
1568+ Just v -> modifyFailure addKeyName
1569+ $ Just <$> parseJSON v <?> Key key
1570+ where
1571+ addKeyName = (("failed to parse field " <> unpack key <> ": ") <>)
1572+{-# INLINE (.:!) #-}
1573+
15621574-- | Helper for use in combination with '.:?' to provide default
15631575-- values for optional JSON object fields.
15641576--
15651577-- This combinator is most useful if the key and value can be absent
15661578-- from an object without affecting its validity and we know a default
15671579-- value to assign in that case. If the key and value are mandatory,
1568−-- use '(.:)' instead.
1580+-- use '.:' instead.
15691581--
15701582-- Example usage:
15711583--