Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
Created October 15, 2011 07:36
Show Gist options
  • Save MichaelXavier/1289221 to your computer and use it in GitHub Desktop.
Save MichaelXavier/1289221 to your computer and use it in GitHub Desktop.
Recursive operator for parsing values from deeply nested Objects in Aeson
(.:/) :: (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