@@ -44,6 +44,7 @@ module Data.Aeson.Types.Internal
4444 , parse
4545 , parseEither
4646 , parseMaybe
47+ , parseFail
4748 , modifyFailure
4849 , prependFailure
4950 , parserThrowError
@@ -342,6 +343,10 @@ instance Monoid (Parser a) where
342343 mappend = (<>)
343344 {-# INLINE mappend #-}
344345
346+-- | Raise a parsing failure with some custom message.
347+parseFail :: String -> Parser a
348+parseFail = fail
349+
345350apP :: Parser (a -> b) -> Parser a -> Parser b
346351apP d e = do
347352 b <- d
@@ -581,41 +586,53 @@ data Options = Options
581586 -- the constructor tag. If 'False' the encoding will always
582587 -- follow the `sumEncoding`.
583588 , omitNothingFields :: Bool
584− -- ^ If 'True' record fields with a 'Nothing' value will be
585− -- omitted from the resulting object. If 'False' the resulting
589+ -- ^ If 'True', record fields with a 'Nothing' value will be
590+ -- omitted from the resulting object. If 'False', the resulting
586591 -- object will include those fields mapping to @null@.
587592 --
593+ -- Note that this /does not/ affect parsing: 'Maybe' fields are
594+ -- optional regardless of the value of 'omitNothingFields', subject
595+ -- to the note below.
596+ --
588597 -- === Note
589598 --
590599 -- Setting 'omitNothingFields' to 'True' only affects fields which are of
591− -- type 'Maybe' /uniformly/ in the 'ToJSON' or 'FromJSON' instance. In
592− -- particular, if the type of a field is declared as a type variable, it
600+ -- type 'Maybe' /uniformly/ in the 'ToJSON' instance.
601+ -- In particular, if the type of a field is declared as a type variable, it
593602 -- will not be omitted from the JSON object, unless the field is
594603 -- specialized upfront in the instance.
595604 --
605+ -- The same holds for 'Maybe' fields being optional in the 'FromJSON' instance.
606+ --
596607 -- ==== __Example__
597608 --
598609 -- The generic instance for the following type @Fruit@ depends on whether
599610 -- the instance head is @Fruit a@ or @Fruit (Maybe a)@.
600611 --
601612 -- @
602− -- data Fruit a =
613+ -- data Fruit a = Fruit
603614 -- { apples :: a -- A field whose type is a type variable.
604615 -- , oranges :: 'Maybe' Int
605− -- }
606− --
607− -- options :: 'Options'
608− -- options = 'defaultOptions' { 'omitNothingFields' = 'True' }
616+ -- } deriving 'Generic'
609617 --
610618 -- -- apples required, oranges optional
611619 -- -- Even if 'Data.Aeson.fromJSON' is then specialized to (Fruit ('Maybe' a)).
612− -- instance 'Data.Aeson.FromJSON' a => 'Data.Aeson.FromJSON' (Fruit a) where
613− -- 'Data.Aeson.fromJSON' = 'Data.Aeson.genericFromJSON' options
620+ -- instance 'Data.Aeson.FromJSON' a => 'Data.Aeson.FromJSON' (Fruit a)
614621 --
615622 -- -- apples optional, oranges optional
616623 -- -- In this instance, the field apples is uniformly of type ('Maybe' a).
617− -- instance 'Data.Aeson.FromJSON' a => 'Data.Aeson.FromJSON' (Fruit ('Maybe' a)) where
618− -- 'Data.Aeson.fromJSON' = 'Data.Aeson.genericFromJSON' options
624+ -- instance 'Data.Aeson.FromJSON' a => 'Data.Aeson.FromJSON' (Fruit ('Maybe' a))
625+ --
626+ -- options :: 'Options'
627+ -- options = 'defaultOptions' { 'omitNothingFields' = 'True' }
628+ --
629+ -- -- apples always present in the output, oranges is omitted if 'Nothing'
630+ -- instance 'Data.Aeson.ToJSON' a => 'Data.Aeson.ToJSON' (Fruit a) where
631+ -- 'Data.Aeson.toJSON' = 'Data.Aeson.genericToJSON' options
632+ --
633+ -- -- both apples and oranges are omitted if 'Nothing'
634+ -- instance 'Data.Aeson.ToJSON' a => 'Data.Aeson.ToJSON' (Fruit ('Maybe' a)) where
635+ -- 'Data.Aeson.toJSON' = 'Data.Aeson.genericToJSON' options
619636 -- @
620637 , sumEncoding :: SumEncoding
621638 -- ^ Specifies how to encode constructors of a sum datatype.