@@ -213,18 +213,18 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
213
213
--
214
214
-- data Coord = Coord { x :: Double, y :: Double }
215
215
--
216
- -- instance ToJSON Coord where
217
- -- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
216
+ -- instance ' ToJSON' Coord where
217
+ -- ' toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
218
218
--
219
- -- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
219
+ -- ' toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
220
220
-- @
221
221
--
222
222
-- Instead of manually writing your 'ToJSON' instance, there are two options
223
223
-- to do it automatically:
224
224
--
225
225
-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
226
226
-- instance at compile time. The generated instance is optimized for your type
227
- -- so will probably be more efficient than the following two options:
227
+ -- so it will probably be more efficient than the following option.
228
228
--
229
229
-- * The compiler can provide a default generic implementation for
230
230
-- 'toJSON'.
@@ -243,8 +243,8 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
243
243
--
244
244
-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
245
245
--
246
- -- instance ToJSON Coord where
247
- -- toEncoding = 'genericToEncoding' 'defaultOptions'
246
+ -- instance ' ToJSON' Coord where
247
+ -- ' toEncoding' = 'genericToEncoding' 'defaultOptions'
248
248
-- @
249
249
--
250
250
-- Why do we provide an implementation for 'toEncoding' here? The
@@ -257,15 +257,43 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
257
257
-- than directly emitting an 'Encoding'. Our one-liner definition of
258
258
-- 'toEncoding' above bypasses the intermediate 'Value'.
259
259
--
260
- -- If @DefaultSignatures@ doesn't give exactly the results you want,
260
+ -- If the default implementation doesn't give exactly the results you want,
261
261
-- you can customize the generic encoding with only a tiny amount of
262
262
-- effort, using 'genericToJSON' and 'genericToEncoding' with your
263
- -- preferred 'Options':
263
+ -- preferred 'Options'.
264
+ --
265
+ -- The following examples ensure that 'toEncoding' represents the same
266
+ -- encoding as 'toJSON'.
267
+ --
268
+ -- - A typical implementation defines both 'toJSON' and 'toEncoding'
269
+ -- with the same options.
270
+ --
271
+ -- @
272
+ -- customOptions = 'defaultOptions'
273
+ -- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
274
+ -- }
275
+ --
276
+ -- instance 'ToJSON' Coord where
277
+ -- 'toJSON' = 'genericToJSON' customOptions
278
+ -- 'toEncoding' = 'genericToEncoding' customOptions
279
+ -- @
280
+ --
281
+ -- - A minimal definition of 'ToJSON' is given by 'toJSON'.
282
+ -- This is most useful to define an instance manually when
283
+ -- the generic options are not satisfactory.
284
+ --
285
+ -- @
286
+ -- instance 'ToJSON' Coord where
287
+ -- 'toJSON' (Coord x y) = 'Array' (toJSON [x, y])
288
+ -- @
289
+ --
290
+ -- - The default implementation of 'toJSON' uses 'defaultOptions'. If you leave
291
+ -- 'toJSON' implicit while providing a more efficient explicit implementation
292
+ -- of 'toEncoding', then it *must* be equivalent to using 'defaultOptions'.
264
293
--
265
294
-- @
266
295
-- instance ToJSON Coord where
267
- -- toJSON = 'genericToJSON' 'defaultOptions'
268
- -- toEncoding = 'genericToEncoding' 'defaultOptions'
296
+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
269
297
-- @
270
298
class ToJSON a where
271
299
-- | Convert a Haskell value to a JSON-friendly intermediate type.
@@ -288,8 +316,8 @@ class ToJSON a where
288
316
-- extension, and then have GHC generate a method body as follows.
289
317
--
290
318
-- @
291
- -- instance ToJSON Coord where
292
- -- toEncoding = 'genericToEncoding' 'defaultOptions'
319
+ -- instance ' ToJSON' Coord where
320
+ -- ' toEncoding' = 'genericToEncoding' 'defaultOptions'
293
321
-- @
294
322
295
323
toEncoding :: a -> Encoding
@@ -491,7 +519,7 @@ contramapToJSONKeyFunction h x = case x of
491
519
--
492
520
-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
493
521
-- instance at compile time. The generated instance is optimized for your type
494
- -- so will probably be more efficient than the following two options:
522
+ -- so it will probably be more efficient than the following option.
495
523
--
496
524
-- * The compiler can provide a default generic implementation for
497
525
-- 'toJSON1'.
@@ -509,19 +537,25 @@ contramapToJSONKeyFunction h x = case x of
509
537
--
510
538
-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
511
539
--
512
- -- instance ToJSON a => ToJSON1 (Pair a)
540
+ -- instance ' ToJSON' a => ' ToJSON1' (Pair a)
513
541
-- @
514
542
--
515
- -- If @DefaultSignatures@ doesn't give exactly the results you want,
543
+ -- If the default implementation doesn't give exactly the results you want,
516
544
-- you can customize the generic encoding with only a tiny amount of
517
545
-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
518
546
-- your preferred 'Options':
519
547
--
520
548
-- @
521
- -- instance ToJSON a => ToJSON1 (Pair a) where
522
- -- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
523
- -- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
549
+ -- customOptions = 'defaultOptions'
550
+ -- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
551
+ -- }
552
+ --
553
+ -- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
554
+ -- 'liftToJSON' = 'genericLiftToJSON' customOptions
555
+ -- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
524
556
-- @
557
+ --
558
+ -- See also 'ToJSON'.
525
559
class ToJSON1 f where
526
560
liftToJSON :: (a -> Value ) -> ([a ] -> Value ) -> f a -> Value
527
561
@@ -588,7 +622,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
588
622
-- @
589
623
-- newtype F a = F [a]
590
624
--
591
- -- -- This instance encodes String as an array of chars
625
+ -- -- This instance encodes ' String' as an array of chars
592
626
-- instance 'ToJSON1' F where
593
627
-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
594
628
-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
0 commit comments