[Haskell-beginners] Reading JSON using Text.JSON

Magnus Therning magnus at therning.org
Mon May 16 20:59:16 CEST 2011


On Mon, May 16, 2011 at 01:38:02AM +0200, Christopher Done wrote:
> On 15 May 2011 23:20, Magnus Therning <magnus at therning.org> wrote:
> 
> > I wrote down some stuff from my first look at json, maybe it can
> > help you in getting further?
> >
> > http://therning.org/magnus/archives/719
> 
> FWIW, rather than:
> 
> readJSON (JSObject obj) = let
>         jsonObjAssoc = fromJSObject obj
>     in do
>         para <- mLookup "para" jsonObjAssoc >>= readJSON
>         seeAlso <- mLookup "GlossSeeAlso" jsonObjAssoc >>= readJSON
>         return $ GlossDef
>             { glossDefPara = para
>             , glossDefSeeAlso = seeAlso
>             }
> 
> You can just write:
> 
> 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.

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: magnus at therning.org   jabber: magnus at therning.org
twitter: magthe               http://therning.org/magnus

I invented the term Object-Oriented, and I can tell you I did not have
C++ in mind.
     -- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110516/f641a548/attachment.pgp>


More information about the Beginners mailing list