on master help sign in
Merge Either instance
commitad0ab3014ae61286b90c67c22075cfdd8d127802 authorBryan O'Sullivan <bos@serpentine.com> dateSat 6 Aug 2011, 22:20 (15 years ago) parentf61e4b63cc7cd0
1 file changed+12−4
Data/Aeson/Types.hs+12−4
Data/Aeson/Types.hs+12−4
@@ -1,5 +1,6 @@
11{-# LANGUAGE DeriveDataTypeable, FlexibleInstances, GeneralizedNewtypeDeriving,
2− IncoherentInstances, OverlappingInstances, OverloadedStrings, Rank2Types #-}
2+ IncoherentInstances, OverlappingInstances, OverloadedStrings, Rank2Types,
3+ ViewPatterns #-}
34
45-- |
56-- Module: Data.Aeson.Types
@@ -329,14 +330,21 @@ instance (FromJSON a) => FromJSON (Maybe a) where
329330 {-# INLINE parseJSON #-}
330331
331332instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
332− toJSON (Left a) = toJSON a
333− toJSON (Right b) = toJSON b
333+ toJSON (Left a) = object [left .= a]
334+ toJSON (Right b) = object [right .= b]
334335 {-# INLINE toJSON #-}
335336
336337instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
337− parseJSON a = Left <$> parseJSON a <|> Right <$> parseJSON a
338+ parseJSON (Object (M.toList -> [(key, value)]))
339+ | key == left = Left <$> parseJSON value
340+ | key == right = Right <$> parseJSON value
341+ parseJSON _ = mzero
338342 {-# INLINE parseJSON #-}
339343
344+left, right :: Text
345+left = "Left"
346+right = "Right"
347+
340348instance ToJSON Bool where
341349 toJSON = Bool
342350 {-# INLINE toJSON #-}