@@ -28,6 +28,8 @@ module Data.Aeson.Types.Class
28 -- ** Generic JSON classes
28 -- ** Generic JSON classes
29 , GFromJSON(..)
29 , GFromJSON(..)
30 , GToJSON(..)
30 , GToJSON(..)
32 -- * Types
34 -- * Types
33 , DotNetTime(..)
35 , DotNetTime(..)
@@ -99,6 +101,18 @@ class GFromJSON f where
99 -- | This method (applied to 'defaultOptions') is used as the
101 -- | This method (applied to 'defaultOptions') is used as the
100 -- default generic implementation of 'parseJSON'.
102 -- default generic implementation of 'parseJSON'.
101 gParseJSON :: Options -> Value -> Parser (f a)
103 gParseJSON :: Options -> Value -> Parser (f a)
105+-- | A configurable generic JSON encoder. This function applied to
106+-- 'defaultOptions' is used as the default for 'toJSON' when the type
107+-- is an instance of 'Generic'.
108+genericToJSON :: (Generic a, GToJSON (Rep a)) => Options -> a -> Value
109+genericToJSON opts = gToJSON opts . from
111+-- | A configurable generic JSON decoder. This function applied to
112+-- 'defaultOptions' is used as the default for 'parseJSON' when the
113+-- type is an instance of 'Generic'.
114+genericParseJSON :: (Generic a, GFromJSON (Rep a)) => Options -> Value -> Parser a
115+genericParseJSON opts = fmap to . gParseJSON opts
104-- | A type that can be converted to JSON.
118-- | A type that can be converted to JSON.
@@ -146,19 +160,19 @@ class GFromJSON f where
148-- Note that, instead of using @DefaultSignatures@, it's also possible
162-- Note that, instead of using @DefaultSignatures@, it's also possible
149−-- to parameterize the generic encoding by using 'gToJSON' applied to
163+-- to parameterize the generic encoding using 'genericToJSON' applied
150−-- the encoding 'Options':
164+-- to your encoding/decoding 'Options':
153-- instance ToJSON Coord where
167-- instance ToJSON Coord where
154−-- toJSON = 'gToJSON' 'defaultOptions' . from
168+-- toJSON = 'genericToJSON' 'defaultOptions'
156class ToJSON a where
170class ToJSON a where
157 toJSON :: a -> Value
171 toJSON :: a -> Value
159#ifdef GENERICS
173#ifdef GENERICS
160 default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
174 default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
161− toJSON = gToJSON defaultOptions . from
175+ toJSON = genericToJSON defaultOptions
164-- | A type that can be converted from JSON, with the possibility of
178-- | A type that can be converted from JSON, with the possibility of
@@ -216,12 +230,12 @@ class ToJSON a where
218-- Note that, instead of using @DefaultSignatures@, it's also possible
232-- Note that, instead of using @DefaultSignatures@, it's also possible
219−-- to parameterize the generic decoding by using 'gParseJSON' applied
233+-- to parameterize the generic decoding using 'genericParseJSON' applied
220−-- to the encoding 'Options':
234+-- to your encoding/decoding 'Options':
223-- instance FromJSON Coord where
237-- instance FromJSON Coord where
224−-- parseJSON = fmap to . 'gParseJSON' 'defaultOptions'
238+-- parseJSON = 'genericParseJSON' 'defaultOptions'
227class FromJSON a where
241class FromJSON a where
@@ -229,7 +243,7 @@ class FromJSON a where
230#ifdef GENERICS
244#ifdef GENERICS
231 default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
245 default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
232− parseJSON = fmap to . gParseJSON defaultOptions
246+ parseJSON = genericParseJSON defaultOptions
235instance (ToJSON a) => ToJSON (Maybe a) where
249instance (ToJSON a) => ToJSON (Maybe a) where