]> www.average.org Git - pdns-pipe-nmc.git/blob - Data/JsonRpc.hs
79c21c349dcde14e791275a53bd5649d196ea07c
[pdns-pipe-nmc.git] / Data / JsonRpc.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module JsonRpc  ( JsonRpcVersion(JsonRpcV1 ,JsonRpcV2)
4                 , JsonRpcRequest
5                 , JsonRpcNotification
6                 , JsonRpcResponse
7                 ) where
8
9 import Data.ByteString.Lazy (ByteString)
10 import Control.Applicative ((<$>), (<*>), empty)
11 import Data.Aeson
12
13 data JsonRpcVersion = JsonRpcV1 | JsonRpcV2
14         deriving (Show)
15
16 data JsonRpcRequest = JsonRpcRequest { jrpcVersion    :: JsonRpcVersion
17                                      , jrpcReqMethod  :: ByteString
18                                      , jrpcReqParams  :: [ByteString]
19                                      , jrpcReqId      :: ByteString
20                                      } deriving (Show)
21 instance ToJSON JsonRpcRequest where
22   toJSON (JsonRpcRequest version method params id) =
23     let l = [ "method" .= method , "params" .= params , "id" .= id ]
24     in case version of
25       JsonRpcV1 -> object l
26       JsonRpcV2 -> object $ ("jsonrpc" .= toJSON ("2.0" :: ByteString)):l
27     
28 data JsonRpcNotification = JsonRpcNotification { jrpcNtfMethod :: ByteString
29                                                , jrpcNtfParams :: [ByteString]
30                                                } deriving (Show)
31
32 data JsonRpcError = JsonRpcError { jrpcErrCode    :: Int
33                                  , jrpcErrMessage :: ByteString
34                                  , jrpcErrData    :: Maybe Value
35                                  } deriving (Show)
36
37 data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value
38                                        , jrpcRspError  :: JsonRpcError
39                                        , jrpcRspId     :: ByteString
40                                        } deriving (Show)