@@ -213,10 +213,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
213213--
214214-- data Coord = Coord { x :: Double, y :: Double }
215215--
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]
218218--
219−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
219+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
220220-- @
221221--
222222-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -224,7 +224,7 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
224224--
225225-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
226226-- 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.
228228--
229229-- * The compiler can provide a default generic implementation for
230230-- 'toJSON'.
@@ -243,8 +243,8 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
243243--
244244-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
245245--
246−-- instance ToJSON Coord where
247−-- toEncoding = 'genericToEncoding' 'defaultOptions'
246+-- instance 'ToJSON' Coord where
247+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
248248-- @
249249--
250250-- Why do we provide an implementation for 'toEncoding' here? The
@@ -257,15 +257,43 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
257257-- than directly emitting an 'Encoding'. Our one-liner definition of
258258-- 'toEncoding' above bypasses the intermediate 'Value'.
259259--
260−-- If @DefaultSignatures@ doesn't give exactly the results you want,
260+-- If the default implementation doesn't give exactly the results you want,
261261-- you can customize the generic encoding with only a tiny amount of
262262-- 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'.
264293--
265294-- @
266295-- instance ToJSON Coord where
267−-- toJSON = 'genericToJSON' 'defaultOptions'
268−-- toEncoding = 'genericToEncoding' 'defaultOptions'
296+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
269297-- @
270298class ToJSON a where
271299 -- | Convert a Haskell value to a JSON-friendly intermediate type.
@@ -288,8 +316,8 @@ class ToJSON a where
288316 -- extension, and then have GHC generate a method body as follows.
289317 --
290318 -- @
291− -- instance ToJSON Coord where
292− -- toEncoding = 'genericToEncoding' 'defaultOptions'
319+ -- instance 'ToJSON' Coord where
320+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
293321 -- @
294322
295323 toEncoding :: a -> Encoding
@@ -491,7 +519,7 @@ contramapToJSONKeyFunction h x = case x of
491519--
492520-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
493521-- 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.
495523--
496524-- * The compiler can provide a default generic implementation for
497525-- 'toJSON1'.
@@ -509,19 +537,25 @@ contramapToJSONKeyFunction h x = case x of
509537--
510538-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
511539--
512−-- instance ToJSON a => ToJSON1 (Pair a)
540+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
513541-- @
514542--
515−-- If @DefaultSignatures@ doesn't give exactly the results you want,
543+-- If the default implementation doesn't give exactly the results you want,
516544-- you can customize the generic encoding with only a tiny amount of
517545-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
518546-- your preferred 'Options':
519547--
520548-- @
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
524556-- @
557+--
558+-- See also 'ToJSON'.
525559class ToJSON1 f where
526560 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
527561
@@ -588,7 +622,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
588622-- @
589623-- newtype F a = F [a]
590624--
591−-- -- This instance encodes String as an array of chars
625+-- -- This instance encodes 'String' as an array of chars
592626-- instance 'ToJSON1' F where
593627-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
594628-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs