on master help sign in
Added functions for inspecting Values
commit79b2601190829a77b58452797e2503f2d2ad2090 authorBas van Dijk <v.dijk.bas@gmail.com> dateSun 28 Oct 2012, 19:14 (14 years ago) parent9e8c3c9
3 files changed+57
Data/Aeson.hs+6 Data/Aeson/Types.hs+8 Data/Aeson/Types/Class.hs+43
Data/Aeson.hs+6
@@ -28,6 +28,12 @@ module Data.Aeson
2828 , Result(..)
2929 , fromJSON
3030 , ToJSON(..)
31+ -- * Inspecting @'Value's@
32+ , withObject
33+ , withText
34+ , withArray
35+ , withNumber
36+ , withBool
3137 -- * Constructors and accessors
3238 , (.=)
3339 , (.:)
Data/Aeson/Types.hs+8
@@ -32,6 +32,14 @@ module Data.Aeson.Types
3232 , parseEither
3333 , parseMaybe
3434 , ToJSON(..)
35+
36+ -- * Inspecting @'Value's@
37+ , withObject
38+ , withText
39+ , withArray
40+ , withNumber
41+ , withBool
42+
3543 -- * Constructors and accessors
3644 , (.=)
3745 , (.:)
Data/Aeson/Types/Class.hs+43
@@ -30,6 +30,14 @@ module Data.Aeson.Types.Class
3030#endif
3131 -- * Types
3232 , DotNetTime(..)
33+
34+ -- * Inspecting @'Value's@
35+ , withObject
36+ , withText
37+ , withArray
38+ , withNumber
39+ , withBool
40+
3341 -- * Functions
3442 , fromJSON
3543 , (.:)
@@ -711,6 +719,41 @@ instance FromJSON a => FromJSON (Last a) where
711719 parseJSON = fmap Last . parseJSON
712720 {-# INLINE parseJSON #-}
713721
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+
714757-- | Construct a 'Pair' from a key and a value.
715758(.=) :: ToJSON a => Text -> a -> Pair
716759name .= value = (name, toJSON value)