@@ -22,7 +22,8 @@ module Network.JsonRpc.Types ( RpcResult
22
22
import Data.String (fromString )
23
23
import Data.Maybe (catMaybes )
24
24
import Data.Text (Text , append , unpack )
25
- import Data.Aeson
25
+ import qualified Data.Aeson as A
26
+ import Data.Aeson ((.=) , (.:) , (.:?) , (.!=) )
26
27
import Data.Aeson.Types (emptyObject )
27
28
import qualified Data.Vector as V
28
29
import qualified Data.HashMap.Strict as H
@@ -51,38 +52,38 @@ infixr :+:
51
52
-- monad ('m'), and return type ('r'). 'p' has one 'Parameter' for
52
53
-- every argument of 'f' and is terminated by @()@. The return type
53
54
-- of 'f' is @RpcResult m r@. This class is treated as closed.
54
- class (Monad m , Functor m , ToJSON r ) => MethodParams f p m r | f -> p m r where
55
+ class (Monad m , Functor m , A. ToJSON r ) => MethodParams f p m r | f -> p m r where
55
56
apply :: f -> p -> Args -> RpcResult m r
56
57
57
- instance (Monad m , Functor m , ToJSON r ) => MethodParams (RpcResult m r ) () m r where
58
+ instance (Monad m , Functor m , A. ToJSON r ) => MethodParams (RpcResult m r ) () m r where
58
59
apply r _ args | Left _ <- args = r
59
60
| Right ar <- args, V. null ar = r
60
61
| otherwise = throwError $ rpcError (- 32602 ) " Too many unnamed arguments"
61
62
62
- instance (FromJSON a , MethodParams f p m r ) => MethodParams (a -> f ) (a :+: p ) m r where
63
+ instance (A. FromJSON a , MethodParams f p m r ) => MethodParams (a -> f ) (a :+: p ) m r where
63
64
apply f (param :+: ps) args = arg >>= \ a -> apply (f a) ps nextArgs
64
65
where arg = either (parseArg name) return =<<
65
66
(Left <$> lookupValue <|> Right <$> paramDefault param)
66
67
lookupValue = either (lookupArg name) (headArg name) args
67
68
nextArgs = tailOrEmpty <$> args
68
69
name = paramName param
69
70
70
- lookupArg :: Monad m => Text -> Object -> RpcResult m Value
71
+ lookupArg :: Monad m => Text -> A. Object -> RpcResult m A. Value
71
72
lookupArg name hm = case H. lookup name hm of
72
73
Nothing -> throwError $ missingArgError name
73
74
Just v -> return v
74
75
75
- headArg :: Monad m => Text -> V. Vector a -> RpcResult m a
76
+ headArg :: Monad m => Text -> A. Array -> RpcResult m A. Value
76
77
headArg name vec | V. null vec = throwError $ missingArgError name
77
78
| otherwise = return $ V. head vec
78
79
79
- tailOrEmpty :: V. Vector a -> V. Vector a
80
+ tailOrEmpty :: A. Array -> A. Array
80
81
tailOrEmpty vec = if V. null vec then V. empty else V. tail vec
81
82
82
- parseArg :: (Monad m , FromJSON r ) => Text -> Value -> RpcResult m r
83
- parseArg name val = case fromJSON val of
84
- Error msg -> throwError $ rpcErrorWithData (- 32602 ) (" Wrong type for argument: " `append` name) msg
85
- Success x -> return x
83
+ parseArg :: (Monad m , A. FromJSON r ) => Text -> A. Value -> RpcResult m r
84
+ parseArg name val = case A. fromJSON val of
85
+ A. Error msg -> throwError $ rpcErrorWithData (- 32602 ) (" Wrong type for argument: " `append` name) msg
86
+ A. Success x -> return x
86
87
87
88
paramDefault :: Monad m => Parameter a -> RpcResult m a
88
89
paramDefault (Optional _ d) = return d
@@ -96,58 +97,58 @@ paramName (Optional n _) = n
96
97
paramName (Required n) = n
97
98
98
99
-- | Single method.
99
- data Method m = Method Text (Args -> RpcResult m Value )
100
+ data Method m = Method Text (Args -> RpcResult m A. Value )
100
101
101
102
-- | Multiple methods.
102
103
newtype Methods m = Methods (H. HashMap Text (Method m ))
103
104
104
- type Args = Either Object Array
105
+ type Args = Either A. Object A. Array
105
106
106
107
data Request = Request Text Args (Maybe Id )
107
108
108
- instance FromJSON Request where
109
- parseJSON (Object x) = (checkVersion =<< x .:? versionKey .!= jsonRpcVersion) *>
109
+ instance A. FromJSON Request where
110
+ parseJSON (A. Object x) = (checkVersion =<< x .:? versionKey .!= jsonRpcVersion) *>
110
111
(Request <$>
111
112
x .: " method" <*>
112
113
(parseParams =<< x .:? " params" .!= emptyObject) <*>
113
114
(Just <$> x .: idKey <|> return Nothing )) -- (.:?) parses Null value as Nothing
114
- where parseParams (Object obj) = return $ Left obj
115
- parseParams (Array ar) = return $ Right ar
115
+ where parseParams (A. Object obj) = return $ Left obj
116
+ parseParams (A. Array ar) = return $ Right ar
116
117
parseParams _ = empty
117
118
checkVersion ver = when (ver /= jsonRpcVersion) (fail $ " Wrong JSON RPC version: " ++ unpack ver)
118
119
parseJSON _ = empty
119
120
120
- data Response = Response Id (Either RpcError Value )
121
+ data Response = Response Id (Either RpcError A. Value )
121
122
122
- instance ToJSON Response where
123
- toJSON (Response i result) = object pairs
123
+ instance A. ToJSON Response where
124
+ toJSON (Response i result) = A. object pairs
124
125
where pairs = [ versionKey .= jsonRpcVersion
125
126
, either (" error" .= ) (" result" .= ) result
126
127
, idKey .= i]
127
128
128
129
data Id = IdString Text | IdNumber Number | IdNull
129
130
130
- instance FromJSON Id where
131
- parseJSON (String x) = return $ IdString x
132
- parseJSON (Number x) = return $ IdNumber x
133
- parseJSON Null = return IdNull
131
+ instance A. FromJSON Id where
132
+ parseJSON (A. String x) = return $ IdString x
133
+ parseJSON (A. Number x) = return $ IdNumber x
134
+ parseJSON A. Null = return IdNull
134
135
parseJSON _ = empty
135
136
136
- instance ToJSON Id where
137
- toJSON (IdString x) = String x
138
- toJSON (IdNumber x) = Number x
139
- toJSON IdNull = Null
137
+ instance A. ToJSON Id where
138
+ toJSON (IdString x) = A. String x
139
+ toJSON (IdNumber x) = A. Number x
140
+ toJSON IdNull = A. Null
140
141
141
142
-- | Error to be returned to the client.
142
- data RpcError = RpcError Int Text (Maybe Value )
143
+ data RpcError = RpcError Int Text (Maybe A. Value )
143
144
deriving Show
144
145
145
146
instance Error RpcError where
146
147
noMsg = strMsg " unknown error"
147
148
strMsg msg = RpcError (- 32000 ) (fromString msg) Nothing
148
149
149
- instance ToJSON RpcError where
150
- toJSON (RpcError code msg data') = object pairs
150
+ instance A. ToJSON RpcError where
151
+ toJSON (RpcError code msg data') = A. object pairs
151
152
where pairs = catMaybes [ Just $ " code" .= code
152
153
, Just $ " message" .= msg
153
154
, (" data" .= ) <$> data' ]
@@ -161,8 +162,8 @@ rpcError code msg = RpcError code msg Nothing
161
162
162
163
-- | Creates an 'RpcError' with the given code, message, and additional data.
163
164
-- See 'rpcError' for the recommended error code ranges.
164
- rpcErrorWithData :: ToJSON a => Int -> Text -> a -> RpcError
165
- rpcErrorWithData code msg errorData = RpcError code msg $ Just $ toJSON errorData
165
+ rpcErrorWithData :: A. ToJSON a => Int -> Text -> a -> RpcError
166
+ rpcErrorWithData code msg errorData = RpcError code msg $ Just $ A. toJSON errorData
166
167
167
168
jsonRpcVersion , versionKey , idKey :: Text
168
169
jsonRpcVersion = " 2.0"
0 commit comments