4
4
{-# LANGUAGE FlexibleContexts #-}
5
5
{-# LANGUAGE FlexibleInstances #-}
6
6
{-# LANGUAGE FunctionalDependencies #-}
7
+ {-# LANGUAGE InstanceSigs #-}
7
8
{-# LANGUAGE GADTs #-}
8
9
{-# LANGUAGE NoImplicitPrelude #-}
9
10
{-# LANGUAGE OverloadedStrings #-}
@@ -68,6 +69,7 @@ import qualified Data.Aeson.Key as Key
68
69
import qualified Data.Aeson.KeyMap as KM
69
70
import Data.Attoparsec.Number (Number (.. ))
70
71
import Data.Bits (unsafeShiftR )
72
+ import Data.Coerce (coerce )
71
73
import Data.DList (DList )
72
74
import Data.Fixed (Fixed , HasResolution , Nano )
73
75
import Data.Foldable (toList )
@@ -101,6 +103,9 @@ import Data.Word (Word16, Word32, Word64, Word8)
101
103
import Foreign.Storable (Storable )
102
104
import Foreign.C.Types (CTime (.. ))
103
105
import GHC.Generics
106
+ #if !MIN_VERSION_base(4,17,0)
107
+ import GHC.Generics.Generically (Generically (.. ), Generically1 (.. ))
108
+ #endif
104
109
import Numeric.Natural (Natural )
105
110
import qualified Data.Aeson.Encoding as E
106
111
import qualified Data.Aeson.Encoding.Internal as E (InArray , comma , econcat , retagEncoding , key )
@@ -256,6 +261,12 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
256
261
-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
257
262
-- @
258
263
--
264
+ -- or more conveniently using the [DerivingVia extension](https://downloads.haskell.org/ghc/9.2.3/docs/html/users_guide/exts/deriving_via.html)
265
+ --
266
+ -- @
267
+ -- deriving via 'Generically' Coord instance 'ToJSON' Coord
268
+ -- @
269
+ --
259
270
-- If on the other hand you wish to customize the generic decoding, you have
260
271
-- to implement both methods:
261
272
--
@@ -272,7 +283,7 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
272
283
-- Previous versions of this library only had the 'toJSON' method. Adding
273
284
-- 'toEncoding' had two reasons:
274
285
--
275
- -- 1. toEncoding is more efficient for the common case that the output of
286
+ -- 1. ' toEncoding' is more efficient for the common case that the output of
276
287
-- 'toJSON' is directly serialized to a @ByteString@.
277
288
-- Further, expressing either method in terms of the other would be
278
289
-- non-optimal.
@@ -321,6 +332,11 @@ class ToJSON a where
321
332
toEncodingList :: [a ] -> Encoding
322
333
toEncodingList = listEncoding toEncoding
323
334
335
+ -- | @since 2.1.0.0
336
+ instance (Generic a , GToJSON' Value Zero (Rep a ), GToJSON' Encoding Zero (Rep a )) => ToJSON (Generically a ) where
337
+ toJSON = coerce (genericToJSON defaultOptions :: a -> Value )
338
+ toEncoding = coerce (genericToEncoding defaultOptions :: a -> Encoding )
339
+
324
340
-------------------------------------------------------------------------------
325
341
-- Object key-value pairs
326
342
-------------------------------------------------------------------------------
@@ -615,6 +631,14 @@ class ToJSON1 f where
615
631
liftToEncodingList :: (a -> Encoding ) -> ([a ] -> Encoding ) -> [f a ] -> Encoding
616
632
liftToEncodingList f g = listEncoding (liftToEncoding f g)
617
633
634
+ -- | @since 2.1.0.0
635
+ instance (Generic1 f , GToJSON' Value One (Rep1 f ), GToJSON' Encoding One (Rep1 f )) => ToJSON1 (Generically1 f ) where
636
+ liftToJSON :: forall a . (a -> Value ) -> ([a ] -> Value ) -> Generically1 f a -> Value
637
+ liftToJSON = coerce (genericLiftToJSON defaultOptions :: (a -> Value ) -> ([a ] -> Value ) -> f a -> Value )
638
+
639
+ liftToEncoding :: forall a . (a -> Encoding ) -> ([a ] -> Encoding ) -> Generically1 f a -> Encoding
640
+ liftToEncoding = coerce (genericLiftToEncoding defaultOptions :: (a -> Encoding ) -> ([a ] -> Encoding ) -> f a -> Encoding )
641
+
618
642
-- | Lift the standard 'toJSON' function through the type constructor.
619
643
toJSON1 :: (ToJSON1 f , ToJSON a ) => f a -> Value
620
644
toJSON1 = liftToJSON toJSON toJSONList
0 commit comments