In my code I use a lot of newtype
declarations, like:
newtype PersonName = PersonName { personName :: Text }
newtype PetName = PetName { petName :: Text }
(In practice I use lenses to avoid the cumbersome names for the accessor functions.)
However, if I derive the instance from ToJSON
and FromJSON
automatically, the resulting JSON will be of the form:
{ "personName": "The person name" }
{ "petName": "The pet name" }
Is there a way to avoid the boilerplate of declaring trivial instances of ToJSON
and FromJSON
for the newtype
s above, in such a way that the resulting JSON objects will be of the form:
"The person name"
"The pet name"
You can use GeneralizedNewtypeDeriving
to derive the instances.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype PersonName = PersonName { personName :: Text }
deriving (ToJSON, FromJSON)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With