on master help sign in
Added functions for inspecting Values
79b2601Bas van Dijk
Data/Aeson.hs+6
@@ -28,6 +28,12 @@ module Data.Aeson
28 , Result(..)
28 , Result(..)
29 , fromJSON
29 , fromJSON
30 , ToJSON(..)
30 , ToJSON(..)
31+ -- * Inspecting @'Value's@
32+ , withObject
33+ , withText
34+ , withArray
35+ , withNumber
36+ , withBool
31 -- * Constructors and accessors
37 -- * Constructors and accessors
32 , (.=)
38 , (.=)
33 , (.:)
39 , (.:)
Data/Aeson/Types.hs+8
@@ -32,6 +32,14 @@ module Data.Aeson.Types
32 , parseEither
32 , parseEither
33 , parseMaybe
33 , parseMaybe
34 , ToJSON(..)
34 , ToJSON(..)
35+
36+ -- * Inspecting @'Value's@
37+ , withObject
38+ , withText
39+ , withArray
40+ , withNumber
41+ , withBool
42+
35 -- * Constructors and accessors
43 -- * Constructors and accessors
36 , (.=)
44 , (.=)
37 , (.:)
45 , (.:)
Data/Aeson/Types/Class.hs+43
@@ -30,6 +30,14 @@ module Data.Aeson.Types.Class
30#endif
30#endif
31 -- * Types
31 -- * Types
32 , DotNetTime(..)
32 , DotNetTime(..)
33+
34+ -- * Inspecting @'Value's@
35+ , withObject
36+ , withText
37+ , withArray
38+ , withNumber
39+ , withBool
40+
33 -- * Functions
41 -- * Functions
34 , fromJSON
42 , fromJSON
35 , (.:)
43 , (.:)
@@ -711,6 +719,41 @@ instance FromJSON a => FromJSON (Last a) where
711 parseJSON = fmap Last . parseJSON
719 parseJSON = fmap Last . parseJSON
712 {-# INLINE parseJSON #-}
720 {-# INLINE parseJSON #-}
713
721
722+-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
723+-- and fails using @'typeMismatch' expected@ otherwise.
724+withObject :: String -> (Object -> Parser a) -> Value -> Parser a
725+withObject _ f (Object obj) = f obj
726+withObject expected _ v = typeMismatch expected v
727+{-# INLINE withObject #-}
728+
729+-- | @withObject expected f value@ applies @f@ to the 'Text' when @value@ is a @String@
730+-- and fails using @'typeMismatch' expected@ otherwise.
731+withText :: String -> (Text -> Parser a) -> Value -> Parser a
732+withText _ f (String txt) = f txt
733+withText expected _ v = typeMismatch expected v
734+{-# INLINE withText #-}
735+
736+-- | @withObject expected f value@ applies @f@ to the 'Array' when @value@ is an @Array@
737+-- and fails using @'typeMismatch' expected@ otherwise.
738+withArray :: String -> (Array -> Parser a) -> Value -> Parser a
739+withArray _ f (Array arr) = f arr
740+withArray expected _ v = typeMismatch expected v
741+{-# INLINE withArray #-}
742+
743+-- | @withObject expected f value@ applies @f@ to the 'Number' when @value@ is a @Number@
744+-- and fails using @'typeMismatch' expected@ otherwise.
745+withNumber :: String -> (Number -> Parser a) -> Value -> Parser a
746+withNumber _ f (Number num) = f num
747+withNumber expected _ v = typeMismatch expected v
748+{-# INLINE withNumber #-}
749+
750+-- | @withObject expected f value@ applies @f@ to the 'Bool' when @value@ is a @Bool@
751+-- and fails using @'typeMismatch' expected@ otherwise.
752+withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
753+withBool _ f (Bool arr) = f arr
754+withBool expected _ v = typeMismatch expected v
755+{-# INLINE withBool #-}
756+
714-- | Construct a 'Pair' from a key and a value.
757-- | Construct a 'Pair' from a key and a value.
715(.=) :: ToJSON a => Text -> a -> Pair
758(.=) :: ToJSON a => Text -> a -> Pair
716name .= value = (name, toJSON value)
759name .= value = (name, toJSON value)