Last active
April 1, 2023 21:16
-
-
Save ramirez7/eadb4d40d62b6fdfd1f8af6057754452 to your computer and use it in GitHub Desktop.
You can use `Maybe Void` to drop a field from a JSON object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE ImportQualifiedPost #-} | |
| {-# LANGUAGE DeriveGeneric #-} | |
| module MaybeVoidAeson where | |
| import Data.Aeson qualified as Ae | |
| import GHC.Generics | |
| data T a = T { x :: Int, y :: a } deriving Generic | |
| instance {-# OVERLAPPING #-} Ae.ToJSON a => Ae.ToJSON (T (Maybe a)) where | |
| toJSON = Ae.genericToJSON Ae.defaultOptions { Ae.omitNothingFields = True } | |
| instance Ae.ToJSON a => Ae.ToJSON (T a) | |
| {- | |
| λ Ae.encode $ T 2 (Nothing :: Maybe Void) | |
| "{\"x\":2}" | |
| -} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment