I'm using Aeson to parse json quote data from Yahoo's API. A quote might look like this:
{
"date": "2010-03-10",
"Date": "2010-03-10",
"Open": "0.37",
"High": "0.37",
"Low": "0.34",
"Close": "0.35",
"Volume": "443000",
"Adj_Close": "0.35"
}
(that's using this YQL query)
As you can see, the numbers are quoted. I can write a fromJSON
implementation like this:
instance FromJSON Quote where
parseJSON (Object o) =
Quote <$> o .: "Date"
<*> o .: "Open"
<*> o .: "High"
<*> o .: "Low"
<*> o .: "Close"
<*> o .: "Volume"
parseJSON _ = mzero
which is the same as what would be derived. Unfortunately this only works if I want Open, High, Low, etc, to be a Text type. The parse fails if I try and have any of those fields as Double, say.
I can write this:
<*> (fmap read $ o .: "Open")
to get it as anything I like, but this uses read
, which is a partial function. How would I get the above functionality without using a partial function?