[Haskell-cafe] Typeclasses question in "Real World Haskell" book
S. Doaitse Swierstra
doaitse at swierstra.net
Mon Jul 26 09:50:05 EDT 2010
How about:
*Main> fromJValue (JBool True) :: Either JSONError Bool
Right True
*Main>
Doaitse
On 26 jul 2010, at 15:16, Angel de Vicente wrote:
> data JValue = JString String
> | JNumber Double
> | JBool Bool
> | JNull
> | JObject [(String, JValue)]
> | JArray [JValue]
> deriving (Eq, Ord, Show)
>
> type JSONError = String
>
> class JSON a where
> toJValue :: a -> JValue
> fromJValue :: JValue -> Either JSONError a
>
> instance JSON JValue where
> toJValue = id
> fromJValue = Right
>
> instance JSON Bool where
> toJValue = JBool
> fromJValue (JBool b) = Right b
> fromJValue _ = Left "not a JSON boolean"
More information about the Haskell-Cafe
mailing list