on master help sign in
Generalise the type of the FromJSON parser.
commit6e370d2873f59b86fb40e2a266d18241452779ee authorBryan O'Sullivan <bos@mailrank.com> dateTue 22 Mar 2011, 10:13 (16 years ago) parent9213f47
1 file changed+58−9
Data/Aeson/Types.hs+58−9
Data/Aeson/Types.hs+58−9
@@ -28,6 +28,8 @@ module Data.Aeson.Types
2828 , FromJSON(..)
2929 , fromJSON
3030 , parse
31+ , parseEither
32+ , parseMaybe
3133 , ToJSON(..)
3234 -- * Constructors and accessors
3335 , (.=)
@@ -38,7 +40,7 @@ module Data.Aeson.Types
3840
3941import Control.Applicative
4042import Control.DeepSeq (NFData(..))
41−import Control.Monad (MonadPlus(..))
43+import Control.Monad (MonadPlus(..), ap)
4244import Data.Aeson.Functions
4345import Data.Attoparsec.Char8 (Number(..))
4446import Data.Data (Data)
@@ -71,17 +73,54 @@ data Result a = Error String
7173 | Success a
7274 deriving (Eq, Show, Typeable)
7375
74−-- | Failure continuation. Constructs an 'Error'.
75−type Failure r = String -> Result r
76−-- | Success continuation. Constructs a 'Success'.
77−type Success a r = a -> Result r
76+instance Functor Result where
77+ fmap f (Success a) = Success (f a)
78+ fmap _ (Error err) = Error err
79+ {-# INLINE fmap #-}
80+
81+instance Monad Result where
82+ return = Success
83+ {-# INLINE return #-}
84+ Success a >>= k = k a
85+ Error err >>= _ = Error err
86+ {-# INLINE (>>=) #-}
87+
88+instance Applicative Result where
89+ pure = return
90+ {-# INLINE pure #-}
91+ (<*>) = ap
92+ {-# INLINE (<*>) #-}
93+
94+instance MonadPlus Result where
95+ mzero = fail "mzero"
96+ {-# INLINE mzero #-}
97+ mplus a@(Success _) _ = a
98+ mplus _ b = b
99+ {-# INLINE mplus #-}
100+
101+instance Alternative Result where
102+ empty = mzero
103+ {-# INLINE empty #-}
104+ (<|>) = mplus
105+ {-# INLINE (<|>) #-}
106+
107+instance Monoid (Result a) where
108+ mempty = fail "mempty"
109+ {-# INLINE mempty #-}
110+ mappend = mplus
111+ {-# INLINE mappend #-}
112+
113+-- | Failure continuation.
114+type Failure f r = String -> f r
115+-- | Success continuation.
116+type Success a f r = a -> f r
78117
79118-- | A continuation-based parser type.
80119newtype Parser a = Parser {
81− runParser :: forall r.
82− Failure r
83− -> Success a r
84− -> Result r
120+ runParser :: forall f r.
121+ Failure f r
122+ -> Success a f r
123+ -> f r
85124 }
86125
87126instance Monad Parser where
@@ -183,6 +222,16 @@ parse :: (a -> Parser b) -> a -> Result b
183222parse m v = runParser (m v) Error Success
184223{-# INLINE parse #-}
185224
225+-- | Run a 'Parser' with a 'Maybe' result type.
226+parseMaybe :: (a -> Parser b) -> a -> Maybe b
227+parseMaybe m v = runParser (m v) (const Nothing) Just
228+{-# INLINE parseMaybe #-}
229+
230+-- | Run a 'Parser' with an 'Either' result type.
231+parseEither :: (a -> Parser b) -> a -> Either String b
232+parseEither m v = runParser (m v) Left Right
233+{-# INLINE parseEither #-}
234+
186235-- | Retrieve the value associated with the given key of an 'Object'.
187236-- The result is 'empty' if the key is not present or the value cannot
188237-- be converted to the desired type.