Permalink
Browse files

Add (<.:>), which is like (.:) but traverses through the JSON accordi…

…ng to the given path.
  • Loading branch information...
1 parent 017fdb4 commit 1ee4fa85cf5272e8bcc1201b4cc64ac982c80fb7 @mike-burns committed Dec 29, 2011
Showing with 11 additions and 0 deletions.
  1. +11 −0 Data/Aeson/Types/Class.hs
View
@@ -765,6 +765,17 @@ obj .:? key = case H.lookup key obj of
pmval .!= val = fromMaybe val <$> pmval
{-# INLINE (.!=) #-}
+-- | Produce the value for the last key by traversing through the JSON.
+--
+-- Example usage:
+--
+-- > o <.:> ["_links", "comments", "href"]
+obj <.:> [key] = obj .: key
+obj <.:> (key:keys) =
+ let (Object nextObj) = findWithDefault (Object H.empty) key obj in
+ nextObj <.:> keys
+ where findWithDefault def k m = fromMaybe def $ H.lookup k m
+
-- | Fail parsing due to a type mismatch, with a descriptive message.
typeMismatch :: String -- ^ The name of the type you are trying to parse.
-> Value -- ^ The actual value encountered.

0 comments on commit 1ee4fa8

Please sign in to comment.