Skip to content

Commit

Permalink
Instances for Any and All
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceRixte committed Jun 4, 2024
1 parent 98d8b56 commit 57cd653
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,20 @@ instance (FromJSON a) => FromJSON (Monoid.Product a) where

omittedField = omittedField1

instance FromJSON Monoid.All where
parseJSON = coerce . (parseJSON :: Value -> Parser Bool)

parseJSONList = coerce . (parseJSONList :: Value -> Parser [Bool])

omittedField = coerce (omittedField :: Maybe Bool)

instance FromJSON Monoid.Any where
parseJSON v = coerce (parseJSON v :: Parser Bool)

parseJSONList v = coerce (parseJSONList v :: Parser [Bool])

omittedField = coerce (omittedField :: Maybe Bool)


instance FromJSON1 Monoid.First where
liftParseJSON o = coerce (liftParseJSON @Maybe o)
Expand Down
10 changes: 10 additions & 0 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,16 @@ instance ToJSON a => ToJSON (Monoid.Product a) where
toEncoding = toEncoding1
omitField = omitField1

instance ToJSON Monoid.All where
toJSON = toJSON . Monoid.getAll
toEncoding = toEncoding . Monoid.getAll
omitField = omitField . Monoid.getAll

instance ToJSON Monoid.Any where
toJSON = toJSON . Monoid.getAny
toEncoding = toEncoding . Monoid.getAny
omitField = omitField . Monoid.getAny

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
Expand Down
2 changes: 2 additions & 0 deletions tests/PropertyRoundTrip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ roundTripTests =
, testProperty "Monoid.First" $ roundTripEq @(Monoid.First Int)
, testProperty "Monoid.Sum" $ roundTripEq @(Monoid.Sum Int)
, testProperty "Monoid.Product" $ roundTripEq @(Monoid.Product Int)
, testProperty "Monoid.All" $ roundTripEq @Monoid.All
, testProperty "Monoid.Any" $ roundTripEq @Monoid.Any
, 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 57cd653

Please sign in to comment.