[Haskell-beginners] help understanding JSON

Adrien Haxaire adrien at adrienhaxaire.org
Tue May 10 10:05:57 CEST 2011


 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




More information about the Beginners mailing list