Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…ng to the given path.
  • Loading branch information...
commit 1ee4fa85cf5272e8bcc1201b4cc64ac982c80fb7 1 parent 017fdb4
@mike-burns authored
Showing with 11 additions and 0 deletions.
  1. +11 −0 Data/Aeson/Types/Class.hs
View
11 Data/Aeson/Types/Class.hs
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.