From 57cd653f66befeb899c78e4899e76f06bb792b9a Mon Sep 17 00:00:00 2001 From: autonym8 Date: Tue, 4 Jun 2024 14:48:36 +0200 Subject: [PATCH] Instances for Any and All --- src/Data/Aeson/Types/FromJSON.hs | 14 ++++++++++++++ src/Data/Aeson/Types/ToJSON.hs | 10 ++++++++++ tests/PropertyRoundTrip.hs | 2 ++ 3 files changed, 26 insertions(+) diff --git a/src/Data/Aeson/Types/FromJSON.hs b/src/Data/Aeson/Types/FromJSON.hs index 869e6b94..3ccb0d58 100644 --- a/src/Data/Aeson/Types/FromJSON.hs +++ b/src/Data/Aeson/Types/FromJSON.hs @@ -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) diff --git a/src/Data/Aeson/Types/ToJSON.hs b/src/Data/Aeson/Types/ToJSON.hs index e62b46f8..9cd768b0 100644 --- a/src/Data/Aeson/Types/ToJSON.hs +++ b/src/Data/Aeson/Types/ToJSON.hs @@ -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 diff --git a/tests/PropertyRoundTrip.hs b/tests/PropertyRoundTrip.hs index 7a8a5281..b2f3bc09 100644 --- a/tests/PropertyRoundTrip.hs +++ b/tests/PropertyRoundTrip.hs @@ -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)