[Haskell-beginners] Reading JSON using Text.JSON

Adrien Haxaire adrien at adrienhaxaire.org
Mon May 16 22:40:23 CEST 2011


Hello,

Thanks for your help and advices, I managed to read the sample file I have !

At the moment I can read a node like this :

instance JSON Node where
   readJSON object = do
     obj <- readJSON object
     coords <- valFromObj "coordinates" obj
     number <- valFromObj "number" obj
     return (Node coords number)
   showJSON (Node coords number) = makeObj [("coordinates",showJSON 
coords),("number", showJSON number)]

I will take a look at how to extend it, as I will have much more than 
nodes to read.

Still, I like the solution using <$>, <*>, etc, and would like to use 
it. I will need to read more about Applicative typeclasses before, 
though. But that's why I wanted to learn Haskell: I have many things to 
discover :)

The only thing that I do not understand yet, is why I can't use the 
'Node' data which I defined in another module. If the Node data 
declaration is in the file in which I read the JSON, it is ok. If I 
import the module in which I want to place it, ghc tells me that the 
data constructor 'Node' is not in scope.

in Input.hs:
import Elements (Node)

and in Elements.hs:


module Elements (
        Node(..)
        , Element(..)
        , Material(..)
        ) where
[...]
data Node = Node Coordinates Number
           deriving (Eq, Ord, Show)

Regarding pattern matching, I really like it. I am just discovering that 
it is way more powerful than I first thought!


To recap, the clouds which where on this JSON handling are gone, now it 
is mainly practicing it which will make me do precisely what I want with 
it.

Thanks a lot again for your tutorial, help and comments. This is very 
cheerful, and mmakes me as a part of the community, even if I am just 
starting :)

best regards,
Adrien




Le 16/05/2011 21:21, Magnus Therning a écrit :
> On Mon, May 16, 2011 at 09:07:25PM +0200, Christopher Done wrote:
>> On 16 May 2011 20:59, Magnus Therning<magnus at therning.org>  wrote:
>>>> readJSON object = do obj<- readJSON object
>>>>                       GlossDef<$>  valFromObj "para" obj
>>>>                                <*>  valFromObj "GlossSeeAlso" obj
>>>
>>> FWIW, read the first comment ;-)
>>>
>>> I still consider the monadic style easier to read than the
>>> applicative so that's what I usually start with.
>>
>> I was more referring to the fact that you're re-implementing
>> valFromObj and needlessly pattern matching.
>
> Indeed I am, thanks for pointing that out.
>
> /M
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list