on master help sign in
Fix id_ breakage in benchmarks (#562)
commita2955fb119f9ea0805a6c6d644621ad7b137c6cd authorOleg Grenrus <oleg.grenrus@iki.fi> dateWed 12 Jul 2017, 16:22 (9 years ago) parenta1044dd
5 files changed+88−28
benchmarks/Typed/Generic.hs+1−1 benchmarks/aeson-benchmarks.cabal+2 examples/Twitter/Generic.hs+47−19 examples/Twitter/Options.hs+29 examples/Twitter/TH.hs+9−8
benchmarks/Typed/Generic.hs+1−1
@@ -7,7 +7,7 @@ import Prelude.Compat
77import "aeson" Data.Aeson hiding (Result)
88import Criterion
99import Data.ByteString.Lazy as L
10−import Twitter.TH
10+import Twitter.Generic
1111import Typed.Common
1212import qualified "aeson-benchmarks" Data.Aeson as B
1313
benchmarks/aeson-benchmarks.cabal+2
@@ -125,7 +125,9 @@ executable aeson-benchmark-typed
125125 cpp-options: -DHAS_BOTH_AESON_AND_BENCHMARKS
126126 other-modules:
127127 Twitter
128+ Twitter.Generic
128129 Twitter.Manual
130+ Twitter.Options
129131 Twitter.TH
130132 Typed.Common
131133 Typed.Generic
examples/Twitter/Generic.hs+47−19
@@ -1,5 +1,8 @@
11-- Use GHC generics to automatically generate good instances.
2+
23{-# LANGUAGE CPP #-}
4+{-# LANGUAGE PackageImports #-}
5+
36{-# OPTIONS_GHC -fno-warn-orphans #-}
47
58module Twitter.Generic
@@ -11,39 +14,64 @@ module Twitter.Generic
1114 ) where
1215
1316import Prelude ()
14−import Prelude.Compat
17+import Prelude.Compat ()
1518
1619import Twitter
20+import Twitter.Options
1721
1822#ifndef HAS_BOTH_AESON_AND_BENCHMARKS
19−import Data.Aeson (ToJSON, FromJSON)
23+import Data.Aeson (ToJSON (..), FromJSON (..), genericToJSON, genericToEncoding, genericParseJSON)
2024#else
21−import "aeson" Data.Aeson (ToJSON, FromJSON)
25+import "aeson" Data.Aeson (ToJSON (..), FromJSON (..), genericToJSON, genericToEncoding, genericParseJSON)
2226import qualified "aeson-benchmarks" Data.Aeson as B
2327#endif
2428
25−instance ToJSON Metadata
26−instance FromJSON Metadata
29+instance ToJSON Metadata where
30+ toJSON = genericToJSON twitterOptions
31+ toEncoding = genericToEncoding twitterOptions
32+instance FromJSON Metadata where
33+ parseJSON = genericParseJSON twitterOptions
2734
28−instance ToJSON Geo
29−instance FromJSON Geo
35+instance ToJSON Geo where
36+ toJSON = genericToJSON twitterOptions
37+ toEncoding = genericToEncoding twitterOptions
38+instance FromJSON Geo where
39+ parseJSON = genericParseJSON twitterOptions
3040
31−instance ToJSON Story
32−instance FromJSON Story
41+instance ToJSON Story where
42+ toJSON = genericToJSON twitterOptions
43+ toEncoding = genericToEncoding twitterOptions
44+instance FromJSON Story where
45+ parseJSON = genericParseJSON twitterOptions
3346
34−instance ToJSON Result
35−instance FromJSON Result
47+instance ToJSON Result where
48+ toJSON = genericToJSON twitterOptions
49+ toEncoding = genericToEncoding twitterOptions
50+instance FromJSON Result where
51+ parseJSON = genericParseJSON twitterOptions
3652
3753#ifdef HAS_BOTH_AESON_AND_BENCHMARKS
38−instance B.ToJSON Metadata
39−instance B.FromJSON Metadata
54+instance B.ToJSON Metadata where
55+ toJSON = B.genericToJSON btwitterOptions
56+ toEncoding = B.genericToEncoding btwitterOptions
57+instance B.FromJSON Metadata where
58+ parseJSON = B.genericParseJSON btwitterOptions
4059
41−instance B.ToJSON Geo
42−instance B.FromJSON Geo
60+instance B.ToJSON Geo where
61+ toJSON = B.genericToJSON btwitterOptions
62+ toEncoding = B.genericToEncoding btwitterOptions
63+instance B.FromJSON Geo where
64+ parseJSON = B.genericParseJSON btwitterOptions
4365
44−instance B.ToJSON Story
45−instance B.FromJSON Story
66+instance B.ToJSON Story where
67+ toJSON = B.genericToJSON btwitterOptions
68+ toEncoding = B.genericToEncoding btwitterOptions
69+instance B.FromJSON Story where
70+ parseJSON = B.genericParseJSON btwitterOptions
4671
47−instance B.ToJSON Result
48−instance B.FromJSON Result
72+instance B.ToJSON Result where
73+ toJSON = B.genericToJSON btwitterOptions
74+ toEncoding = B.genericToEncoding btwitterOptions
75+instance B.FromJSON Result where
76+ parseJSON = B.genericParseJSON btwitterOptions
4977#endif
examples/Twitter/Options.hsnew file+29
@@ -0,0 +1,29 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE PackageImports #-}
3+module Twitter.Options (module Twitter.Options) where
4+
5+#ifndef HAS_BOTH_AESON_AND_BENCHMARKS
6+import Data.Aeson
7+import Data.Aeson.Types
8+#else
9+import "aeson" Data.Aeson
10+import "aeson" Data.Aeson.Types
11+import qualified "aeson-benchmarks" Data.Aeson as B
12+import qualified "aeson-benchmarks" Data.Aeson.Types as B
13+#endif
14+
15+twitterOptions :: Options
16+twitterOptions = defaultOptions
17+ { fieldLabelModifier = \x -> case x of
18+ "id_" -> "id"
19+ _ -> x
20+ }
21+
22+#ifdef HAS_BOTH_AESON_AND_BENCHMARKS
23+btwitterOptions :: B.Options
24+btwitterOptions = B.defaultOptions
25+ { B.fieldLabelModifier = \x -> case x of
26+ "id_" -> "id"
27+ _ -> x
28+ }
29+#endif
examples/Twitter/TH.hs+9−8
@@ -16,6 +16,7 @@ module Twitter.TH
1616import Prelude ()
1717
1818import Twitter
19+import Twitter.Options
1920
2021#ifndef HAS_BOTH_AESON_AND_BENCHMARKS
2122import Data.Aeson.TH
@@ -24,14 +25,14 @@ import "aeson" Data.Aeson.TH
2425import qualified "aeson-benchmarks" Data.Aeson.TH as B
2526#endif
2627
27−$(deriveJSON defaultOptions ''Metadata)
28−$(deriveJSON defaultOptions ''Geo)
29−$(deriveJSON defaultOptions ''Story)
30−$(deriveJSON defaultOptions ''Result)
28+$(deriveJSON twitterOptions ''Metadata)
29+$(deriveJSON twitterOptions ''Geo)
30+$(deriveJSON twitterOptions ''Story)
31+$(deriveJSON twitterOptions ''Result)
3132
3233#ifdef HAS_BOTH_AESON_AND_BENCHMARKS
33−$(B.deriveJSON B.defaultOptions ''Metadata)
34−$(B.deriveJSON B.defaultOptions ''Geo)
35−$(B.deriveJSON B.defaultOptions ''Story)
36−$(B.deriveJSON B.defaultOptions ''Result)
34+$(B.deriveJSON btwitterOptions ''Metadata)
35+$(B.deriveJSON btwitterOptions ''Geo)
36+$(B.deriveJSON btwitterOptions ''Story)
37+$(B.deriveJSON btwitterOptions ''Result)
3738#endif