X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=Data%2FJsonRpc.hs;h=15b1d88e99d9e447ccb650b061707458350bfc0b;hp=79c21c349dcde14e791275a53bd5649d196ea07c;hb=a0ac4a88c7f48dac8e4a79d1b7ac17a19057d662;hpb=017b4bfc02d626ff3c22cf0873afb76153aa055c diff --git a/Data/JsonRpc.hs b/Data/JsonRpc.hs index 79c21c3..15b1d88 100644 --- a/Data/JsonRpc.hs +++ b/Data/JsonRpc.hs @@ -1,13 +1,15 @@ {-# LANGUAGE OverloadedStrings #-} -module JsonRpc ( JsonRpcVersion(JsonRpcV1 ,JsonRpcV2) +module JsonRpc ( JsonRpcVersion(JsonRpcV1, JsonRpcV2) , JsonRpcRequest , JsonRpcNotification - , JsonRpcResponse + , JsonRpcError(..) + , parseJsonRpc ) where import Data.ByteString.Lazy (ByteString) import Control.Applicative ((<$>), (<*>), empty) +import Data.Either import Data.Aeson data JsonRpcVersion = JsonRpcV1 | JsonRpcV2 @@ -20,21 +22,61 @@ data JsonRpcRequest = JsonRpcRequest { jrpcVersion :: JsonRpcVersion } deriving (Show) instance ToJSON JsonRpcRequest where toJSON (JsonRpcRequest version method params id) = - let l = [ "method" .= method , "params" .= params , "id" .= id ] + let l = [ "method" .= method, "params" .= params, "id" .= id ] + in case version of + JsonRpcV1 -> object l + JsonRpcV2 -> object $ ("jsonrpc" .= toJSON ("2.0" :: ByteString)):l + +data JsonRpcNotification = JsonRpcNotification + { jrpcNtfVersion :: JsonRpcVersion + , jrpcNtfMethod :: ByteString + , jrpcNtfParams :: [ByteString] + } deriving (Show) +instance ToJSON JsonRpcNotification where + toJSON (JsonRpcNotification version method params) = + let l = [ "method" .= method, "params" .= params ] in case version of JsonRpcV1 -> object l JsonRpcV2 -> object $ ("jsonrpc" .= toJSON ("2.0" :: ByteString)):l - -data JsonRpcNotification = JsonRpcNotification { jrpcNtfMethod :: ByteString - , jrpcNtfParams :: [ByteString] - } deriving (Show) data JsonRpcError = JsonRpcError { jrpcErrCode :: Int , jrpcErrMessage :: ByteString , jrpcErrData :: Maybe Value } deriving (Show) +instance FromJSON JsonRpcError where + parseJSON (Object o) = JsonRpcError + <$> o .: "code" + <*> o .: "error" + <*> o .: "data" + parseJSON x = return $ JsonRpcError + (-32600) + "Unparseable error object" + Nothing data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value , jrpcRspError :: JsonRpcError , jrpcRspId :: ByteString } deriving (Show) +instance FromJSON JsonRpcResponse where + parseJSON (Object o) = JsonRpcResponse + <$> o .: "result" + <*> o .: "error" + <*> o .: "id" + parseJSON x = return $ JsonRpcResponse + Nothing + (JsonRpcError + (-32700) + "Unparseable response object" + Nothing + ) + "" + +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 + Success a -> Right a + Error s -> Left $ JsonRpcError (-32900) "Unparseable result" Nothing + Nothing -> Left error + Nothing -> Left $ JsonRpcError (-32800) "Unparseable response" Nothing