Skip to content

Commit

Permalink
Uniformize instances for newtype monoids
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceRixte committed Jun 6, 2024
1 parent e37d60e commit 562a8f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
32 changes: 5 additions & 27 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2382,67 +2382,45 @@ instance FromJSON a => FromJSON (Monoid.Last a) where
omittedField = omittedField1

instance FromJSON1 Semigroup.Min where
liftParseJSON _ p _ a = coerce (p a)

liftParseJSONList _ _ p a = coerce (p a)
liftParseJSON _ p _ = coerce p

liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Semigroup.Min a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList

omittedField = omittedField1

instance FromJSON1 Semigroup.Max where
liftParseJSON _ p _ a = coerce (p a)
liftParseJSON _ p _ = coerce p

liftParseJSONList _ _ p a = coerce (p a)
liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Semigroup.Max a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList
omittedField = omittedField1

instance FromJSON1 Semigroup.First where
liftParseJSON _ p _ a = coerce (p a)
liftParseJSON _ p _ = coerce p

liftParseJSONList _ _ p a = coerce (p a)
liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Semigroup.First a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList


instance FromJSON1 Semigroup.Last where
liftParseJSON _ p _ a = coerce (p a)
liftParseJSON _ p _ = coerce p

liftParseJSONList _ _ p a = coerce (p a)
liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Semigroup.Last a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList
omittedField = omittedField1

instance FromJSON1 Semigroup.WrappedMonoid where
liftParseJSON _ p _ a = coerce (p a)
liftParseJSON _ p _ = coerce p

liftParseJSONList _ _ p a = coerce (p a)
liftOmittedField = coerce

instance (FromJSON a) => FromJSON (Semigroup.WrappedMonoid a) where
parseJSON = parseJSON1

parseJSONList = liftParseJSONList omittedField parseJSON parseJSONList
omittedField = omittedField1

#if !MIN_VERSION_base(4,16,0)
instance FromJSON1 Semigroup.Option where
liftParseJSON o = coerce (liftParseJSON @Maybe o)
Expand Down
24 changes: 12 additions & 12 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,8 +2091,8 @@ instance ToJSON a => ToJSON (Down a) where
-------------------------------------------------------------------------------

instance ToJSON1 Monoid.Dual where
liftToJSON _ t _ = t . Monoid.getDual
liftToEncoding _ t _ = t . Monoid.getDual
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Monoid.Dual a) where
Expand Down Expand Up @@ -2155,8 +2155,8 @@ instance ToJSON a => ToJSON (Monoid.Last a) where
omitField = omitField1

instance ToJSON1 Semigroup.Min where
liftToJSON _ t _ (Semigroup.Min x) = t x
liftToEncoding _ t _ (Semigroup.Min x) = t x
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.Min a) where
Expand All @@ -2166,8 +2166,8 @@ instance ToJSON a => ToJSON (Semigroup.Min a) where


instance ToJSON1 Semigroup.Max where
liftToJSON _ t _ (Semigroup.Max x) = t x
liftToEncoding _ t _ (Semigroup.Max x) = t x
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.Max a) where
Expand All @@ -2176,8 +2176,8 @@ instance ToJSON a => ToJSON (Semigroup.Max a) where
omitField = omitField1

instance ToJSON1 Semigroup.First where
liftToJSON _ t _ (Semigroup.First x) = t x
liftToEncoding _ t _ (Semigroup.First x) = t x
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.First a) where
Expand All @@ -2186,8 +2186,8 @@ instance ToJSON a => ToJSON (Semigroup.First a) where
omitField = omitField1

instance ToJSON1 Semigroup.Last where
liftToJSON _ t _ (Semigroup.Last x) = t x
liftToEncoding _ t _ (Semigroup.Last x) = t x
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.Last a) where
Expand All @@ -2196,8 +2196,8 @@ instance ToJSON a => ToJSON (Semigroup.Last a) where
omitField = omitField1

instance ToJSON1 Semigroup.WrappedMonoid where
liftToJSON _ t _ (Semigroup.WrapMonoid x) = t x
liftToEncoding _ t _ (Semigroup.WrapMonoid x) = t x
liftToJSON _ t _ = coerce t
liftToEncoding _ t _ = coerce t
liftOmitField = coerce

instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
Expand Down

0 comments on commit 562a8f2

Please sign in to comment.