@@ -162,6 +162,9 @@ class GToJSON' enc arity f where
162162 -- and 'liftToEncoding' (if the @arity@ is 'One').
163163 gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
164164
165+ class GOmitToJSON enc arity f where
166+ gOmitField :: ToArgs enc arity a -> f a -> Bool
167+
165168-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the three
166169-- function arguments that encode occurrences of the type parameter (for
167170-- 'ToJSON1').
@@ -814,6 +817,22 @@ instance ( AllNullary (a :+: b) allNullary
814817 . sumToJSON opts targs
815818 {-# INLINE gToJSON #-}
816819
820+ instance ToJSON a => GOmitToJSON enc arity (K1 i a ) where
821+ gOmitField _ = omitField . unK1
822+ {-# INLINE gOmitField #-}
823+
824+ instance GOmitToJSON enc One Par1 where
825+ gOmitField (To1Args o _ _) = o . unPar1
826+ {-# INLINE gOmitField #-}
827+
828+ instance ToJSON1 f => GOmitToJSON enc One (Rec1 f ) where
829+ gOmitField (To1Args o _ _) = liftOmitField o . unRec1
830+ {-# INLINE gOmitField #-}
831+
832+ instance (ToJSON1 f , GOmitToJSON enc One g ) => GOmitToJSON enc One (f :.: g ) where
833+ gOmitField targs = liftOmitField (gOmitField targs) . unComp1
834+ {-# INLINE gOmitField #-}
835+
817836--------------------------------------------------------------------------------
818837-- Generic toJSON
819838
@@ -1167,47 +1186,14 @@ instance ( Monoid pairs
11671186 {-# INLINE recordToPairs #-}
11681187
11691188instance ( Selector s
1170- , GToJSON' enc arity (K1 i t )
1189+ , GToJSON' enc arity a
1190+ , GOmitToJSON enc arity a
11711191 , KeyValuePair enc pairs
1172- , ToJSON t
1173- ) => RecordToPairs enc pairs arity (S1 s (K1 i t ))
1192+ ) => RecordToPairs enc pairs arity (S1 s a )
11741193 where
11751194 recordToPairs opts targs m1
11761195 | omitNothingFields opts
1177- , omitField (unK1 $ unM1 m1 :: t )
1178- = mempty
1179-
1180- | otherwise =
1181- let key = Key. fromString $ fieldLabelModifier opts (selName m1)
1182- value = gToJSON opts targs (unM1 m1)
1183- in key `pair` value
1184- {-# INLINE recordToPairs #-}
1185-
1186- instance ( Selector s
1187- , GToJSON' enc One (Rec1 f )
1188- , KeyValuePair enc pairs
1189- , ToJSON1 f
1190- ) => RecordToPairs enc pairs One (S1 s (Rec1 f ))
1191- where
1192- recordToPairs opts targs@ (To1Args o _ _) m1
1193- | omitNothingFields opts
1194- , liftOmitField o $ unRec1 $ unM1 m1
1195- = mempty
1196-
1197- | otherwise =
1198- let key = Key. fromString $ fieldLabelModifier opts (selName m1)
1199- value = gToJSON opts targs (unM1 m1)
1200- in key `pair` value
1201- {-# INLINE recordToPairs #-}
1202-
1203- instance ( Selector s
1204- , GToJSON' enc One Par1
1205- , KeyValuePair enc pairs
1206- ) => RecordToPairs enc pairs One (S1 s Par1 )
1207- where
1208- recordToPairs opts targs@ (To1Args o _ _) m1
1209- | omitNothingFields opts
1210- , o (unPar1 (unM1 m1))
1196+ , gOmitField targs $ unM1 m1
12111197 = mempty
12121198
12131199 | otherwise =
0 commit comments