
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