on master help sign in
Added the genericToJSON and genericParseJSON functions

This makes it easier to parameterize the generic JSON encoding/decoding since users now don't need to import GHC.Generics and so don't need to depend on ghc-prim.

commit8cf877c1b39f9c08bf254308575a33682f15dccb authorBas van Dijk <v.dijk.bas@gmail.com> dateFri 19 Apr 2013, 14:02 (13 years ago) parenta19a5eb
3 files changed+26−8
Data/Aeson.hs+2 Data/Aeson/Types.hs+2 Data/Aeson/Types/Class.hs+22−8
Data/Aeson.hs+2
@@ -55,6 +55,8 @@ module Data.Aeson
5555 -- ** Generic JSON classes
5656 , GFromJSON(..)
5757 , GToJSON(..)
58+ , genericToJSON
59+ , genericParseJSON
5860#endif
5961 -- * Inspecting @'Value's@
6062 , withObject
Data/Aeson/Types.hs+2
@@ -38,6 +38,8 @@ module Data.Aeson.Types
3838 -- ** Generic JSON classes
3939 , GFromJSON(..)
4040 , GToJSON(..)
41+ , genericToJSON
42+ , genericParseJSON
4143#endif
4244
4345 -- * Inspecting @'Value's@
Data/Aeson/Types/Class.hs+22−8
@@ -28,6 +28,8 @@ module Data.Aeson.Types.Class
2828 -- ** Generic JSON classes
2929 , GFromJSON(..)
3030 , GToJSON(..)
31+ , genericToJSON
32+ , genericParseJSON
3133#endif
3234 -- * Types
3335 , DotNetTime(..)
@@ -99,6 +101,18 @@ class GFromJSON f where
99101 -- | This method (applied to 'defaultOptions') is used as the
100102 -- default generic implementation of 'parseJSON'.
101103 gParseJSON :: Options -> Value -> Parser (f a)
104+
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
110+
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
102116#endif
103117
104118-- | A type that can be converted to JSON.
@@ -146,19 +160,19 @@ class GFromJSON f where
146160-- @
147161--
148162-- Note that, instead of using @DefaultSignatures@, it's also possible
149−-- to parameterize the generic encoding by using 'gToJSON' applied to
150−-- the encoding 'Options':
163+-- to parameterize the generic encoding using 'genericToJSON' applied
164+-- to your encoding/decoding 'Options':
151165--
152166-- @
153167-- instance ToJSON Coord where
154−-- toJSON = 'gToJSON' 'defaultOptions' . from
168+-- toJSON = 'genericToJSON' 'defaultOptions'
155169-- @
156170class ToJSON a where
157171 toJSON :: a -> Value
158172
159173#ifdef GENERICS
160174 default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
161− toJSON = gToJSON defaultOptions . from
175+ toJSON = genericToJSON defaultOptions
162176#endif
163177
164178-- | A type that can be converted from JSON, with the possibility of
@@ -216,12 +230,12 @@ class ToJSON a where
216230-- @
217231--
218232-- Note that, instead of using @DefaultSignatures@, it's also possible
219−-- to parameterize the generic decoding by using 'gParseJSON' applied
220−-- to the encoding 'Options':
233+-- to parameterize the generic decoding using 'genericParseJSON' applied
234+-- to your encoding/decoding 'Options':
221235--
222236-- @
223237-- instance FromJSON Coord where
224−-- parseJSON = fmap to . 'gParseJSON' 'defaultOptions'
238+-- parseJSON = 'genericParseJSON' 'defaultOptions'
225239-- @
226240
227241class FromJSON a where
@@ -229,7 +243,7 @@ class FromJSON a where
229243
230244#ifdef GENERICS
231245 default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
232− parseJSON = fmap to . gParseJSON defaultOptions
246+ parseJSON = genericParseJSON defaultOptions
233247#endif
234248
235249instance (ToJSON a) => ToJSON (Maybe a) where