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