Let's say I have a type Person
import GHC.Generics
import Data.Text
import Data.Aeson
import Control.Lens
data Person = Person {
_firstName :: Text,
_lastName :: Text,
_age :: Int
} deriving (Show, Generic)
And I want to automatically derive Lenses and JSON typeclasses for it
makeLenses ''Person
instance FromJSON Person
instance ToJSON Person
This works correctly, however DeriveGeneric sees my field names as having an underscore and expects my JSON to be formatted accordingly.
{ "_firstName": "James" ... etc} -- The underscore doesn't belong here.
Obviously I could remove the underscore from the data
definition itself, but then makeLenses
wouldn't be able to derive the required getters and setters.
Ideally what I want to be able to do is something like this
let person = decode blob
let name = person ^. firstName
i.e. I want to be able to derive lenses and JSON instances with all field names lining up correctly with the values in the JSON-REST Api I'm consuming, without having to write much boilerplate.
This seems like such a straight forward thing that I feel I'm missing something obvious?