Skip to content

Commit 95287fe

Browse files
authored
Merge pull request #775 from phadej/issue-700
Resolve #700. Expose GToJSON.
2 parents f439b22 + dd2ace9 commit 95287fe

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

Data/Aeson.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ module Data.Aeson
9191
, FromArgs
9292
, GToJSON
9393
, GToEncoding
94+
, GToJSON'
9495
, ToArgs
9596
, Zero
9697
, One

Data/Aeson/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module Data.Aeson.Types
7777
, FromArgs
7878
, GToJSON
7979
, GToEncoding
80+
, GToJSON'
8081
, ToArgs
8182
, Zero
8283
, One

Data/Aeson/Types/Class.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Data.Aeson.Types.Class
3939
, FromArgs(..)
4040
, GToJSON
4141
, GToEncoding
42+
, GToJSON'
4243
, ToArgs(..)
4344
, Zero
4445
, One
@@ -100,10 +101,9 @@ module Data.Aeson.Types.Class
100101

101102
import Data.Aeson.Types.FromJSON
102103
import Data.Aeson.Types.Generic (One, Zero)
103-
import Data.Aeson.Types.ToJSON hiding (GToJSON)
104-
import qualified Data.Aeson.Types.ToJSON as ToJSON
104+
import Data.Aeson.Types.ToJSON
105105
import Data.Aeson.Types.Internal (Value)
106106
import Data.Aeson.Encoding (Encoding)
107107

108-
type GToJSON = ToJSON.GToJSON Value
109-
type GToEncoding = ToJSON.GToJSON Encoding
108+
type GToJSON = GToJSON' Value
109+
type GToEncoding = GToJSON' Encoding

Data/Aeson/Types/ToJSON.hs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Data.Aeson.Types.ToJSON
3232
, toJSON2
3333
, toEncoding2
3434
-- * Generic JSON classes
35-
, GToJSON(..)
35+
, GToJSON'(..)
3636
, ToArgs(..)
3737
, genericToJSON
3838
, genericToEncoding
@@ -155,7 +155,7 @@ realFloatToJSON d
155155

156156
-- | Class of generic representation types that can be converted to
157157
-- JSON.
158-
class GToJSON enc arity f where
158+
class GToJSON' enc arity f where
159159
-- | This method (applied to 'defaultOptions') is used as the
160160
-- default generic implementation of 'toJSON'
161161
-- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
@@ -176,29 +176,29 @@ data ToArgs res arity a where
176176
-- | A configurable generic JSON creator. This function applied to
177177
-- 'defaultOptions' is used as the default for 'toJSON' when the type
178178
-- is an instance of 'Generic'.
179-
genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
179+
genericToJSON :: (Generic a, GToJSON' Value Zero (Rep a))
180180
=> Options -> a -> Value
181181
genericToJSON opts = gToJSON opts NoToArgs . from
182182

183183
-- | A configurable generic JSON creator. This function applied to
184184
-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
185185
-- is an instance of 'Generic1'.
186-
genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
186+
genericLiftToJSON :: (Generic1 f, GToJSON' Value One (Rep1 f))
187187
=> Options -> (a -> Value) -> ([a] -> Value)
188188
-> f a -> Value
189189
genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
190190

191191
-- | A configurable generic JSON encoder. This function applied to
192192
-- 'defaultOptions' is used as the default for 'toEncoding' when the type
193193
-- is an instance of 'Generic'.
194-
genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
194+
genericToEncoding :: (Generic a, GToJSON' Encoding Zero (Rep a))
195195
=> Options -> a -> Encoding
196196
genericToEncoding opts = gToJSON opts NoToArgs . from
197197

198198
-- | A configurable generic JSON encoder. This function applied to
199199
-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
200200
-- is an instance of 'Generic1'.
201-
genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
201+
genericLiftToEncoding :: (Generic1 f, GToJSON' Encoding One (Rep1 f))
202202
=> Options -> (a -> Encoding) -> ([a] -> Encoding)
203203
-> f a -> Encoding
204204
genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
@@ -287,7 +287,7 @@ class ToJSON a where
287287
-- | Convert a Haskell value to a JSON-friendly intermediate type.
288288
toJSON :: a -> Value
289289

290-
default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
290+
default toJSON :: (Generic a, GToJSON' Value Zero (Rep a)) => a -> Value
291291
toJSON = genericToJSON defaultOptions
292292

293293
-- | Encode a Haskell value as JSON.
@@ -590,7 +590,7 @@ instance GetConName f => GToJSONKey f
590590
class ToJSON1 f where
591591
liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
592592

593-
default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
593+
default liftToJSON :: (Generic1 f, GToJSON' Value One (Rep1 f))
594594
=> (a -> Value) -> ([a] -> Value) -> f a -> Value
595595
liftToJSON = genericLiftToJSON defaultOptions
596596

@@ -599,7 +599,7 @@ class ToJSON1 f where
599599

600600
liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
601601

602-
default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
602+
default liftToEncoding :: (Generic1 f, GToJSON' Encoding One (Rep1 f))
603603
=> (a -> Encoding) -> ([a] -> Encoding)
604604
-> f a -> Encoding
605605
liftToEncoding = genericLiftToEncoding defaultOptions
@@ -699,12 +699,12 @@ instance (ToJSON a) => ToJSON [a] where
699699
-- Generic toJSON / toEncoding
700700
-------------------------------------------------------------------------------
701701

702-
instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
702+
instance OVERLAPPABLE_ (GToJSON' enc arity a) => GToJSON' enc arity (M1 i c a) where
703703
-- Meta-information, which is not handled elsewhere, is ignored:
704704
gToJSON opts targs = gToJSON opts targs . unM1
705705
{-# INLINE gToJSON #-}
706706

707-
instance GToJSON enc One Par1 where
707+
instance GToJSON' enc One Par1 where
708708
-- Direct occurrences of the last type parameter are encoded with the
709709
-- function passed in as an argument:
710710
gToJSON _opts (To1Args tj _) = tj . unPar1
@@ -713,7 +713,7 @@ instance GToJSON enc One Par1 where
713713
instance ( ConsToJSON enc arity a
714714
, AllNullary (C1 c a) allNullary
715715
, SumToJSON enc arity (C1 c a) allNullary
716-
) => GToJSON enc arity (D1 d (C1 c a)) where
716+
) => GToJSON' enc arity (D1 d (C1 c a)) where
717717
-- The option 'tagSingleConstructors' determines whether to wrap
718718
-- a single-constructor type.
719719
gToJSON opts targs
@@ -723,15 +723,15 @@ instance ( ConsToJSON enc arity a
723723
| otherwise = consToJSON opts targs . unM1 . unM1
724724
{-# INLINE gToJSON #-}
725725

726-
instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
726+
instance (ConsToJSON enc arity a) => GToJSON' enc arity (C1 c a) where
727727
-- Constructors need to be encoded differently depending on whether they're
728728
-- a record or not. This distinction is made by 'consToJSON':
729729
gToJSON opts targs = consToJSON opts targs . unM1
730730
{-# INLINE gToJSON #-}
731731

732732
instance ( AllNullary (a :+: b) allNullary
733733
, SumToJSON enc arity (a :+: b) allNullary
734-
) => GToJSON enc arity (a :+: b)
734+
) => GToJSON' enc arity (a :+: b)
735735
where
736736
-- If all constructors of a sum datatype are nullary and the
737737
-- 'allNullaryToStringTag' option is set they are encoded to
@@ -747,31 +747,31 @@ instance ( AllNullary (a :+: b) allNullary
747747
-- possible but makes error messages a bit harder to understand for missing
748748
-- instances.
749749

750-
instance GToJSON Value arity V1 where
750+
instance GToJSON' Value arity V1 where
751751
-- Empty values do not exist, which makes the job of formatting them
752752
-- rather easy:
753753
gToJSON _ _ x = x `seq` error "case: V1"
754754
{-# INLINE gToJSON #-}
755755

756-
instance ToJSON a => GToJSON Value arity (K1 i a) where
756+
instance ToJSON a => GToJSON' Value arity (K1 i a) where
757757
-- Constant values are encoded using their ToJSON instance:
758758
gToJSON _opts _ = toJSON . unK1
759759
{-# INLINE gToJSON #-}
760760

761-
instance ToJSON1 f => GToJSON Value One (Rec1 f) where
761+
instance ToJSON1 f => GToJSON' Value One (Rec1 f) where
762762
-- Recursive occurrences of the last type parameter are encoded using their
763763
-- ToJSON1 instance:
764764
gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
765765
{-# INLINE gToJSON #-}
766766

767-
instance GToJSON Value arity U1 where
767+
instance GToJSON' Value arity U1 where
768768
-- Empty constructors are encoded to an empty array:
769769
gToJSON _opts _ _ = emptyArray
770770
{-# INLINE gToJSON #-}
771771

772772
instance ( WriteProduct arity a, WriteProduct arity b
773773
, ProductSize a, ProductSize b
774-
) => GToJSON Value arity (a :*: b)
774+
) => GToJSON' Value arity (a :*: b)
775775
where
776776
-- Products are encoded to an array. Here we allocate a mutable vector of
777777
-- the same size as the product and write the product's elements to it using
@@ -787,8 +787,8 @@ instance ( WriteProduct arity a, WriteProduct arity b
787787
{-# INLINE gToJSON #-}
788788

789789
instance ( ToJSON1 f
790-
, GToJSON Value One g
791-
) => GToJSON Value One (f :.: g)
790+
, GToJSON' Value One g
791+
) => GToJSON' Value One (f :.: g)
792792
where
793793
-- If an occurrence of the last type parameter is nested inside two
794794
-- composed types, it is encoded by using the outermost type's ToJSON1
@@ -801,25 +801,25 @@ instance ( ToJSON1 f
801801
--------------------------------------------------------------------------------
802802
-- Generic toEncoding
803803

804-
instance ToJSON a => GToJSON Encoding arity (K1 i a) where
804+
instance ToJSON a => GToJSON' Encoding arity (K1 i a) where
805805
-- Constant values are encoded using their ToJSON instance:
806806
gToJSON _opts _ = toEncoding . unK1
807807
{-# INLINE gToJSON #-}
808808

809-
instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
809+
instance ToJSON1 f => GToJSON' Encoding One (Rec1 f) where
810810
-- Recursive occurrences of the last type parameter are encoded using their
811811
-- ToEncoding1 instance:
812812
gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
813813
{-# INLINE gToJSON #-}
814814

815-
instance GToJSON Encoding arity U1 where
815+
instance GToJSON' Encoding arity U1 where
816816
-- Empty constructors are encoded to an empty array:
817817
gToJSON _opts _ _ = E.emptyArray_
818818
{-# INLINE gToJSON #-}
819819

820820
instance ( EncodeProduct arity a
821821
, EncodeProduct arity b
822-
) => GToJSON Encoding arity (a :*: b)
822+
) => GToJSON' Encoding arity (a :*: b)
823823
where
824824
-- Products are encoded to an array. Here we allocate a mutable vector of
825825
-- the same size as the product and write the product's elements to it using
@@ -828,8 +828,8 @@ instance ( EncodeProduct arity a
828828
{-# INLINE gToJSON #-}
829829

830830
instance ( ToJSON1 f
831-
, GToJSON Encoding One g
832-
) => GToJSON Encoding One (f :.: g)
831+
, GToJSON' Encoding One g
832+
) => GToJSON' Encoding One (f :.: g)
833833
where
834834
-- If an occurrence of the last type parameter is nested inside two
835835
-- composed types, it is encoded by using the outermost type's ToJSON1
@@ -939,7 +939,7 @@ class TaggedObject' enc pairs arity f isRecord where
939939
taggedObject' :: Options -> ToArgs enc arity a
940940
-> String -> f a -> Tagged isRecord pairs
941941

942-
instance ( GToJSON enc arity f
942+
instance ( GToJSON' enc arity f
943943
, KeyValuePair enc pairs
944944
) => TaggedObject' enc pairs arity f False
945945
where
@@ -994,7 +994,7 @@ instance ( SumToJSON' s enc arity a
994994

995995
--------------------------------------------------------------------------------
996996

997-
instance ( GToJSON Value arity a
997+
instance ( GToJSON' Value arity a
998998
, ConsToJSON Value arity a
999999
, Constructor c
10001000
) => SumToJSON' TwoElemArray Value arity (C1 c a) where
@@ -1007,7 +1007,7 @@ instance ( GToJSON Value arity a
10071007

10081008
--------------------------------------------------------------------------------
10091009

1010-
instance ( GToJSON Encoding arity a
1010+
instance ( GToJSON' Encoding arity a
10111011
, ConsToJSON Encoding arity a
10121012
, Constructor c
10131013
) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
@@ -1039,7 +1039,7 @@ instance ( IsRecord f isRecord
10391039
instance OVERLAPPING_
10401040
( RecordToPairs enc pairs arity (S1 s f)
10411041
, FromPairs enc pairs
1042-
, GToJSON enc arity f
1042+
, GToJSON' enc arity f
10431043
) => ConsToJSON' enc arity (S1 s f) True
10441044
where
10451045
consToJSON' opts targs
@@ -1054,7 +1054,7 @@ instance ( RecordToPairs enc pairs arity f
10541054
consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
10551055
{-# INLINE consToJSON' #-}
10561056

1057-
instance GToJSON enc arity f => ConsToJSON' enc arity f False where
1057+
instance GToJSON' enc arity f => ConsToJSON' enc arity f False where
10581058
consToJSON' opts targs = Tagged . gToJSON opts targs
10591059
{-# INLINE consToJSON' #-}
10601060

@@ -1080,7 +1080,7 @@ instance ( Monoid pairs
10801080
{-# INLINE recordToPairs #-}
10811081

10821082
instance ( Selector s
1083-
, GToJSON enc arity a
1083+
, GToJSON' enc arity a
10841084
, KeyValuePair enc pairs
10851085
) => RecordToPairs enc pairs arity (S1 s a)
10861086
where
@@ -1089,7 +1089,7 @@ instance ( Selector s
10891089

10901090
instance INCOHERENT_
10911091
( Selector s
1092-
, GToJSON enc arity (K1 i (Maybe a))
1092+
, GToJSON' enc arity (K1 i (Maybe a))
10931093
, KeyValuePair enc pairs
10941094
, Monoid pairs
10951095
) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
@@ -1101,7 +1101,7 @@ instance INCOHERENT_
11011101

11021102
instance INCOHERENT_
11031103
( Selector s
1104-
, GToJSON enc arity (K1 i (Maybe a))
1104+
, GToJSON' enc arity (K1 i (Maybe a))
11051105
, KeyValuePair enc pairs
11061106
, Monoid pairs
11071107
) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
@@ -1113,7 +1113,7 @@ instance INCOHERENT_
11131113
{-# INLINE recordToPairs #-}
11141114

11151115
fieldToPair :: (Selector s
1116-
, GToJSON enc arity a
1116+
, GToJSON' enc arity a
11171117
, KeyValuePair enc pairs)
11181118
=> Options -> ToArgs enc arity p
11191119
-> S1 s a p -> pairs
@@ -1146,7 +1146,7 @@ instance ( WriteProduct arity a
11461146
ixR = ix + lenL
11471147
{-# INLINE writeProduct #-}
11481148

1149-
instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1149+
instance OVERLAPPABLE_ (GToJSON' Value arity a) => WriteProduct arity a where
11501150
writeProduct opts targs mv ix _ =
11511151
VM.unsafeWrite mv ix . gToJSON opts targs
11521152
{-# INLINE writeProduct #-}
@@ -1169,13 +1169,13 @@ instance ( EncodeProduct arity a
11691169
encodeProduct opts targs b
11701170
{-# INLINE encodeProduct #-}
11711171

1172-
instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1172+
instance OVERLAPPABLE_ (GToJSON' Encoding arity a) => EncodeProduct arity a where
11731173
encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
11741174
{-# INLINE encodeProduct #-}
11751175

11761176
--------------------------------------------------------------------------------
11771177

1178-
instance ( GToJSON enc arity a
1178+
instance ( GToJSON' enc arity a
11791179
, ConsToJSON enc arity a
11801180
, FromPairs enc pairs
11811181
, KeyValuePair enc pairs

0 commit comments

Comments
 (0)