Created
October 15, 2011 07:36
-
-
Save MichaelXavier/1289221 to your computer and use it in GitHub Desktop.
Recursive operator for parsing values from deeply nested Objects in Aeson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(.:/) :: (FromJSON a) => Object | |
-> [Text] | |
-> Parser a | |
obj .:/ (k:ks) = maybe (fail msg) parseJSON parsed | |
where parsed = deepValue ks =<< M.lookup k obj | |
msg = "Failed to find " ++ (intercalate "/" $ map unpack (k:ks)) | |
obj .:/ [] = parseJSON $ Object obj | |
deepValue :: [Text] | |
-> Value | |
-> Maybe Value | |
deepValue (k:[]) (Object obj) = M.lookup k obj | |
deepValue (k:[]) _ = Nothing | |
deepValue (k:ks) (Object obj) = deepValue ks =<< M.lookup k obj | |
deepValue (k:ks) _ = Nothing | |
deepValue [] v = Just v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment