Skip to content

Commit c0c4959

Browse files
committed
Inlined key literals
1 parent ce7ecf1 commit c0c4959

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

src/Network/JsonRpc/Types.hs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ data Request = Request Text Args (Maybe Id)
108108
instance FromJSON Request where
109109
parseJSON (Object x) = (checkVersion =<< x .:? versionKey .!= jsonRpcVersion) *>
110110
(Request <$>
111-
x .: methodKey <*>
112-
(parseParams =<< x .:? paramsKey .!= emptyObject) <*>
111+
x .: "method" <*>
112+
(parseParams =<< x .:? "params" .!= emptyObject) <*>
113113
(Just <$> x .: idKey <|> return Nothing)) -- (.:?) parses Null value as Nothing
114114
where parseParams (Object obj) = return $ Left obj
115115
parseParams (Array ar) = return $ Right ar
@@ -122,7 +122,7 @@ data Response = Response Id (Either RpcError Value)
122122
instance ToJSON Response where
123123
toJSON (Response i result) = object pairs
124124
where pairs = [ versionKey .= jsonRpcVersion
125-
, either (errorKey .=) (resultKey .=) result
125+
, either ("error" .=) ("result" .=) result
126126
, idKey .= i]
127127

128128
data Id = IdString Text | IdNumber Number | IdNull
@@ -149,9 +149,9 @@ instance Error RpcError where
149149

150150
instance ToJSON RpcError where
151151
toJSON (RpcError code msg data') = object pairs
152-
where pairs = catMaybes [ Just $ codeKey .= code
153-
, Just $ msgKey .= msg
154-
, (dataKey .=) <$> data' ]
152+
where pairs = catMaybes [ Just $ "code" .= code
153+
, Just $ "message" .= msg
154+
, ("data" .=) <$> data' ]
155155

156156
-- | Creates an 'RpcError' with the given error code and message.
157157
-- According to the specification, server error codes should be
@@ -169,16 +169,3 @@ jsonRpcVersion, versionKey, idKey :: Text
169169
jsonRpcVersion = "2.0"
170170
versionKey = "jsonrpc"
171171
idKey = "id"
172-
173-
methodKey, paramsKey :: Text
174-
methodKey = "method"
175-
paramsKey = "params"
176-
177-
resultKey, errorKey :: Text
178-
resultKey = "result"
179-
errorKey = "error"
180-
181-
codeKey, msgKey, dataKey :: Text
182-
codeKey = "code"
183-
msgKey = "message"
184-
dataKey = "data"

tests/TestTypes.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ module TestTypes ( TestRequest (..)
44
, TestResponse (..)
55
, TestRpcError (..)
66
, TestId (..)
7-
, jsonRpcVersion
87
, versionKey) where
98

109
import Data.Aeson
1110
import Data.Maybe (catMaybes)
12-
import Data.Text (Text)
11+
import Data.Text (Text, pack)
1312
import Data.Attoparsec.Number (Number)
1413
import Data.HashMap.Strict (size)
1514
import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
@@ -43,7 +42,7 @@ data TestResponse = TestResponse { rspId :: TestId
4342
instance FromJSON TestResponse where
4443
parseJSON (Object obj) = do
4544
guard (size obj == 3)
46-
guard . (jsonRpcVersion ==) =<< obj .: versionKey
45+
guard . (pack "2.0" ==) =<< obj .: versionKey
4746
TestResponse <$> obj .: "id" <*>
4847
((Left <$> obj .: "error") <|> (Right <$> obj .: "result"))
4948
parseJSON _ = empty
@@ -65,6 +64,3 @@ instance ToJSON TestId where
6564

6665
versionKey :: Text
6766
versionKey = "jsonrpc"
68-
69-
jsonRpcVersion :: Text
70-
jsonRpcVersion = "2.0"

0 commit comments

Comments
 (0)