aeson json paring

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

aeson json paring

Miro Karpis
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140926/f1394940/attachment.html>

Reply | Threaded
Open this post in threaded view
|

aeson json paring

Raphaël Mongeau
The key here is "The type variable ?a0? is ambiguous".

You need to add a type signature to age so the compiler know if age is of
type In,Float or Double ...

:)

2014-09-26 10:34 GMT-04:00 Miro Karpis <miroslav.karpis at gmail.com>:

> 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
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


--
Viva Cila
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140926/0f8002eb/attachment.html>

Reply | Threaded
Open this post in threaded view
|

aeson json paring

Mateusz Kowalczyk
In reply to this post by Miro Karpis
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.