Skip to content

Commit 6350e9a

Browse files
committed
class-based composition for parsing side
1 parent c93d60d commit 6350e9a

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/Data/Aeson/Types/FromJSON.hs

+22-30
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ class GFromJSON arity f where
249249
-- or 'liftParseJSON' (if the @arity@ is 'One').
250250
gParseJSON :: Options -> FromArgs arity a -> Value -> Parser (f a)
251251

252+
class GOmitFromJSON arity f where
253+
gOmittedField :: FromArgs arity a -> Maybe (f a)
254+
252255
-- | A 'FromArgs' value either stores nothing (for 'FromJSON') or it stores the
253256
-- three function arguments that decode occurrences of the type parameter (for
254257
-- 'FromJSON1').
@@ -1013,18 +1016,30 @@ instance (FromJSON a) => GFromJSON arity (K1 i a) where
10131016
gParseJSON _opts _ = fmap K1 . parseJSON
10141017
{-# INLINE gParseJSON #-}
10151018

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

1029+
instance GOmitFromJSON One Par1 where
1030+
gOmittedField (From1Args o _ _) = fmap Par1 o
1031+
{-# INLINE gOmittedField #-}
1032+
10221033
instance (FromJSON1 f) => GFromJSON One (Rec1 f) where
10231034
-- Recursive occurrences of the last type parameter are decoded using their
10241035
-- FromJSON1 instance:
10251036
gParseJSON _opts (From1Args o pj pjl) = fmap Rec1 . liftParseJSON o pj pjl
10261037
{-# INLINE gParseJSON #-}
10271038

1039+
instance FromJSON1 f => GOmitFromJSON One (Rec1 f) where
1040+
gOmittedField (From1Args o _ _) = fmap Rec1 $ liftOmittedField o
1041+
{-# INLINE gOmittedField #-}
1042+
10281043
instance (FromJSON1 f, GFromJSON One g) => GFromJSON One (f :.: g) where
10291044
-- If an occurrence of the last type parameter is nested inside two
10301045
-- 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
10371052
in fmap Comp1 . liftParseJSON Nothing gpj (listParser gpj)
10381053
{-# INLINE gParseJSON #-}
10391054

1055+
instance (FromJSON1 f, GOmitFromJSON One g) => GOmitFromJSON One (f :.: g) where
1056+
gOmittedField = fmap Comp1 . liftOmittedField . gOmittedField
1057+
{-# INLINE gOmittedField #-}
1058+
10401059
--------------------------------------------------------------------------------
10411060

10421061
instance (GFromJSON' arity a, Datatype d) => GFromJSON arity (D1 d a) where
@@ -1423,36 +1442,9 @@ instance ( RecordFromJSON' arity a
14231442
<*> recordParseJSON' p obj
14241443
{-# INLINE recordParseJSON' #-}
14251444

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
14561448
{-# INLINE recordParseJSON' #-}
14571449

14581450

0 commit comments

Comments
 (0)