I am making a REST API for university courses:
data Course = Course {
id :: Maybe Text,
name :: Text,
deleted :: Bool
} deriving(Show, Generic)
instance FromJSON Course
instance ToJSON Course
I would like to allow deleted
to be optional in the serialized JSON structure, but not in my application. I want to set deleted
to False
if it isn't specified when parsing.
I could write a manual instance for FromJSON
, but I don't want to have to write it out for all the fields. I want to declare how deleted is handled and let the automatic instance handle everything else.
How would I do this?
To my knowledge there is not a way to customize the generic instance, but you could structure your type a bit differently:
Now you can do
And now you can just optionally tag a course as deletable when you need it, and the
FromJSON
instances take care of everything.