Commits

basvandijk committed 96b9638

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.

  • Participants
  • Parent commits fcf4ead

Comments (0)

Files changed (3)

File Data/Aeson.hs

     -- ** Generic JSON classes
     , GFromJSON(..)
     , GToJSON(..)
+    , genericToJSON
+    , genericParseJSON
 #endif
     -- * Inspecting @'Value's@
     , withObject

File Data/Aeson/Types.hs

     -- ** Generic JSON classes
     , GFromJSON(..)
     , GToJSON(..)
+    , genericToJSON
+    , genericParseJSON
 #endif
 
     -- * Inspecting @'Value's@

File Data/Aeson/Types/Class.hs

     -- ** Generic JSON classes
     , GFromJSON(..)
     , GToJSON(..)
+    , genericToJSON
+    , genericParseJSON
 #endif
     -- * Types
     , DotNetTime(..)
     -- | This method (applied to 'defaultOptions') is used as the
     -- default generic implementation of 'parseJSON'.
     gParseJSON :: Options -> Value -> Parser (f a)
+
+-- | A configurable generic JSON encoder. This function applied to
+-- 'defaultOptions' is used as the default for 'toJSON' when the type
+-- is an instance of 'Generic'.
+genericToJSON :: (Generic a, GToJSON (Rep a)) => Options -> a -> Value
+genericToJSON opts = gToJSON opts . from
+
+-- | A configurable generic JSON decoder. This function applied to
+-- 'defaultOptions' is used as the default for 'parseJSON' when the
+-- type is an instance of 'Generic'.
+genericParseJSON :: (Generic a, GFromJSON (Rep a)) => Options -> Value -> Parser a
+genericParseJSON opts = fmap to . gParseJSON opts
 #endif
 
 -- | A type that can be converted to JSON.
 -- @
 --
 -- Note that, instead of using @DefaultSignatures@, it's also possible
--- to parameterize the generic encoding by using 'gToJSON' applied to
--- the encoding 'Options':
+-- to parameterize the generic encoding using 'genericToJSON' applied
+-- to your encoding/decoding 'Options':
 --
 -- @
 -- instance ToJSON Coord where
---     toJSON = 'gToJSON' 'defaultOptions' . from
+--     toJSON = 'genericToJSON' 'defaultOptions'
 -- @
 class ToJSON a where
     toJSON   :: a -> Value
 
 #ifdef GENERICS
     default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
-    toJSON = gToJSON defaultOptions . from
+    toJSON = genericToJSON defaultOptions
 #endif
 
 -- | A type that can be converted from JSON, with the possibility of
 -- @
 --
 -- Note that, instead of using @DefaultSignatures@, it's also possible
--- to parameterize the generic decoding by using 'gParseJSON' applied
--- to the encoding 'Options':
+-- to parameterize the generic decoding using 'genericParseJSON' applied
+-- to your encoding/decoding 'Options':
 --
 -- @
 -- instance FromJSON Coord where
---     parseJSON = fmap to . 'gParseJSON' 'defaultOptions'
+--     parseJSON = 'genericParseJSON' 'defaultOptions'
 -- @
 
 class FromJSON a where
 
 #ifdef GENERICS
     default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
-    parseJSON = fmap to . gParseJSON defaultOptions
+    parseJSON = genericParseJSON defaultOptions
 #endif
 
 instance (ToJSON a) => ToJSON (Maybe a) where