From 5814aa33eb4b29fbfb95333d8dcbaf991e6d0d10 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Tue, 25 Mar 2014 18:20:48 +0400 Subject: [PATCH] JsonRpcClient mostly works --- Data/{JsonRpc.hs => JsonRpcClient.hs} | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) rename Data/{JsonRpc.hs => JsonRpcClient.hs} (88%) diff --git a/Data/JsonRpc.hs b/Data/JsonRpcClient.hs similarity index 88% rename from Data/JsonRpc.hs rename to Data/JsonRpcClient.hs index 15b1d88..cf23d54 100644 --- a/Data/JsonRpc.hs +++ b/Data/JsonRpcClient.hs @@ -1,6 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} -module JsonRpc ( JsonRpcVersion(JsonRpcV1, JsonRpcV2) +module JsonRpcClient + ( JsonRpcVersion(JsonRpcV1, JsonRpcV2) , JsonRpcRequest , JsonRpcNotification , JsonRpcError(..) @@ -18,7 +19,7 @@ data JsonRpcVersion = JsonRpcV1 | JsonRpcV2 data JsonRpcRequest = JsonRpcRequest { jrpcVersion :: JsonRpcVersion , jrpcReqMethod :: ByteString , jrpcReqParams :: [ByteString] - , jrpcReqId :: ByteString + , jrpcReqId :: Value } deriving (Show) instance ToJSON JsonRpcRequest where toJSON (JsonRpcRequest version method params id) = @@ -51,11 +52,11 @@ instance FromJSON JsonRpcError where parseJSON x = return $ JsonRpcError (-32600) "Unparseable error object" - Nothing + (Just (toJSON x)) data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value , jrpcRspError :: JsonRpcError - , jrpcRspId :: ByteString + , jrpcRspId :: Value } deriving (Show) instance FromJSON JsonRpcResponse where parseJSON (Object o) = JsonRpcResponse @@ -67,16 +68,16 @@ instance FromJSON JsonRpcResponse where (JsonRpcError (-32700) "Unparseable response object" - Nothing + (Just (toJSON x)) ) - "" + (String "n/a") parseJsonRpc :: (FromJSON a) => ByteString -> Either JsonRpcError a parseJsonRpc s = case (decode s :: Maybe JsonRpcResponse) of Just (JsonRpcResponse result error id) -> case result of - Just v -> case (fromJSON v) of + Just v -> case fromJSON v of Success a -> Right a - Error s -> Left $ JsonRpcError (-32900) "Unparseable result" Nothing + Error s -> Left $ JsonRpcError (-32900) "Unparseable result" (Just v) Nothing -> Left error - Nothing -> Left $ JsonRpcError (-32800) "Unparseable response" Nothing + Nothing -> Left $ JsonRpcError (-32800) "Unparseable response" (Just (toJSON s)) -- 2.39.2