
Hi folks. Hackage contains several JSON packages but as far as I see, they all provide 'static' conversion from JSON format to Haskell data type. Is there a method of converting object containing optional filed 'a' to for example Maybe a. Thanks, Sergey

On 02/17/2013 04:01 PM, Sergey Mironov wrote:
Hi folks. Hackage contains several JSON packages but as far as I see, they all provide 'static' conversion from JSON format to Haskell data type. Is there a method of converting object containing optional filed 'a' to for example Maybe a.
Data.Aeson does this with the .:? operator: instance FromJSON Bar where parseJSON (Object v) = Bar <$> v .: "required" <*> v .:? "optional"

On 02/17/2013 03:01 PM, Sergey Mironov wrote:
Hi folks. Hackage contains several JSON packages but as far as I see, they all provide 'static' conversion from JSON format to Haskell data type. Is there a method of converting object containing optional filed 'a' to for example Maybe a. Assuming you have some sort of 'path' to the key in question, aeson-lens might be exactly what you want: http://hackage.haskell.org/package/aeson-lens
I use aeson-lens to turn a list of strings of the form ["foo", "bar", "baz"] into a query into first the 'foo' object, then the 'bar' object, then the 'baz' object, using the following: pathToLens :: Functor f => [T.Text] -> (Maybe Value -> f (Maybe Value)) -> Maybe Value -> f (Maybe Value) pathToLens [p] = key p pathToLens ps = let ps' = filter (not . T.null) ps in key (head ps') . (foldl (.) id . map pathElem $ tail ps') where pathElem p = maybe (key p) nth (readMay $ T.unpack p) So as you can see - I turn any arbitrary path into a query into JSON, with a chance of failure. Hope this helps! - Ollie

2013/2/17 Oliver Charles
On 02/17/2013 03:01 PM, Sergey Mironov wrote:
Hi folks. Hackage contains several JSON packages but as far as I see, they all provide 'static' conversion from JSON format to Haskell data type. Is there a method of converting object containing optional filed 'a' to for example Maybe a.
Assuming you have some sort of 'path' to the key in question, aeson-lens might be exactly what you want: http://hackage.haskell.org/package/aeson-lens
I use aeson-lens to turn a list of strings of the form ["foo", "bar", "baz"] into a query into first the 'foo' object, then the 'bar' object, then the 'baz' object, using the following:
pathToLens :: Functor f => [T.Text] -> (Maybe Value -> f (Maybe Value)) -> Maybe Value -> f (Maybe Value) pathToLens [p] = key p pathToLens ps = let ps' = filter (not . T.null) ps in key (head ps') . (foldl (.) id . map pathElem $ tail ps') where pathElem p = maybe (key p) nth (readMay $ T.unpack p)
So as you can see - I turn any arbitrary path into a query into JSON, with a chance of failure.
Hope this helps! - Ollie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Good news, thanks a lot! Sergey
participants (3)
-
Oliver Charles
-
Sergey Mironov
-
Simon Marechal