on master help sign in
Get rid of the (.:/) combinator.
commitd93baa191855b595c0bdd8f4e387b643e0432de8 authorBryan O'Sullivan <bos@serpentine.com> dateWed 30 Nov 2011, 21:45 (15 years ago) parenteb11f43
3 files changed+5−23
Data/Aeson.hs−1 Data/Aeson/Types.hs−1 Data/Aeson/Types/Class.hs+5−21
Data/Aeson.hs−1
@@ -32,7 +32,6 @@ module Data.Aeson
3232 , (.:)
3333 , (.:?)
3434 , (.!=)
35− , (.:/)
3635 , object
3736 -- * Parsing
3837 , json
Data/Aeson/Types.hs−1
@@ -36,7 +36,6 @@ module Data.Aeson.Types
3636 , (.:)
3737 , (.:?)
3838 , (.!=)
39− , (.:/)
4039 , object
4140 ) where
4241
Data/Aeson/Types/Class.hs+5−21
@@ -34,7 +34,6 @@ module Data.Aeson.Types.Class
3434 , (.:)
3535 , (.:?)
3636 , (.!=)
37− , (.:/)
3837 , (.=)
3938 , typeMismatch
4039 ) where
@@ -749,37 +748,22 @@ obj .:? key = case H.lookup key obj of
749748
750749-- | Helper for use in combination with '.:?' to provide default
751750-- values for optional JSON object fields.
751+--
752+-- This combinator is most useful if the key and value can be absent
753+-- from an object without affecting its validity and we know a default
754+-- value to assign in that case. If the key and value are mandatory,
755+-- use '(.:)' instead.
752756--
753757-- Example usage:
754758--
755759-- @ v1 <- o '.:?' \"opt_field_with_dfl\" .!= \"default_val\"
756760-- v2 <- o '.:' \"mandatory_field\"
757761-- v3 <- o '.:?' \"opt_field2\"
758−--
759−-- \-- alternative version of v1 using the '.:/' operator
760−-- v1' <- o '.:/' (\"opt_field_with_dfl\", \"default_val\")
761762-- @
762763(.!=) :: Parser (Maybe a) -> a -> Parser a
763764pmval .!= val = fromMaybe val <$> pmval
764765{-# INLINE (.!=) #-}
765766
766−-- | Retrieve the value associated with the given key of an 'Object'.
767−-- The result is a default value if the key is not present, or 'empty'
768−-- if the value cannot be converted to the desired type.
769−--
770−-- This accessor is most useful if the key and value can be absent
771−-- from an object without affecting its validity and we know a
772−-- default value to assign in that case. If the key and value
773−-- are mandatory, use '(.:)' instead.
774−--
775−-- See also '.!=' for an alternative way to represent optional fields
776−-- with defaulting.
777−(.:/) :: (FromJSON a) => Object -> (Text, a) -> Parser a
778−obj .:/ (key, val) = case H.lookup key obj of
779− Nothing -> pure val
780− Just v -> parseJSON v
781−{-# INLINE (.:/) #-}
782−
783767-- | Fail parsing due to a type mismatch, with a descriptive message.
784768typeMismatch :: String -- ^ The name of the type you are trying to parse.
785769 -> Value -- ^ The actual value encountered.