@@ -34,6 +34,17 @@ module Data.Aeson.Types.Instances
34 , FromJSONKeyFunction(..)
34 , FromJSONKeyFunction(..)
35 , fromJSONKeyCoerce
35 , fromJSONKeyCoerce
36 , coerceFromJSONKeyFunction
36 , coerceFromJSONKeyFunction
37+ -- ** Liftings to unary and binary type constructors
37 -- ** Generic JSON classes
48 -- ** Generic JSON classes
38 , GFromJSON(..)
49 , GFromJSON(..)
39 , GToJSON(..)
50 , GToJSON(..)
@@ -159,50 +170,102 @@ toJSONPair keySerialiser (a, b) = Array $ V.create $ do
159 VM.unsafeWrite mv 1 (toJSON b)
170 VM.unsafeWrite mv 1 (toJSON b)
160 return mv
171 return mv
173+instance ToJSON1 Identity where
174+ liftToJSON to (Identity a) = to a
175+ {-# INLINE liftToJSON #-}
177+ liftToEncoding to (Identity a) = to a
178+ {-# INLINE liftToEncoding #-}
162instance (ToJSON a) => ToJSON (Identity a) where
180instance (ToJSON a) => ToJSON (Identity a) where
163− toJSON (Identity a) = toJSON a
181+ toJSON = toJSON1
164 {-# INLINE toJSON #-}
182 {-# INLINE toJSON #-}
166− toEncoding (Identity a) = toEncoding a
184+ toEncoding = toEncoding1
167 {-# INLINE toEncoding #-}
185 {-# INLINE toEncoding #-}
187+instance FromJSON1 Identity where
188+ liftParseJSON p a = Identity <$> p a
189+ {-# INLINE liftParseJSON #-}
169instance (FromJSON a) => FromJSON (Identity a) where
191instance (FromJSON a) => FromJSON (Identity a) where
170− parseJSON a = Identity <$> parseJSON a
192+ parseJSON = parseJSON1
171 {-# INLINE parseJSON #-}
193 {-# INLINE parseJSON #-}
196+instance ToJSON1 Maybe where
197+ liftToJSON to (Just a) = to a
198+ liftToJSON _ Nothing = Null
199+ {-# INLINE liftToJSON #-}
201+ liftToEncoding to (Just a) = to a
202+ liftToEncoding _ Nothing = Encoding E.null_
203+ {-# INLINE liftToEncoding #-}
173instance (ToJSON a) => ToJSON (Maybe a) where
205instance (ToJSON a) => ToJSON (Maybe a) where
174− toJSON (Just a) = toJSON a
206+ toJSON = toJSON1
175− toJSON Nothing = Null
176 {-# INLINE toJSON #-}
207 {-# INLINE toJSON #-}
178− toEncoding (Just a) = toEncoding a
209+ toEncoding = toEncoding1
179− toEncoding Nothing = Encoding E.null_
180 {-# INLINE toEncoding #-}
210 {-# INLINE toEncoding #-}
212+instance FromJSON1 Maybe where
213+ liftParseJSON _ Null = pure Nothing
214+ liftParseJSON p a = Just <$> p a
215+ {-# INLINE liftParseJSON #-}
182instance (FromJSON a) => FromJSON (Maybe a) where
217instance (FromJSON a) => FromJSON (Maybe a) where
183− parseJSON Null = pure Nothing
218+ parseJSON = parseJSON1
184− parseJSON a = Just <$> parseJSON a
185 {-# INLINE parseJSON #-}
219 {-# INLINE parseJSON #-}
222+instance ToJSON2 Either where
223+ liftToJSON2 toA _toB (Left a) = Object $ H.singleton left (toA a)
224+ liftToJSON2 _toA toB (Right b) = Object $ H.singleton right (toB b)
225+ {-# INLINE liftToJSON2 #-}
227+ liftToEncoding2 toA _toB (Left a) =
228+ Encoding (B.shortByteString "{\"Left\":")
230+ <> Encoding (B.char7 '}')
232+ liftToEncoding2 _toA toB (Right b) =
233+ Encoding (B.shortByteString "{\"Right\":")
235+ <> Encoding (B.char7 '}')
236+ {-# INLINE liftToEncoding2 #-}
238+instance (ToJSON a) => ToJSON1 (Either a) where
239+ liftToJSON = liftToJSON2 toJSON
240+ {-# INLINE liftToJSON #-}
242+ liftToEncoding = liftToEncoding2 toEncoding
243+ {-# INLINE liftToEncoding #-}
187instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
245instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
188− toJSON (Left a) = object [left .= a]
246+ toJSON = toJSON2
189− toJSON (Right b) = object [right .= b]
190 {-# INLINE toJSON #-}
247 {-# INLINE toJSON #-}
192− toEncoding (Left a) = Encoding $
249+ toEncoding = toEncoding2
193− B.shortByteString "{\"Left\":" <> builder a <> B.char7 '}'
194− toEncoding (Right a) = Encoding $
195− B.shortByteString "{\"Right\":" <> builder a <> B.char7 '}'
196 {-# INLINE toEncoding #-}
250 {-# INLINE toEncoding #-}
198−instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
252+instance FromJSON2 Either where
199− parseJSON (Object (H.toList -> [(key, value)]))
253+ liftParseJSON2 pA pB (Object (H.toList -> [(key, value)]))
200− | key == left = Left <$> parseJSON value <?> Key left
254+ | key == left = Left <$> pA value <?> Key left
201− | key == right = Right <$> parseJSON value <?> Key right
255+ | key == right = Right <$> pB value <?> Key right
202− parseJSON _ = fail $
256+
257+ liftParseJSON2 _ _ _ = fail $
203 "expected an object with a single property " ++
258 "expected an object with a single property " ++
204 "where the property key should be either " ++
259 "where the property key should be either " ++
205 "\"Left\" or \"Right\""
260 "\"Left\" or \"Right\""
261+ {-# INLINE liftParseJSON2 #-}
263+instance (FromJSON a) => FromJSON1 (Either a) where
264+ liftParseJSON = liftParseJSON2 parseJSON
265+ {-# INLINE liftParseJSON #-}
267+instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
268+ parseJSON = parseJSON2
206 {-# INLINE parseJSON #-}
269 {-# INLINE parseJSON #-}
208left, right :: Text
271left, right :: Text