@@ -162,6 +162,9 @@ class GToJSON' enc arity f where
162
162
-- and 'liftToEncoding' (if the @arity@ is 'One').
163
163
gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
164
164
165
+ class GOmitToJSON enc arity f where
166
+ gOmitField :: ToArgs enc arity a -> f a -> Bool
167
+
165
168
-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the three
166
169
-- function arguments that encode occurrences of the type parameter (for
167
170
-- 'ToJSON1').
@@ -814,6 +817,22 @@ instance ( AllNullary (a :+: b) allNullary
814
817
. sumToJSON opts targs
815
818
{-# INLINE gToJSON #-}
816
819
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
+
817
836
--------------------------------------------------------------------------------
818
837
-- Generic toJSON
819
838
@@ -1167,47 +1186,14 @@ instance ( Monoid pairs
1167
1186
{-# INLINE recordToPairs #-}
1168
1187
1169
1188
instance ( Selector s
1170
- , GToJSON' enc arity (K1 i t )
1189
+ , GToJSON' enc arity a
1190
+ , GOmitToJSON enc arity a
1171
1191
, KeyValuePair enc pairs
1172
- , ToJSON t
1173
- ) => RecordToPairs enc pairs arity (S1 s (K1 i t ))
1192
+ ) => RecordToPairs enc pairs arity (S1 s a )
1174
1193
where
1175
1194
recordToPairs opts targs m1
1176
1195
| 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
1211
1197
= mempty
1212
1198
1213
1199
| otherwise =
0 commit comments