I am trying to rename keys when serializing an object to json.
I understand the way to do that is to, rather than just using deriving generic, define an instance with custom key names like so:
-- instance ToJSON R2 -- old
instance ToJSON R2 where
toJSON (R2 recCode recDate) = object [ "code" .= recCode , "date" .= recDate ]
-- new
However, this gives me:
<interactive>:2:70: error:
Ambiguous occurrence ‘.=’
It could refer to either ‘Data.Aeson..=’, imported from ‘Data.Aeson’ (and originally defined in ‘aeson-1.3.1.1:Data.Aeson.Types.ToJSON’)
or ‘Control.Lens..=’, imported from ‘Control.Lens’ (and originally defined in ‘Control.Lens.Setter’)
My attempt to fix this was to explicitly force the meaning of the .=
operator by defining it in my code, eg:
(.=) = Data.Aeson.(.=)
This was a guess, but seems like the wrong syntax. I added the parens by analogy to the following resources:
- https://hackage.haskell.org/package/aeson-0.6.1.0/docs/src/Data-Aeson-Types-Class.html#.%3D
- http://www.stephendiehl.com/posts/protolude.html
- https://stackoverflow.com/a/18372384/1052117
This gave me this error:
(.=) = Data.Aeson (.=)
<interactive>:1:8: error:
Not in scope: data constructor ‘Data.Aeson’
No module named ‘Data’ is imported.
What is the correct syntax to say, "let .=
be unambiguously the .=
from Data.Aeson
" ?