@@ -20,12 +20,22 @@ module Network.JsonRpc.Types ( RpcResult
20
20
, rpcErrorWithData ) where
21
21
22
22
import Data.Maybe (catMaybes )
23
- import Data.Text (Text , append , unpack )
23
+ import Data.Text (Text )
24
+ #if ! MIN_VERSION_aeson(2,0,0)
25
+ import Data.Text (unpack )
26
+ #endif
24
27
import qualified Data.Aeson as A
28
+ #if MIN_VERSION_aeson(2,0,0)
29
+ import qualified Data.Aeson.Key as A
30
+ #endif
25
31
import Data.Aeson ((.=) , (.:) , (.:?) , (.!=) )
26
32
import Data.Aeson.Types (emptyObject )
27
33
import qualified Data.Vector as V
34
+ #if MIN_VERSION_aeson(2,0,0)
35
+ import qualified Data.Aeson.KeyMap as KeyMap
36
+ #else
28
37
import qualified Data.HashMap.Strict as H
38
+ #endif
29
39
import Control.DeepSeq (NFData , rnf )
30
40
import Control.Monad (when )
31
41
import Control.Monad.Except (ExceptT (.. ), throwError )
@@ -68,22 +78,22 @@ instance (A.FromJSON a, MethodParams f p m r) => MethodParams (a -> f) (a :+: p)
68
78
ExceptT (return arg) >>= \ a -> _apply (f a) ps nextArgs
69
79
where
70
80
arg = maybe (paramDefault param) (parseArg name) lookupValue
71
- lookupValue = either (H. lookup name) (V. !? 0 ) args
81
+ lookupValue = either (lookupInObject name) (V. !? 0 ) args
72
82
nextArgs = V. drop 1 <$> args
73
83
name = paramName param
74
84
75
85
parseArg :: A. FromJSON r => Text -> A. Value -> Either RpcError r
76
86
parseArg name val = case A. fromJSON val of
77
87
A. Error msg -> throwError $ argTypeError msg
78
88
A. Success x -> return x
79
- where argTypeError = rpcErrorWithData (- 32602 ) $ " Wrong type for argument: " `append` name
89
+ where argTypeError = rpcErrorWithData (- 32602 ) $ " Wrong type for argument: " <> name
80
90
81
91
paramDefault :: Parameter a -> Either RpcError a
82
92
paramDefault (Optional _ d) = Right d
83
93
paramDefault (Required name) = Left $ missingArgError name
84
94
85
95
missingArgError :: Text -> RpcError
86
- missingArgError name = rpcError (- 32602 ) $ " Cannot find required argument: " `append` name
96
+ missingArgError name = rpcError (- 32602 ) $ " Cannot find required argument: " <> name
87
97
88
98
paramName :: Parameter a -> Text
89
99
paramName (Optional n _) = n
@@ -106,7 +116,7 @@ instance A.FromJSON Request where
106
116
parseParams (A. Array ar) = return $ Right ar
107
117
parseParams _ = empty
108
118
checkVersion ver = when (ver /= jsonRpcVersion) $
109
- fail $ " Wrong JSON-RPC version: " ++ unpack ver
119
+ fail $ " Wrong JSON-RPC version: " ++ unpackKey ver
110
120
-- (.:?) parses Null value as Nothing so parseId needs
111
121
-- to use both (.:?) and (.:) to handle all cases
112
122
parseId = x .:? idKey >>= \ optional ->
@@ -180,7 +190,25 @@ rpcError code msg = RpcError code msg Nothing
180
190
rpcErrorWithData :: A. ToJSON a => Int -> Text -> a -> RpcError
181
191
rpcErrorWithData code msg errorData = RpcError code msg $ Just $ A. toJSON errorData
182
192
193
+ #if MIN_VERSION_aeson(2,0,0)
194
+ jsonRpcVersion , versionKey , idKey :: A. Key
195
+ #else
183
196
jsonRpcVersion , versionKey , idKey :: Text
197
+ #endif
184
198
jsonRpcVersion = " 2.0"
185
199
versionKey = " jsonrpc"
186
200
idKey = " id"
201
+
202
+ #if MIN_VERSION_aeson(2,0,0)
203
+ unpackKey :: A. Key -> String
204
+ unpackKey = A. toString
205
+
206
+ lookupInObject :: Text -> KeyMap. KeyMap A. Value -> Maybe A. Value
207
+ lookupInObject key = KeyMap. lookup (A. fromText key)
208
+ #else
209
+ unpackKey :: Text -> String
210
+ unpackKey = unpack
211
+
212
+ lookupInObject :: Text -> H. HashMap Text A. Value -> Maybe A. Value
213
+ lookupInObject = H. lookup
214
+ #endif
0 commit comments