Skip to content

Commit ef29e38

Browse files
committed
class-based composition for parsing side
1 parent 16776c4 commit ef29e38

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ class GFromJSON arity f where
246246
-- or 'liftParseJSON' (if the @arity@ is 'One').
247247
gParseJSON :: Options -> FromArgs arity a -> Value -> Parser (f a)
248248

249+
class GOmitFromJSON arity f where
250+
gOmittedField :: FromArgs arity a -> Maybe (f a)
251+
249252
-- | A 'FromArgs' value either stores nothing (for 'FromJSON') or it stores the
250253
-- three function arguments that decode occurrences of the type parameter (for
251254
-- 'FromJSON1').
@@ -1010,18 +1013,30 @@ instance (FromJSON a) => GFromJSON arity (K1 i a) where
10101013
gParseJSON _opts _ = fmap K1 . parseJSON
10111014
{-# INLINE gParseJSON #-}
10121015

1016+
instance FromJSON a => GOmitFromJSON arity (K1 i a) where
1017+
gOmittedField _ = fmap K1 omittedField
1018+
{-# INLINE gOmittedField #-}
1019+
10131020
instance GFromJSON One Par1 where
10141021
-- Direct occurrences of the last type parameter are decoded with the
10151022
-- function passed in as an argument:
10161023
gParseJSON _opts (From1Args _ pj _) = fmap Par1 . pj
10171024
{-# INLINE gParseJSON #-}
10181025

1026+
instance GOmitFromJSON One Par1 where
1027+
gOmittedField (From1Args o _ _) = fmap Par1 o
1028+
{-# INLINE gOmittedField #-}
1029+
10191030
instance (FromJSON1 f) => GFromJSON One (Rec1 f) where
10201031
-- Recursive occurrences of the last type parameter are decoded using their
10211032
-- FromJSON1 instance:
10221033
gParseJSON _opts (From1Args o pj pjl) = fmap Rec1 . liftParseJSON o pj pjl
10231034
{-# INLINE gParseJSON #-}
10241035

1036+
instance FromJSON1 f => GOmitFromJSON One (Rec1 f) where
1037+
gOmittedField (From1Args o _ _) = fmap Rec1 $ liftOmittedField o
1038+
{-# INLINE gOmittedField #-}
1039+
10251040
instance (FromJSON1 f, GFromJSON One g) => GFromJSON One (f :.: g) where
10261041
-- If an occurrence of the last type parameter is nested inside two
10271042
-- 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
10341049
in fmap Comp1 . liftParseJSON Nothing gpj (listParser gpj)
10351050
{-# INLINE gParseJSON #-}
10361051

1052+
instance (FromJSON1 f, GOmitFromJSON One g) => GOmitFromJSON One (f :.: g) where
1053+
gOmittedField = fmap Comp1 . liftOmittedField . gOmittedField
1054+
{-# INLINE gOmittedField #-}
1055+
10371056
--------------------------------------------------------------------------------
10381057

10391058
instance (GFromJSON' arity a, Datatype d) => GFromJSON arity (D1 d a) where
@@ -1420,36 +1439,9 @@ instance ( RecordFromJSON' arity a
14201439
<*> recordParseJSON' p obj
14211440
{-# INLINE recordParseJSON' #-}
14221441

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
14531445
{-# INLINE recordParseJSON' #-}
14541446

14551447

0 commit comments

Comments
 (0)