on master help sign in
Improved documentation on encoding Options
5bf02dcBas van Dijk
Data/Aeson/Types/Internal.hs+18−14
@@ -249,7 +249,7 @@ modifyFailure f (Parser p) = Parser $ \kf -> p (kf . f)
249-- Generic and TH encoding configuration
249-- Generic and TH encoding configuration
250--------------------------------------------------------------------------------
250--------------------------------------------------------------------------------
251
251
252−-- | Options that specify how to encode/decode your datatype to/from JSON.
252+-- | Options that specify how to encode\/decode your datatype to\/from JSON.
253data Options = Options
253data Options = Options
254 { fieldNameModifier :: String -> String
254 { fieldNameModifier :: String -> String
255 -- ^ Function applied to field names.
255 -- ^ Function applied to field names.
@@ -257,39 +257,43 @@ data Options = Options
257 , constructorNameModifier :: String -> String
257 , constructorNameModifier :: String -> String
258 -- ^ Function applied to constructor names.
258 -- ^ Function applied to constructor names.
259 -- Handy for lower-casing constructor names for example.
259 -- Handy for lower-casing constructor names for example.
260− , nullaryToString :: Bool
260+ , nullaryToString :: Bool
261− -- ^ If 'True' the constructors of a datatype, with all nullary
261+ -- ^ If 'True' the constructors of a datatype, with /all/ nullary
262 -- constructors, will be encoded to a string with the
262 -- constructors, will be encoded to a string with the
263 -- constructor name. If 'False' the encoding will always follow
263 -- constructor name. If 'False' the encoding will always follow
264 -- the `sumEncoding`.
264 -- the `sumEncoding`.
265 , omitNothingFields :: Bool
265 , omitNothingFields :: Bool
266 -- ^ If 'True' record fields with a 'Nothing' value will be
266 -- ^ If 'True' record fields with a 'Nothing' value will be
267 -- omitted from the resulting object. If 'False' the resulting
267 -- omitted from the resulting object. If 'False' the resulting
268− -- object will include those fields with @null@ values.
268+ -- object will include those fields mapping to @null@.
269− , sumEncoding :: SumEncoding
269+ , sumEncoding :: SumEncoding
270 -- ^ Specifies how to encode constructors of a sum datatype.
270 -- ^ Specifies how to encode constructors of a sum datatype.
271 }
271 }
272
272
273-- | Specifies how to encode constructors of a sum datatype.
273-- | Specifies how to encode constructors of a sum datatype.
274data SumEncoding =
274data SumEncoding =
275− TwoElemArray -- ^ A constructor will be encoded to a 2-element
276− -- array where the first element is the name of the
277− -- constructor (modified by the
278− -- 'constructorNameModifier') and the second element
279− -- the content of the constructor.
280− | ObjectWithType { typeFieldName :: String
275+ ObjectWithType { typeFieldName :: String
281 , valueFieldName :: String
276 , valueFieldName :: String
282 }
277 }
283 -- ^ A constructor will be encoded to an object with a field
278 -- ^ A constructor will be encoded to an object with a field
284 -- 'typeFieldName' which specifies the constructor name (modified
279 -- 'typeFieldName' which specifies the constructor name (modified
285− -- by the 'constructorNameModifier'). If the constructor is not a
280+ -- by the 'constructorNameModifier'). If the constructor is a
281+ -- record the encoded record fields will be unpacked into this
282+ -- object. So make sure that your record doesn't have a field with
283+ -- the same name as the 'typeFieldName'. Otherwise the type gets
284+ -- overwritten by that field. If the constructor is not a record
286− -- record the constructor content will be stored under the
285+ -- the encoded constructor content will be stored under the
287 -- 'valueFieldName' field.
286 -- 'valueFieldName' field.
288 | ObjectWithSingleField
287 | ObjectWithSingleField
289 -- ^ A constructor will be encoded to an object with a single
288 -- ^ A constructor will be encoded to an object with a single
290 -- field named after the constructor (modified by the
289 -- field named after the constructor (modified by the
291− -- 'constructorNameModifier') and the value will be the contents
290+ -- 'constructorNameModifier') which maps to the encoded contents
292 -- of the constructor.
291 -- of the constructor.
292+ | TwoElemArray
293+ -- ^ A constructor will be encoded to a 2-element array where the
294+ -- first element is the name of the constructor (modified by the
295+ -- 'constructorNameModifier') and the second element the encoded
296+ -- content of the constructor.
293
297
294-- | Default encoding 'Options':
298-- | Default encoding 'Options':
295--
299--