[Haskell-beginners] aeson json paring

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Sat Sep 27 04:20:11 UTC 2014


On 09/26/2014 03:34 PM, Miro Karpis wrote:
> Hi, I'm trying to run an example from the eason documentation
> <https://hackage.haskell.org/package/aeson-0.6.1.0/docs/Data-Aeson.html#g:3>
> :
> 
> λ> do result <- decode "{\"name\":\"Dave\",\"age\":2}"
>        flip parseMaybe result $ \obj -> do
>          age <- obj .: "age"
>          name <- obj .: "name"
>          return (name ++ ": " ++ show (age*2))
> 
> 
> I made a function:
> 
> jsonTest = do
> 	result <- decode "{\"name\":\"Dave\",\"age\":2}"
> 	flip parseMaybe result $ \obj -> do
> 		age <- obj .: "age"
> 		name <- obj .: "name"
> 		return (name ++ ": " ++ show (age*2))
> 
> which can not compile:
> hh.hs:35:41:
>     No instance for (Show a0) arising from a use of ‘show’
>     The type variable ‘a0’ is ambiguous
> ....
> ...
> 
> 
> Please what am I doing wrong?
> 
> Cheers, Miro
> 

As pointed out in the other response, you'll need a type signature
somewhere to indicate what type your ‘age’ should be.

I'd like to point out something else however. You appear to be using
hard tabs (‘\t’ character): please don't do this! Haskell is
alignment-sensitive and you'll run into problems due to this unless you
*very* strictly only insert tabs. FTR GHC treats one \t as 8 spaces.
There are beginners on daily basis in #haskell wondering why their code
doesn't compile just to discover tabs once they share it through
lpaste.net or similar service.

[1] is the standard reference for style, especially see the ‘Tabs’ section.

So, please, set your editor to only insert spaces.

[1]: http://urchin.earth.li/~ian/style/haskell.html

-- 
Mateusz K.


More information about the Beginners mailing list