From: Eugene Crosser Date: Mon, 24 Mar 2014 22:01:12 +0000 (+0400) Subject: wip JsonRpc version X-Git-Tag: 0.9.0.0~127 X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=commitdiff_plain;h=017b4bfc02d626ff3c22cf0873afb76153aa055c wip JsonRpc version --- diff --git a/Data/JsonRpc.hs b/Data/JsonRpc.hs index 64d184c..79c21c3 100644 --- a/Data/JsonRpc.hs +++ b/Data/JsonRpc.hs @@ -1,7 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} -module JsonRpc ( JsonRpcRequestV1 - , JsonRpcRequestV2 +module JsonRpc ( JsonRpcVersion(JsonRpcV1 ,JsonRpcV2) + , JsonRpcRequest , JsonRpcNotification , JsonRpcResponse ) where @@ -10,27 +10,21 @@ import Data.ByteString.Lazy (ByteString) import Control.Applicative ((<$>), (<*>), empty) import Data.Aeson -data JsonRpcRequestV1 = JsonRpcRequestV1 { jrpcReqMethod1 :: ByteString - , jrpcReqParams1 :: [ByteString] - , jrpcReqId1 :: ByteString - } deriving (Show) -instance ToJSON JsonRpcRequestV1 where - toJSON (JsonRpcRequestV1 method params id) = - object [ "method" .= method - , "params" .= params - , "id" .= id ] - -data JsonRpcRequestV2 = JsonRpcRequestV2 { jrpcReqMethod2 :: ByteString - , jrpcReqParams2 :: [ByteString] - , jrpcReqId2 :: ByteString - } deriving (Show) -instance ToJSON JsonRpcRequestV2 where - toJSON (JsonRpcRequestV2 jrpcReqMethod2 jrpcReqParams2 jrpcReqId2) = - object [ "jsonrpc" .= toJSON ("2.0" :: ByteString) - , "method" .= jrpcReqMethod2 - , "params" .= jrpcReqParams2 - , "id" .= jrpcReqId2 ] +data JsonRpcVersion = JsonRpcV1 | JsonRpcV2 + deriving (Show) +data JsonRpcRequest = JsonRpcRequest { jrpcVersion :: JsonRpcVersion + , jrpcReqMethod :: ByteString + , jrpcReqParams :: [ByteString] + , jrpcReqId :: ByteString + } deriving (Show) +instance ToJSON JsonRpcRequest where + toJSON (JsonRpcRequest version method params 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 { jrpcNtfMethod :: ByteString , jrpcNtfParams :: [ByteString] } deriving (Show)