Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Functor Composition in Generic Instances #1103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,13 @@ instance {-# OVERLAPPING #-}
recordParseJSONImpl (guard (allowOmittedFields opts) >> fmap Par1 o) gParseJSON args obj
{-# INLINE recordParseJSON' #-}

instance {-# OVERLAPPING #-}
(Selector s, GFromJSON One (f :.: Rec1 g), FromJSON1 f, FromJSON1 g) =>
RecordFromJSON' One (S1 s (f :.: Rec1 g)) where
recordParseJSON' args@(_ :* _ :* opts :* From1Args o _ _) obj = recordParseJSONImpl d gParseJSON args obj where
d = guard (allowOmittedFields opts) >> fmap Comp1 (liftOmittedField (fmap Rec1 (liftOmittedField o)))
{-# INLINE recordParseJSON' #-}


recordParseJSONImpl :: forall s arity a f i
. (Selector s)
Expand Down
18 changes: 18 additions & 0 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,24 @@ instance ( Selector s
in key `pair` value
{-# INLINE recordToPairs #-}

instance ( Selector s
, GToJSON' enc One (f :.: Rec1 g)
, KeyValuePair enc pairs
, ToJSON1 f
, ToJSON1 g
) => RecordToPairs enc pairs One (S1 s (f :.: Rec1 g))
where
recordToPairs opts targs@(To1Args o _ _) m1
| omitNothingFields opts
, liftOmitField (liftOmitField o . unRec1) $ unComp1 $ unM1 m1
= mempty

| otherwise =
let key = Key.fromString $ fieldLabelModifier opts (selName m1)
value = gToJSON opts targs (unM1 m1)
in key `pair` value
{-# INLINE recordToPairs #-}

--------------------------------------------------------------------------------

class WriteProduct arity f where
Expand Down