@@ -0,0 +1,87 @@
1+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, TemplateHaskell #-}
5+--------------------------------------------------------------------------------
9+import Control.DeepSeq (NFData, rnf, deepseq)
11+import Data.Typeable (Typeable)
12+import Data.Data (Data)
13+import GHC.Generics (Generic)
15+import Data.Aeson.Types
16+import Data.Aeson.TH (mkToJSON, mkParseJSON)
17+import qualified Data.Aeson.Generic as G (fromJSON, toJSON)
19+--------------------------------------------------------------------------------
21+-- Taken from the documentation of Data.Aeson.TH:
24+ | Product String Char a
25+ | Record { testOne :: Double
28+ } deriving (Eq, Generic, Data, Typeable)
30+instance NFData a => NFData (D a) where
32+ rnf (Unary n) = rnf n
33+ rnf (Product s c x) = s `deepseq` c `deepseq` rnf x
34+ rnf (Record d b y) = d `deepseq` b `deepseq` rnf y
36+--------------------------------------------------------------------------------
42+ { testOne = 1234.56789
44+ , testThree = Product "Hello World!" 'a' $
46+ { testOne = 9876.54321
48+ , testThree = Product "Yeehaa!!!" '\n' Nullary
52+--------------------------------------------------------------------------------
54+instance ToJSON a => ToJSON (D a)
55+instance FromJSON a => FromJSON (D a)
57+--------------------------------------------------------------------------------
59+thToJSON :: ToJSON a => D a -> Value
60+thToJSON = $(mkToJSON id ''D)
62+thParseJSON :: FromJSON a => Value -> Parser (D a)
63+thParseJSON = $(mkParseJSON id ''D)
65+thFromJSON :: FromJSON a => Value -> Result (D a)
66+thFromJSON = parse thParseJSON
68+--------------------------------------------------------------------------------
70+type FJ = Value -> Result T
75+ in d `deepseq` v `deepseq` defaultMain
76+ [ group "toJSON" (nf thToJSON d)
79+ , group "fromJSON" (nf (thFromJSON :: FJ) v)
80+ (nf (G.fromJSON :: FJ) v)
81+ (nf (fromJSON :: FJ) v)
84+group n th syb gen = bgroup n [ bench "th" th
86+ , bench "generic" gen