Skip to content

Commit 57cd653

Browse files
committed
Instances for Any and All
1 parent 98d8b56 commit 57cd653

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,20 @@ instance (FromJSON a) => FromJSON (Monoid.Product a) where
23622362

23632363
omittedField = omittedField1
23642364

2365+
instance FromJSON Monoid.All where
2366+
parseJSON = coerce . (parseJSON :: Value -> Parser Bool)
2367+
2368+
parseJSONList = coerce . (parseJSONList :: Value -> Parser [Bool])
2369+
2370+
omittedField = coerce (omittedField :: Maybe Bool)
2371+
2372+
instance FromJSON Monoid.Any where
2373+
parseJSON v = coerce (parseJSON v :: Parser Bool)
2374+
2375+
parseJSONList v = coerce (parseJSONList v :: Parser [Bool])
2376+
2377+
omittedField = coerce (omittedField :: Maybe Bool)
2378+
23652379

23662380
instance FromJSON1 Monoid.First where
23672381
liftParseJSON o = coerce (liftParseJSON @Maybe o)

src/Data/Aeson/Types/ToJSON.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,16 @@ instance ToJSON a => ToJSON (Monoid.Product a) where
21202120
toEncoding = toEncoding1
21212121
omitField = omitField1
21222122

2123+
instance ToJSON Monoid.All where
2124+
toJSON = toJSON . Monoid.getAll
2125+
toEncoding = toEncoding . Monoid.getAll
2126+
omitField = omitField . Monoid.getAll
2127+
2128+
instance ToJSON Monoid.Any where
2129+
toJSON = toJSON . Monoid.getAny
2130+
toEncoding = toEncoding . Monoid.getAny
2131+
omitField = omitField . Monoid.getAny
2132+
21232133
instance ToJSON1 Monoid.First where
21242134
liftToJSON o t to' = liftToJSON o t to' . Monoid.getFirst
21252135
liftToEncoding o t to' = liftToEncoding o t to' . Monoid.getFirst

tests/PropertyRoundTrip.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ roundTripTests =
8989
, testProperty "Monoid.First" $ roundTripEq @(Monoid.First Int)
9090
, testProperty "Monoid.Sum" $ roundTripEq @(Monoid.Sum Int)
9191
, testProperty "Monoid.Product" $ roundTripEq @(Monoid.Product Int)
92+
, testProperty "Monoid.All" $ roundTripEq @Monoid.All
93+
, testProperty "Monoid.Any" $ roundTripEq @Monoid.Any
9294
, testProperty "Strict Pair" $ roundTripEq @(S.Pair Int Char)
9395
, testProperty "Strict Either" $ roundTripEq @(S.Either Int Char)
9496
, testProperty "Strict These" $ roundTripEq @(S.These Int Char)

0 commit comments

Comments
 (0)