@@ -281,12 +281,12 @@ genericLiftParseJSON opts pj pjl = fmap to1 . gParseJSON opts (From1Args pj pjl)
281
281
--
282
282
-- The basic ways to signal a failed conversion are as follows:
283
283
--
284
- -- * 'empty' and 'mzero' work, but are terse and uninformative
284
+ -- * 'empty' and 'mzero' work, but are terse and uninformative;
285
285
--
286
- -- * 'fail' yields a custom error message
286
+ -- * 'fail' yields a custom error message;
287
287
--
288
288
-- * 'typeMismatch' produces an informative message for cases when the
289
- -- value encountered is not of the expected type
289
+ -- value encountered is not of the expected type.
290
290
--
291
291
-- An example type and instance using 'typeMismatch':
292
292
--
@@ -296,27 +296,27 @@ genericLiftParseJSON opts pj pjl = fmap to1 . gParseJSON opts (From1Args pj pjl)
296
296
--
297
297
-- data Coord = Coord { x :: Double, y :: Double }
298
298
--
299
- -- instance FromJSON Coord where
300
- -- parseJSON ('Object' v) = Coord
301
- -- '<$>' v '.:' \"x\"
299
+ -- instance ' FromJSON' Coord where
300
+ -- ' parseJSON' ('Object' v) = Coord
301
+ -- '<$>' v '.:' \"x\"
302
302
-- '<*>' v '.:' \"y\"
303
303
--
304
304
-- \-- We do not expect a non-'Object' value here.
305
305
-- \-- We could use 'mzero' to fail, but 'typeMismatch'
306
306
-- \-- gives a much more informative error message.
307
- -- parseJSON invalid = 'typeMismatch' \"Coord\" invalid
307
+ -- ' parseJSON' invalid = 'typeMismatch' \"Coord\" invalid
308
308
-- @
309
309
--
310
310
-- For this common case of only being concerned with a single
311
- -- type of JSON value, the functions @ withObject@, @ withNumber@ , etc.
311
+ -- type of JSON value, the functions ' withObject', ' withNumber' , etc.
312
312
-- are provided. Their use is to be preferred when possible, since
313
- -- they are more terse. Using @ withObject@ , we can rewrite the above instance
313
+ -- they are more terse. Using ' withObject' , we can rewrite the above instance
314
314
-- (assuming the same language extension and data type) as:
315
315
--
316
316
-- @
317
- -- instance FromJSON Coord where
318
- -- parseJSON = withObject \"Coord\" $ \v -> Coord
319
- -- '<$>' v '.:' \"x\"
317
+ -- instance ' FromJSON' Coord where
318
+ -- ' parseJSON' = ' withObject' \"Coord\" $ \v -> Coord
319
+ -- '<$>' v '.:' \"x\"
320
320
-- '<*>' v '.:' \"y\"
321
321
-- @
322
322
--
@@ -325,7 +325,7 @@ genericLiftParseJSON opts pj pjl = fmap to1 . gParseJSON opts (From1Args pj pjl)
325
325
--
326
326
-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
327
327
-- instance at compile time. The generated instance is optimized for your type
328
- -- so will probably be more efficient than the following two options:
328
+ -- so it will probably be more efficient than the following option.
329
329
--
330
330
-- * The compiler can provide a default generic implementation for
331
331
-- 'parseJSON'.
@@ -343,18 +343,21 @@ genericLiftParseJSON opts pj pjl = fmap to1 . gParseJSON opts (From1Args pj pjl)
343
343
--
344
344
-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
345
345
--
346
- -- instance FromJSON Coord
346
+ -- instance ' FromJSON' Coord
347
347
-- @
348
348
--
349
- -- If @DefaultSignatures@ doesn't give exactly the results you want,
350
- -- you can customize the generic decoding with only a tiny amount of
351
- -- effort, using 'genericParseJSON' with your preferred 'Options' :
349
+ -- The default implementation will be equivalent to
350
+ -- @parseJSON = 'genericParseJSON' 'defaultOptions'@; If you need different
351
+ -- options, you can customize the generic decoding by defining :
352
352
--
353
353
-- @
354
- -- instance FromJSON Coord where
355
- -- parseJSON = 'genericParseJSON' 'defaultOptions'
354
+ -- customOptions = 'defaultOptions'
355
+ -- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
356
+ -- }
357
+ --
358
+ -- instance 'FromJSON' Coord where
359
+ -- 'parseJSON' = 'genericParseJSON' customOptions
356
360
-- @
357
-
358
361
class FromJSON a where
359
362
parseJSON :: Value -> Parser a
360
363
@@ -516,7 +519,7 @@ typeMismatch expected actual =
516
519
--
517
520
-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
518
521
-- instance at compile time. The generated instance is optimized for your type
519
- -- so will probably be more efficient than the following two options:
522
+ -- so it will probably be more efficient than the following option.
520
523
--
521
524
-- * The compiler can provide a default generic implementation for
522
525
-- 'liftParseJSON'.
@@ -534,16 +537,20 @@ typeMismatch expected actual =
534
537
--
535
538
-- data Pair a b = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
536
539
--
537
- -- instance FromJSON a => FromJSON1 (Pair a)
540
+ -- instance ' FromJSON' a => ' FromJSON1' (Pair a)
538
541
-- @
539
542
--
540
- -- If @DefaultSignatures@ doesn't give exactly the results you want,
543
+ -- If the default implementation doesn't give exactly the results you want,
541
544
-- you can customize the generic decoding with only a tiny amount of
542
545
-- effort, using 'genericLiftParseJSON' with your preferred 'Options':
543
546
--
544
547
-- @
545
- -- instance FromJSON a => FromJSON1 (Pair a) where
546
- -- liftParseJSON = 'genericLiftParseJSON' 'defaultOptions'
548
+ -- customOptions = 'defaultOptions'
549
+ -- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
550
+ -- }
551
+ --
552
+ -- instance 'FromJSON' a => 'FromJSON1' (Pair a) where
553
+ -- 'liftParseJSON' = 'genericLiftParseJSON' customOptions
547
554
-- @
548
555
class FromJSON1 f where
549
556
liftParseJSON :: (Value -> Parser a ) -> (Value -> Parser [a ]) -> Value -> Parser (f a )
@@ -614,43 +621,44 @@ instance (FromJSON a) => FromJSON [a] where
614
621
-- Functions
615
622
-------------------------------------------------------------------------------
616
623
617
- -- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object @
618
- -- and fails using @'typeMismatch' expected@ otherwise.
624
+ -- | @' withObject' expected f value@ applies @f@ to the 'Object' when @value@
625
+ -- is an 'Object' and fails using @'typeMismatch' expected@ otherwise.
619
626
withObject :: String -> (Object -> Parser a ) -> Value -> Parser a
620
627
withObject _ f (Object obj) = f obj
621
628
withObject expected _ v = typeMismatch expected v
622
629
{-# INLINE withObject #-}
623
630
624
- -- | @withText expected f value@ applies @f@ to the 'Text' when @value@ is a @String@
625
- -- and fails using @'typeMismatch' expected@ otherwise.
631
+ -- | @' withText' expected f value@ applies @f@ to the 'Text' when @value@ is a
632
+ -- 'String' and fails using @'typeMismatch' expected@ otherwise.
626
633
withText :: String -> (Text -> Parser a ) -> Value -> Parser a
627
634
withText _ f (String txt) = f txt
628
635
withText expected _ v = typeMismatch expected v
629
636
{-# INLINE withText #-}
630
637
631
- -- | @withArray expected f value@ applies @f@ to the 'Array' when @value@ is an @Array@
632
- -- and fails using @'typeMismatch' expected@ otherwise.
638
+ -- | @' withArray' expected f value@ applies @f@ to the 'Array' when @value@ is
639
+ -- an 'Array' and fails using @'typeMismatch' expected@ otherwise.
633
640
withArray :: String -> (Array -> Parser a ) -> Value -> Parser a
634
641
withArray _ f (Array arr) = f arr
635
642
withArray expected _ v = typeMismatch expected v
636
643
{-# INLINE withArray #-}
637
644
638
- -- | @withNumber expected f value@ applies @f@ to the 'Number' when @value@ is a 'Number'.
639
- -- and fails using @'typeMismatch' expected@ otherwise.
645
+ -- | @' withNumber' expected f value@ applies @f@ to the 'Number' when @value@
646
+ -- is a 'Number' and fails using @'typeMismatch' expected@ otherwise.
640
647
withNumber :: String -> (Number -> Parser a ) -> Value -> Parser a
641
648
withNumber expected f = withScientific expected (f . scientificToNumber)
642
649
{-# INLINE withNumber #-}
643
650
{-# DEPRECATED withNumber "Use withScientific instead" #-}
644
651
645
- -- | @withScientific expected f value@ applies @f@ to the 'Scientific' number when @value@ is a 'Number'.
646
- -- and fails using @'typeMismatch' expected@ otherwise.
652
+ -- | @'withScientific' expected f value@ applies @f@ to the 'Scientific' number
653
+ -- when @value@ is a 'Number' and fails using @'typeMismatch' expected@
654
+ -- otherwise.
647
655
withScientific :: String -> (Scientific -> Parser a ) -> Value -> Parser a
648
656
withScientific _ f (Number scientific) = f scientific
649
657
withScientific expected _ v = typeMismatch expected v
650
658
{-# INLINE withScientific #-}
651
659
652
- -- | @withBool expected f value@ applies @f@ to the 'Bool' when @value@ is a @Bool@
653
- -- and fails using @'typeMismatch' expected@ otherwise.
660
+ -- | @' withBool' expected f value@ applies @f@ to the 'Bool' when @value@ is a
661
+ -- 'Bool' and fails using @'typeMismatch' expected@ otherwise.
654
662
withBool :: String -> (Bool -> Parser a ) -> Value -> Parser a
655
663
withBool _ f (Bool arr) = f arr
656
664
withBool expected _ v = typeMismatch expected v
0 commit comments