git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #25 from GetShopTV/no-aeson-th

Replace aeson TH with Generics-based instances

commita6ac15fbd10b9fcacc29aa0719b376d90b8aa481 authorNickolay Kudasov <fizruk@users.noreply.github.com> dateSat 19 Dec 2015, 11:47 (11 years ago) parente1b45b1ce85903
3 files changed+63−28
src/Data/Swagger/Internal.hs+61−14 src/Data/Swagger/Internal/Utils.hs+1−12 swagger2.cabal+1−2
src/Data/Swagger/Internal.hs+61−14
@@ -9,14 +9,12 @@
99{-# LANGUAGE RecordWildCards #-}
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE StandaloneDeriving #-}
12−{-# LANGUAGE TemplateHaskell #-}
1312{-# LANGUAGE UndecidableInstances #-}
1413module Data.Swagger.Internal where
1514
1615import Control.Applicative
1716import Control.Monad
1817import Data.Aeson
19−import Data.Aeson.TH (deriveJSON)
2018import qualified Data.Aeson.Types as JSON
2119import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
2220import Data.HashMap.Strict (HashMap)
@@ -825,20 +823,69 @@ instance SwaggerMonoid ParamAnySchema where
825823 swaggerMappend _ y = y
826824
827825-- =======================================================================
828−-- TH derived ToJSON and FromJSON instances
826+-- Simple Generic-based ToJSON instances
829827-- =======================================================================
830828
831−deriveJSON (jsonPrefix "Param") ''ParamLocation
832−deriveJSON' ''Info
833−deriveJSON' ''Contact
834−deriveJSON' ''License
835−deriveJSON (jsonPrefix "ApiKey") ''ApiKeyLocation
836−deriveJSON (jsonPrefix "apiKey") ''ApiKeyParams
837−deriveJSONDefault ''Scheme
838−deriveJSON' ''Tag
839−deriveJSON' ''ExternalDocs
840−
841−deriveToJSON' ''Xml
829+instance ToJSON ParamLocation where
830+ toJSON = genericToJSON (jsonPrefix "Param")
831+
832+instance ToJSON Info where
833+ toJSON = genericToJSON (jsonPrefix "Info")
834+
835+instance ToJSON Contact where
836+ toJSON = genericToJSON (jsonPrefix "Contact")
837+
838+instance ToJSON License where
839+ toJSON = genericToJSON (jsonPrefix "License")
840+
841+instance ToJSON ApiKeyLocation where
842+ toJSON = genericToJSON (jsonPrefix "ApiKey")
843+
844+instance ToJSON ApiKeyParams where
845+ toJSON = genericToJSON (jsonPrefix "apiKey")
846+
847+instance ToJSON Scheme where
848+ toJSON = genericToJSON (jsonPrefix "")
849+
850+instance ToJSON Tag where
851+ toJSON = genericToJSON (jsonPrefix "Tag")
852+
853+instance ToJSON ExternalDocs where
854+ toJSON = genericToJSON (jsonPrefix "ExternalDocs")
855+
856+instance ToJSON Xml where
857+ toJSON = genericToJSON (jsonPrefix "Xml")
858+
859+-- =======================================================================
860+-- Simple Generic-based FromJSON instances
861+-- =======================================================================
862+
863+instance FromJSON ParamLocation where
864+ parseJSON = genericParseJSON (jsonPrefix "Param")
865+
866+instance FromJSON Info where
867+ parseJSON = genericParseJSON (jsonPrefix "Info")
868+
869+instance FromJSON Contact where
870+ parseJSON = genericParseJSON (jsonPrefix "Contact")
871+
872+instance FromJSON License where
873+ parseJSON = genericParseJSON (jsonPrefix "License")
874+
875+instance FromJSON ApiKeyLocation where
876+ parseJSON = genericParseJSON (jsonPrefix "ApiKey")
877+
878+instance FromJSON ApiKeyParams where
879+ parseJSON = genericParseJSON (jsonPrefix "apiKey")
880+
881+instance FromJSON Scheme where
882+ parseJSON = genericParseJSON (jsonPrefix "")
883+
884+instance FromJSON Tag where
885+ parseJSON = genericParseJSON (jsonPrefix "Tag")
886+
887+instance FromJSON ExternalDocs where
888+ parseJSON = genericParseJSON (jsonPrefix "ExternalDocs")
842889
843890-- =======================================================================
844891-- Manual ToJSON instances
src/Data/Swagger/Internal/Utils.hs+1−12
@@ -8,8 +8,7 @@ module Data.Swagger.Internal.Utils where
88import Control.Arrow (first)
99import Control.Applicative
1010import Data.Aeson
11−import Data.Aeson.TH
12−import Data.Aeson.Types (Parser, Pair)
11+import Data.Aeson.Types
1312import Data.Char
1413import Data.Data
1514import Data.Hashable (Hashable)
@@ -18,7 +17,6 @@ import qualified Data.HashMap.Strict as HashMap
1817import Data.Monoid
1918import Data.Text (Text)
2019import GHC.Generics
21−import Language.Haskell.TH
2220import Text.Read (readMaybe)
2321
2422gunfoldEnum :: String -> [a] -> (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c a
@@ -50,15 +48,6 @@ jsonPrefix prefix = defaultOptions
5048 lowerFirstUppers s = map toLower x ++ y
5149 where (x, y) = span isUpper s
5250
53−deriveToJSON' :: Name -> Q [Dec]
54−deriveToJSON' name = deriveToJSON (jsonPrefix (nameBase name)) name
55−
56−deriveJSONDefault :: Name -> Q [Dec]
57−deriveJSONDefault = deriveJSON (jsonPrefix "")
58−
59−deriveJSON' :: Name -> Q [Dec]
60−deriveJSON' name = deriveJSON (jsonPrefix (nameBase name)) name
61−
6251parseOneOf :: ToJSON a => [a] -> Value -> Parser a
6352parseOneOf xs js =
6453 case lookup js ys of
swagger2.cabal+1−2
@@ -32,13 +32,12 @@ library
3232 Data.Swagger.Internal.ParamSchema
3333 Data.Swagger.Internal.Utils
3434 build-depends: base == 4.*
35− , aeson < 0.10
35+ , aeson
3636 , containers
3737 , hashable
3838 , http-media
3939 , mtl
4040 , network
41− , template-haskell
4241 , text
4342 , time
4443 , unordered-containers