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