Skip to content

Commit

Permalink
Instances for Sum and Product monoid wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceRixte committed Jun 4, 2024
1 parent 16776c4 commit 98d8b56
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,34 @@ instance FromJSON1 Monoid.Dual where
instance FromJSON a => FromJSON (Monoid.Dual a) where
parseJSON = parseJSON1

instance FromJSON1 Monoid.Sum where
liftParseJSON _ p _ a = coerce (p a)

liftParseJSONList _ _ p a = coerce (p a)

liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Monoid.Sum a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList

omittedField = omittedField1

instance FromJSON1 Monoid.Product where
liftParseJSON _ p _ a = coerce (p a)

liftParseJSONList _ _ p a = coerce (p a)

liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Monoid.Product a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList

omittedField = omittedField1


instance FromJSON1 Monoid.First where
liftParseJSON o = coerce (liftParseJSON @Maybe o)
Expand Down
26 changes: 23 additions & 3 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ instance (key ~ Key, value ~ Value) => KeyValue Value (key, value) where
instance value ~ Value => KeyValue Value (KM.KeyMap value) where
(.=) = explicitToField toJSON
{-# INLINE (.=) #-}

explicitToField f name value = KM.singleton name (f value)
{-# INLINE explicitToField #-}

Expand Down Expand Up @@ -2100,12 +2100,32 @@ instance ToJSON a => ToJSON (Monoid.Dual a) where
toEncoding = toEncoding1
omitField = omitField1

instance ToJSON1 Monoid.Sum where
liftToJSON _ t _ = t . Monoid.getSum
liftToEncoding _ t _ = t . Monoid.getSum
liftOmitField = coerce

instance ToJSON a => ToJSON (Monoid.Sum a) where
toJSON = toJSON1
toEncoding = toEncoding1
omitField = omitField1

instance ToJSON1 Monoid.Product where
liftToJSON _ t _ = t . Monoid.getProduct
liftToEncoding _ t _ = t . Monoid.getProduct
liftOmitField = coerce

instance ToJSON a => ToJSON (Monoid.Product a) where
toJSON = toJSON1
toEncoding = toEncoding1
omitField = omitField1

instance ToJSON1 Monoid.First where
liftToJSON o t to' = liftToJSON o t to' . Monoid.getFirst
liftToEncoding o t to' = liftToEncoding o t to' . Monoid.getFirst
liftOmitField :: forall a. (a -> Bool) -> Monoid.First a -> Bool
liftOmitField _ = coerce (isNothing @a)

instance ToJSON a => ToJSON (Monoid.First a) where
toJSON = toJSON1
toEncoding = toEncoding1
Expand Down Expand Up @@ -2168,7 +2188,7 @@ instance ToJSON1 Semigroup.WrappedMonoid where
liftToJSON _ t _ (Semigroup.WrapMonoid x) = t x
liftToEncoding _ t _ (Semigroup.WrapMonoid x) = t x
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
toJSON = toJSON1
toEncoding = toEncoding1
Expand Down
2 changes: 2 additions & 0 deletions tests/PropertyRoundTrip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ roundTripTests =
, testProperty "Nu" $ roundTripEq @(F.Nu (These Char))
, testProperty "Maybe" $ roundTripEq @(Maybe Int)
, testProperty "Monoid.First" $ roundTripEq @(Monoid.First Int)
, testProperty "Monoid.Sum" $ roundTripEq @(Monoid.Sum Int)
, testProperty "Monoid.Product" $ roundTripEq @(Monoid.Product Int)
, testProperty "Strict Pair" $ roundTripEq @(S.Pair Int Char)
, testProperty "Strict Either" $ roundTripEq @(S.Either Int Char)
, testProperty "Strict These" $ roundTripEq @(S.These Int Char)
Expand Down

0 comments on commit 98d8b56

Please sign in to comment.