I saw this question on SO and am trying to replicate it: Haskell: Reusing FromJSON instances with lenses, lens-aeson, and nested JSON
However, when I run what I think should be a complete example, I get an error.
Here is my code:
import Data.Aeson
import Data.Aeson.Lens
import Control.Lens
import Data.Aeson.Types
data Colour = Yellow | Green | Blue
instance FromJSON Colour where
parseJSON (String s) = return $ case s of
"blue" -> Blue
"green" -> Green
_ -> Yellow
parseJSON _ = mzero
instance ToJSON Colour where
toJSON Yellow = String "yellow"
toJSON Blue = String "blue"
toJSON Green = String "green"
parseColour :: String -> Maybe Colour
parseColour j = j ^? key "info" . key "colour" . _JSON
parseColour "{ \"info\": { \"colour\": \"yellow\" } }"
I get this:
<interactive>:7:17: error: Variable not in scope: mzero :: Parser Colour
A lot of searches show an mzero variable used like this, successfully. I can't tell if it's something imported from a package or just an arbitrary variable name and I'm misusing the function. Either way, it's not clear to me why copying this code from the question seems to fail on this.