@@ -249,6 +249,9 @@ class GFromJSON arity f where
249
249
-- or 'liftParseJSON' (if the @arity@ is 'One').
250
250
gParseJSON :: Options -> FromArgs arity a -> Value -> Parser (f a )
251
251
252
+ class GOmitFromJSON arity f where
253
+ gOmittedField :: FromArgs arity a -> Maybe (f a )
254
+
252
255
-- | A 'FromArgs' value either stores nothing (for 'FromJSON') or it stores the
253
256
-- three function arguments that decode occurrences of the type parameter (for
254
257
-- 'FromJSON1').
@@ -1013,18 +1016,30 @@ instance (FromJSON a) => GFromJSON arity (K1 i a) where
1013
1016
gParseJSON _opts _ = fmap K1 . parseJSON
1014
1017
{-# INLINE gParseJSON #-}
1015
1018
1019
+ instance FromJSON a => GOmitFromJSON arity (K1 i a ) where
1020
+ gOmittedField _ = fmap K1 omittedField
1021
+ {-# INLINE gOmittedField #-}
1022
+
1016
1023
instance GFromJSON One Par1 where
1017
1024
-- Direct occurrences of the last type parameter are decoded with the
1018
1025
-- function passed in as an argument:
1019
1026
gParseJSON _opts (From1Args _ pj _) = fmap Par1 . pj
1020
1027
{-# INLINE gParseJSON #-}
1021
1028
1029
+ instance GOmitFromJSON One Par1 where
1030
+ gOmittedField (From1Args o _ _) = fmap Par1 o
1031
+ {-# INLINE gOmittedField #-}
1032
+
1022
1033
instance (FromJSON1 f ) => GFromJSON One (Rec1 f ) where
1023
1034
-- Recursive occurrences of the last type parameter are decoded using their
1024
1035
-- FromJSON1 instance:
1025
1036
gParseJSON _opts (From1Args o pj pjl) = fmap Rec1 . liftParseJSON o pj pjl
1026
1037
{-# INLINE gParseJSON #-}
1027
1038
1039
+ instance FromJSON1 f => GOmitFromJSON One (Rec1 f ) where
1040
+ gOmittedField (From1Args o _ _) = fmap Rec1 $ liftOmittedField o
1041
+ {-# INLINE gOmittedField #-}
1042
+
1028
1043
instance (FromJSON1 f , GFromJSON One g ) => GFromJSON One (f :.: g ) where
1029
1044
-- If an occurrence of the last type parameter is nested inside two
1030
1045
-- composed types, it is decoded by using the outermost type's FromJSON1
@@ -1037,6 +1052,10 @@ instance (FromJSON1 f, GFromJSON One g) => GFromJSON One (f :.: g) where
1037
1052
in fmap Comp1 . liftParseJSON Nothing gpj (listParser gpj)
1038
1053
{-# INLINE gParseJSON #-}
1039
1054
1055
+ instance (FromJSON1 f , GOmitFromJSON One g ) => GOmitFromJSON One (f :.: g ) where
1056
+ gOmittedField = fmap Comp1 . liftOmittedField . gOmittedField
1057
+ {-# INLINE gOmittedField #-}
1058
+
1040
1059
--------------------------------------------------------------------------------
1041
1060
1042
1061
instance (GFromJSON' arity a , Datatype d ) => GFromJSON arity (D1 d a ) where
@@ -1423,36 +1442,9 @@ instance ( RecordFromJSON' arity a
1423
1442
<*> recordParseJSON' p obj
1424
1443
{-# INLINE recordParseJSON' #-}
1425
1444
1426
- instance {-# OVERLAPPABLE #-}
1427
- RecordFromJSON' arity f => RecordFromJSON' arity (M1 i s f ) where
1428
- recordParseJSON' args obj = M1 <$> recordParseJSON' args obj
1429
- {-# INLINE recordParseJSON' #-}
1430
-
1431
- instance (Selector s , FromJSON a , Generic a , K1 i a ~ Rep a ) =>
1432
- RecordFromJSON' arity (S1 s (K1 i a )) where
1433
- recordParseJSON' args@ (_ :* _ :* opts :* _) obj =
1434
- recordParseJSONImpl (guard (allowOmittedFields opts) >> fmap K1 omittedField) gParseJSON args obj
1435
- {-# INLINE recordParseJSON' #-}
1436
-
1437
- instance {-# OVERLAPPING #-}
1438
- (Selector s , FromJSON a ) =>
1439
- RecordFromJSON' arity (S1 s (Rec0 a )) where
1440
- recordParseJSON' args@ (_ :* _ :* opts :* _) obj =
1441
- recordParseJSONImpl (guard (allowOmittedFields opts) >> fmap K1 omittedField) gParseJSON args obj
1442
- {-# INLINE recordParseJSON' #-}
1443
-
1444
- instance {-# OVERLAPPING #-}
1445
- (Selector s , GFromJSON One (Rec1 f ), FromJSON1 f ) =>
1446
- RecordFromJSON' One (S1 s (Rec1 f )) where
1447
- recordParseJSON' args@ (_ :* _ :* opts :* From1Args o _ _) obj =
1448
- recordParseJSONImpl (guard (allowOmittedFields opts) >> fmap Rec1 (liftOmittedField o)) gParseJSON args obj
1449
- {-# INLINE recordParseJSON' #-}
1450
-
1451
- instance {-# OVERLAPPING #-}
1452
- (Selector s , GFromJSON One Par1 ) =>
1453
- RecordFromJSON' One (S1 s Par1 ) where
1454
- recordParseJSON' args@ (_ :* _ :* opts :* From1Args o _ _) obj =
1455
- recordParseJSONImpl (guard (allowOmittedFields opts) >> fmap Par1 o) gParseJSON args obj
1445
+ instance (Selector s , GFromJSON arity a , GOmitFromJSON arity a ) => RecordFromJSON' arity (S1 s a ) where
1446
+ recordParseJSON' args@ (_ :* _ :* opts :* fargs) obj =
1447
+ recordParseJSONImpl (guard (allowOmittedFields opts) >> gOmittedField fargs) gParseJSON args obj
1456
1448
{-# INLINE recordParseJSON' #-}
1457
1449
1458
1450
0 commit comments