@@ -30,6 +30,7 @@ module Data.Aeson.Types
30 , ToJSON(..)
30 , ToJSON(..)
31 -- * Constructors and accessors
31 -- * Constructors and accessors
@@ -140,6 +141,7 @@ data Value = Object Object
140 | Number Number
141 | Number Number
141 | Bool !Bool
142 | Bool !Bool
143 deriving (Eq, Show, Typeable, Data)
145 deriving (Eq, Show, Typeable, Data)
145instance NFData Value where
147instance NFData Value where
@@ -149,6 +151,7 @@ instance NFData Value where
149 rnf (Number n) = case n of I i -> rnf i; D d -> rnf d
151 rnf (Number n) = case n of I i -> rnf i; D d -> rnf d
150 rnf (Bool b) = rnf b
152 rnf (Bool b) = rnf b
151 rnf Null = ()
153 rnf Null = ()
153-- | The empty array.
156-- | The empty array.
154emptyArray :: Value
157emptyArray :: Value
@@ -166,6 +169,14 @@ type Pair = (Text, Value)
166name .= value = (name, toJSON value)
169name .= value = (name, toJSON value)
167{-# INLINE (.=) #-}
170{-# INLINE (.=) #-}
172+-- | Construct an optional 'Pair' from a key and a Maybe value. If
173+-- the value is Nothing, then it is omitted altogether from the
175+(.=?) :: ToJSON a => Text -> Maybe a -> Pair
176+name .=? Nothing = (name, Missing)
177+name .=? Just value = (name, toJSON value)
169-- | Convert a value from JSON, failing if the types do not match.
180-- | Convert a value from JSON, failing if the types do not match.
170fromJSON :: (FromJSON a) => Value -> Result a
181fromJSON :: (FromJSON a) => Value -> Result a
171fromJSON = parse parseJSON
182fromJSON = parse parseJSON
@@ -205,7 +216,9 @@ obj .:? key = case M.lookup key obj of
205-- | Create a 'Value' from a list of name\/value 'Pair's. If duplicate
216-- | Create a 'Value' from a list of name\/value 'Pair's. If duplicate
206-- keys arise, earlier keys and their associated values win.
217-- keys arise, earlier keys and their associated values win.
207object :: [Pair] -> Value
218object :: [Pair] -> Value
208−object = Object . M.fromList
219+object = Object . M.fromList . filter (notmissing . snd)
220+ where notmissing Missing = False
209{-# INLINE object #-}
222{-# INLINE object #-}
211-- | A type that can be converted to JSON.
224-- | A type that can be converted to JSON.