on master help sign in
Get rid of MonadPlus nonsense, revert version bump, add Monoid instances
commit4474420f8c0655529d3809d7469cd4ba2787388a authorBryan O'Sullivan <bos@serpentine.com> dateTue 1 Feb 2011, 17:28 (16 years ago) parent2a2563c
2 files changed+33−57
Data/Aeson/Types.hs+32−56 aeson.cabal+1−1
Data/Aeson/Types.hs+32−56
@@ -29,8 +29,8 @@ module Data.Aeson.Types
2929 ) where
3030
3131import Control.Arrow ((***))
32+import Control.Applicative
3233import Control.DeepSeq (NFData(..))
33−import Control.Monad (MonadPlus(..), ap, liftM)
3434import Data.Data (Data)
3535import Data.Map (Map)
3636import Data.Monoid (Dual(..), First(..), Last(..))
@@ -98,9 +98,9 @@ name .= value = Pair (name, toJSON value)
9898-- This accessor is appropriate if the key and value /must/ be present
9999-- in an object for it to be valid. If the key and value are
100100-- optional, use '(.:?)' instead.
101−(.:) :: (MonadPlus m, FromJSON a) => Object -> Text -> m a
101+(.:) :: (Alternative m, FromJSON a) => Object -> Text -> m a
102102obj .: key = case M.lookup key obj of
103− Nothing -> mzero
103+ Nothing -> empty
104104 Just v -> fromJSON v
105105{-# INLINE (.:) #-}
106106
@@ -111,9 +111,9 @@ obj .: key = case M.lookup key obj of
111111-- This accessor is most useful if the key and value can be absent
112112-- from an object without affecting its validity. If the key and
113113-- value are mandatory, use '(.:?)' instead.
114−(.:?) :: (MonadPlus m, FromJSON a) => Object -> Text -> m (Maybe a)
114+(.:?) :: (Alternative m, FromJSON a) => Object -> Text -> m (Maybe a)
115115obj .:? key = case M.lookup key obj of
116− Nothing -> return Nothing
116+ Nothing -> pure Nothing
117117 Just v -> fromJSON v
118118{-# INLINE (.:?) #-}
119119
@@ -147,15 +147,15 @@ class ToJSON a where
147147-- @data Coord { x :: Double, y :: Double }
148148--
149149-- instance FromJSON Coord where
150−-- fromJSON ('Object' v) = Coord `'liftM'`
151−-- v '.:' \"x\" `'ap'`
150+-- fromJSON ('Object' v) = Coord '<$>'
151+-- v '.:' \"x\" '<*>'
152152-- v '.:' \"y\"
153153--
154154-- \-- A non-'Object' value is of the wrong type, so use 'mzero' to fail.
155155-- fromJSON _ = 'mzero'
156156-- @
157157class FromJSON a where
158− fromJSON :: MonadPlus m => Value -> m a
158+ fromJSON :: Alternative m => Value -> m a
159159
160160instance (ToJSON a) => ToJSON (Maybe a) where
161161 toJSON (Just a) = toJSON a
@@ -163,7 +163,7 @@ instance (ToJSON a) => ToJSON (Maybe a) where
163163 {-# INLINE toJSON #-}
164164
165165instance (FromJSON a) => FromJSON (Maybe a) where
166− fromJSON Null = return Nothing
166+ fromJSON Null = pure Nothing
167167 fromJSON a = Just <$> fromJSON a
168168 {-# INLINE fromJSON #-}
169169
@@ -181,7 +181,7 @@ instance ToJSON Bool where
181181 {-# INLINE toJSON #-}
182182
183183instance FromJSON Bool where
184− fromJSON (Bool b) = return b
184+ fromJSON (Bool b) = pure b
185185 fromJSON _ = empty
186186 {-# INLINE fromJSON #-}
187187
@@ -190,7 +190,7 @@ instance ToJSON Double where
190190 {-# INLINE toJSON #-}
191191
192192instance FromJSON Double where
193− fromJSON (Number n) = return n
193+ fromJSON (Number n) = pure n
194194 fromJSON _ = empty
195195 {-# INLINE fromJSON #-}
196196
@@ -199,7 +199,7 @@ instance ToJSON Int where
199199 {-# INLINE toJSON #-}
200200
201201instance FromJSON Int where
202− fromJSON (Number n) = return (floor n)
202+ fromJSON (Number n) = pure (floor n)
203203 fromJSON _ = empty
204204 {-# INLINE fromJSON #-}
205205
@@ -208,7 +208,7 @@ instance ToJSON Integer where
208208 {-# INLINE toJSON #-}
209209
210210instance FromJSON Integer where
211− fromJSON (Number n) = return (floor n)
211+ fromJSON (Number n) = pure (floor n)
212212 fromJSON _ = empty
213213 {-# INLINE fromJSON #-}
214214
@@ -217,7 +217,7 @@ instance ToJSON Text where
217217 {-# INLINE toJSON #-}
218218
219219instance FromJSON Text where
220− fromJSON (String t) = return t
220+ fromJSON (String t) = pure t
221221 fromJSON _ = empty
222222 {-# INLINE fromJSON #-}
223223
@@ -226,7 +226,7 @@ instance ToJSON LT.Text where
226226 {-# INLINE toJSON #-}
227227
228228instance FromJSON LT.Text where
229− fromJSON (String t) = return (LT.fromStrict t)
229+ fromJSON (String t) = pure (LT.fromStrict t)
230230 fromJSON _ = empty
231231 {-# INLINE fromJSON #-}
232232
@@ -235,7 +235,7 @@ instance ToJSON B.ByteString where
235235 {-# INLINE toJSON #-}
236236
237237instance FromJSON B.ByteString where
238− fromJSON (String t) = return . encodeUtf8 $ t
238+ fromJSON (String t) = pure . encodeUtf8 $ t
239239 fromJSON _ = empty
240240 {-# INLINE fromJSON #-}
241241
@@ -244,7 +244,7 @@ instance ToJSON LB.ByteString where
244244 {-# INLINE toJSON #-}
245245
246246instance FromJSON LB.ByteString where
247− fromJSON (String t) = return . LB.fromChunks . (:[]) . encodeUtf8 $ t
247+ fromJSON (String t) = pure . LB.fromChunks . (:[]) . encodeUtf8 $ t
248248 fromJSON _ = empty
249249 {-# INLINE fromJSON #-}
250250
@@ -271,7 +271,7 @@ instance (ToJSON a) => ToJSON (Set.Set a) where
271271 {-# INLINE toJSON #-}
272272
273273instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
274− fromJSON = liftM Set.fromList . fromJSON
274+ fromJSON = fmap Set.fromList . fromJSON
275275 {-# INLINE fromJSON #-}
276276
277277instance (ToJSON v) => ToJSON (M.Map Text v) where
@@ -279,31 +279,30 @@ instance (ToJSON v) => ToJSON (M.Map Text v) where
279279 {-# INLINE toJSON #-}
280280
281281instance (FromJSON v) => FromJSON (M.Map Text v) where
282− fromJSON (Object o) = go [] (M.toAscList o)
282+ fromJSON (Object o) = M.fromAscList <$> go (M.toAscList o)
283283 where
284− go acc ((k,v):kvs) = do v' <- fromJSON v
285− go ((k,v'):acc) kvs
286− go acc _ = return (M.fromAscList (reverse acc))
284+ go ((k,v):kvs) = ((:) . (,) k) <$> fromJSON v <*> go kvs
285+ go _ = pure []
287286 fromJSON _ = empty
288287
289288instance (ToJSON v) => ToJSON (M.Map LT.Text v) where
290289 toJSON = Object . transformMap LT.toStrict toJSON
291290
292291instance (FromJSON v) => FromJSON (M.Map LT.Text v) where
293− fromJSON = liftM (M.mapKeysMonotonic LT.fromStrict) . fromJSON
292+ fromJSON = fmap (M.mapKeysMonotonic LT.fromStrict) . fromJSON
294293
295294instance (ToJSON v) => ToJSON (M.Map String v) where
296295 toJSON = Object . transformMap pack toJSON
297296
298297instance (FromJSON v) => FromJSON (M.Map String v) where
299− fromJSON = liftM (M.mapKeysMonotonic unpack) . fromJSON
298+ fromJSON = fmap (M.mapKeysMonotonic unpack) . fromJSON
300299
301300instance ToJSON Value where
302301 toJSON a = a
303302 {-# INLINE toJSON #-}
304303
305304instance FromJSON Value where
306− fromJSON a = return a
305+ fromJSON a = pure a
307306 {-# INLINE fromJSON #-}
308307
309308-- We happen to use the same JSON formatting for a UTCTime as .NET
@@ -315,7 +314,7 @@ instance ToJSON UTCTime where
315314instance FromJSON UTCTime where
316315 fromJSON (String t) =
317316 case parseTime defaultTimeLocale "/Date(%s)/" (unpack t) of
318− Just d -> return d
317+ Just d -> pure d
319318 _ -> empty
320319 fromJSON _ = empty
321320 {-# INLINE fromJSON #-}
@@ -336,7 +335,7 @@ instance ToJSON a => ToJSON (Dual a) where
336335 {-# INLINE toJSON #-}
337336
338337instance FromJSON a => FromJSON (Dual a) where
339− fromJSON = liftM Dual . fromJSON
338+ fromJSON = fmap Dual . fromJSON
340339 {-# INLINE fromJSON #-}
341340
342341instance ToJSON a => ToJSON (First a) where
@@ -344,7 +343,7 @@ instance ToJSON a => ToJSON (First a) where
344343 {-# INLINE toJSON #-}
345344
346345instance FromJSON a => FromJSON (First a) where
347− fromJSON = liftM First . fromJSON
346+ fromJSON = fmap First . fromJSON
348347 {-# INLINE fromJSON #-}
349348
350349instance ToJSON a => ToJSON (Last a) where
@@ -352,7 +351,7 @@ instance ToJSON a => ToJSON (Last a) where
352351 {-# INLINE toJSON #-}
353352
354353instance FromJSON a => FromJSON (Last a) where
355− fromJSON = liftM Last . fromJSON
354+ fromJSON = fmap Last . fromJSON
356355 {-# INLINE fromJSON #-}
357356
358357-- | Transform one map into another. The ordering of keys must be
@@ -361,31 +360,8 @@ transformMap :: (Ord k1, Ord k2) => (k1 -> k2) -> (v1 -> v2)
361360 -> M.Map k1 v1 -> M.Map k2 v2
362361transformMap fk fv = M.fromAscList . map (fk *** fv) . M.toAscList
363362
364−mapA :: (MonadPlus m) => (t -> m a) -> [t] -> m [a]
365−mapA f = go []
363+mapA :: (Alternative m) => (t -> m a) -> [t] -> m [a]
364+mapA f = go
366365 where
367− go acc (a:as) = do
368− v <- f a
369− go (v:acc) as
370− go acc _ = return (reverse acc)
371−
372−-- Applicative-style notation.
373−
374−(<$>) :: (Monad m) => (a1 -> r) -> m a1 -> m r
375−(<$>) = liftM
376−{-# INLINE (<$>) #-}
377−infixl 4 <$>
378−
379−(<*>) :: (Monad m) => m (a -> b) -> m a -> m b
380−(<*>) = ap
381−{-# INLINE (<*>) #-}
382−infixl 4 <*>
383−
384−(<|>) :: (MonadPlus m) => m a -> m a -> m a
385−(<|>) = mplus
386−{-# INLINE (<|>) #-}
387−infixl 3 <|>
388−
389−empty :: (MonadPlus m) => m a
390−empty = mzero
391−{-# INLINE empty #-}
366+ go (a:as) = (:) <$> f a <*> go as
367+ go _ = pure []
aeson.cabal+1−1
@@ -1,5 +1,5 @@
11name: aeson
2−version: 0.2.0.0
2+version: 0.1.0.2
33license: BSD3
44license-file: LICENSE
55category: Text, Web, JSON