Permalink
Browse files

Export (<.:>), handle the list of empty paths, handle the next elemen…

…t in the traversal not being an Object, add an explicit type signature.
  • Loading branch information...
1 parent 1ee4fa8 commit f24ca5c019615f68da1d1da9e3242b11c46df805 @mike-burns committed Dec 29, 2011
Showing with 8 additions and 3 deletions.
  1. +1 −0 Data/Aeson/Types.hs
  2. +7 −3 Data/Aeson/Types/Class.hs
View
@@ -37,6 +37,7 @@ module Data.Aeson.Types
, (.:)
, (.:?)
, (.!=)
+ , (<.:>)
, object
) where
View
@@ -36,6 +36,7 @@ module Data.Aeson.Types.Class
, (.:?)
, (.!=)
, (.=)
+ , (<.:>)
, typeMismatch
) where
@@ -770,11 +771,14 @@ pmval .!= val = fromMaybe val <$> pmval
-- Example usage:
--
-- > o <.:> ["_links", "comments", "href"]
+(<.:>) :: (FromJSON a) => Object -> [Text] -> Parser a
+_ <.:> [] = fail $ "attempted to traverse an Object without a path"
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
+ case next of
+ (Object nextObj) -> nextObj <.:> keys
+ other -> typeMismatch "Object" other
+ where next = fromMaybe (Object H.empty) $ H.lookup key obj
-- | Fail parsing due to a type mismatch, with a descriptive message.
typeMismatch :: String -- ^ The name of the type you are trying to parse.

0 comments on commit f24ca5c

Please sign in to comment.