Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Last active April 1, 2023 21:16
Show Gist options
  • Select an option

  • Save ramirez7/eadb4d40d62b6fdfd1f8af6057754452 to your computer and use it in GitHub Desktop.

Select an option

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
{-# 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