on master help sign in
Add Missing Value for non-present values.
102d07fJeremy Fitzhardinge
Data/Aeson.hs+1
@@ -24,6 +24,7 @@ module Data.Aeson
24 , ToJSON(..)
24 , ToJSON(..)
25 -- * Constructors and accessors
25 -- * Constructors and accessors
26 , (.=)
26 , (.=)
27+ , (.=?)
27 , (.:)
28 , (.:)
28 , (.:?)
29 , (.:?)
29 , object
30 , object
Data/Aeson/Encode.hs+1
@@ -29,6 +29,7 @@ import qualified Data.Vector as V
29-- | Encode a JSON value to a 'Builder'.
29-- | Encode a JSON value to a 'Builder'.
30fromValue :: Value -> Builder
30fromValue :: Value -> Builder
31fromValue Null = fromByteString "null"
31fromValue Null = fromByteString "null"
32+fromValue Missing = error "Unexpected Missing"
32fromValue (Bool b) = fromByteString $ if b then "true" else "false"
33fromValue (Bool b) = fromByteString $ if b then "true" else "false"
33fromValue (Number n) = fromByteString (B.pack (show n))
34fromValue (Number n) = fromByteString (B.pack (show n))
34fromValue (String s) = string s
35fromValue (String s) = string s
Data/Aeson/Types.hs+14−1
@@ -30,6 +30,7 @@ module Data.Aeson.Types
30 , ToJSON(..)
30 , ToJSON(..)
31 -- * Constructors and accessors
31 -- * Constructors and accessors
32 , (.=)
32 , (.=)
33+ , (.=?)
33 , (.:)
34 , (.:)
34 , (.:?)
35 , (.:?)
35 , object
36 , object
@@ -140,6 +141,7 @@ data Value = Object Object
140 | Number Number
141 | Number Number
141 | Bool !Bool
142 | Bool !Bool
142 | Null
143 | Null
144+ | Missing
143 deriving (Eq, Show, Typeable, Data)
145 deriving (Eq, Show, Typeable, Data)
144
146
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 = ()
154+ rnf Missing = ()
152
155
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 (.=) #-}
168
171
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
174+-- resulting Object.
175+(.=?) :: ToJSON a => Text -> Maybe a -> Pair
176+name .=? Nothing = (name, Missing)
177+name .=? Just value = (name, toJSON value)
178+{-# INLINE (.=?) #-}
179+
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
221+ notmissing _ = True
209{-# INLINE object #-}
222{-# INLINE object #-}
210
223
211-- | A type that can be converted to JSON.
224-- | A type that can be converted to JSON.