on master help sign in
Add ./ operator for traversing Object heirarchies.

./ allows an Object heirarchy to be traversed without parsing anything. If any of the path components is missing or the wrong type, it returns Missing.

commitbbc11eb1086f0af5e1fe9e4a0d2d5768e91f2979 authorJeremy Fitzhardinge <jeremy@goop.org> dateThu 17 Feb 2011, 17:42 (16 years ago) parent102d07f
2 files changed+12
Data/Aeson.hs+1 Data/Aeson/Types.hs+11
Data/Aeson.hs+1
@@ -27,6 +27,7 @@ module Data.Aeson
2727 , (.=?)
2828 , (.:)
2929 , (.:?)
30+ , (./)
3031 , object
3132 -- * Encoding and parsing
3233 , encode
Data/Aeson/Types.hs+11
@@ -33,6 +33,7 @@ module Data.Aeson.Types
3333 , (.=?)
3434 , (.:)
3535 , (.:?)
36+ , (./)
3637 , object
3738 ) where
3839
@@ -213,6 +214,16 @@ obj .:? key = case M.lookup key obj of
213214 Just v -> parseJSON v
214215{-# INLINE (.:?) #-}
215216
217+-- | Traverse a object heirarchy.
218+-- This is useful for traversing a JSON object to get to an
219+-- appropriate place before parsing.
220+(./) :: Value -> Text -> Value
221+(Object obj) ./ key = case M.lookup key obj of
222+ Nothing -> Missing
223+ Just v -> v
224+_ ./ _ = Missing
225+{-# INLINE (./) #-}
226+
216227-- | Create a 'Value' from a list of name\/value 'Pair's. If duplicate
217228-- keys arise, earlier keys and their associated values win.
218229object :: [Pair] -> Value