[Haskell-beginners] help understanding JSON
lakshminaras2002 at gmail.com
lakshminaras2002 at gmail.com
Tue May 10 11:25:41 CEST 2011
>Mostly this boils down to a complete lack of useful docs
True, I faced problems with respect to functions in System.Posix.IO because
of lack of proper docs.
On Tue, May 10, 2011 at 2:11 PM, Sean Perry <shaleh at speakeasy.net> wrote:
> Thanks Adrien. In that chapter they actually explicitly define all JS
> numbers to be Double and later in the Parsec chapter their parser reads the
> Strings into Floats directly. I was looking for a way to use the library
> provided parsers rather than crafting my own. Sure it is not much code but
> why rewrite what others have already tested?
>
> Mostly this boils down to a complete lack of useful docs. A laundry list of
> functions is not always helpful. At least we could mimic the Python docs
> and add examples.
>
> On May 10, 2011, at 1:05, Adrien Haxaire <adrien at adrienhaxaire.org> wrote:
>
> > Hello,
> >
> > I do not have the right answer for your question, I am just starting to
> learn Haskell.
> >
> > Still, you may want to take a look at the JSON library example described
> in real world Haskell, this can bring you some more clues:
> >
> >
> http://book.realworldhaskell.org/read/writing-a-library-working-with-json-data.html
> >
> > Regards,
> > Adrien
> >
> >
> > On Tue, 10 May 2011 00:51:30 -0700, Sean Perry wrote:
> >> I have some JSON which looks like this:
> >>
> >> {"count":2, "result":[{"LastTrade":"31.24", "Symbol":"FOO"},
> >> {"LastTrade":"345.12",
> "Symbol":"BAR"}]}
> >> (named testQuoteResult below)
> >>
> >> When I try to parse this into types defined below it failed because
> >> the Double is encoded as a String. If I change the type of LastTrade
> >> in my code to String everything is ok but I am left read'ing the
> >> String.
> >>
> >> I have defined the following using Language.JsonGrammar:
> >>
> >> import Data.Iso
> >> import Language.JsonGrammar
> >> import Prelude hiding (id, (.), head, either)
> >> import Control.Category ((.))
> >>
> >> import qualified Data.Aeson as JS
> >> import qualified Data.ByteString.Char8 as BS
> >> import qualified Data.Attoparsec as P
> >>
> >> data MyQuote = MyQuote { price :: Double,
> >> symbol :: String }
> >> deriving(Show)
> >>
> >> myQuote = $(deriveIsos ''MyQuote)
> >>
> >> instance Json MyQuote where
> >> grammar = myQuote . object
> >> (
> >> prop "LastTrade"
> >> . prop "Symbol"
> >> )
> >>
> >> data MyQuoteResult = MyQuoteResult { count :: Int,
> >>
> >> result :: [MyQuote] }
> >> deriving(Show)
> >>
> >> myQuoteResult = $(deriveIsos ''MyQuoteResult)
> >>
> >> instance Json MyQuoteResult where
> >> grammar = myQuoteResult . object
> >> ( prop "count"
> >> . prop "result"
> >> )
> >>
> >> tryJSON input =
> >> case P.parse JS.json (BS.pack input) of
> >> P.Done s v -> Right v
> >> other -> Left ("failed parse " ++ show other)
> >>
> >> Using it like this:
> >>> let Right qr = tryJSON testQuoteResult
> >>> fromJson qr :: Maybe MyQuoteResult
> >> Nothing
> >>
> >> Is there some way to convince the JSON text parser to turn "12.34"
> >> into (Double 12.34)??
> >>
> >> An example using pure aeson would be fine if required. I just decided
> >> to play with JsonGrammar since it was announced today and it sounded
> >> interesting.
> >>
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> Beginners at haskell.org
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards
Lakshmi Narasimhan T V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110510/5c20efd5/attachment-0001.htm>
More information about the Beginners
mailing list