]> www.average.org Git - pdns-pipe-nmc.git/blob - Data/JsonRpc.hs
64d184c72809f778a092cd82cd2ee14df075fadd
[pdns-pipe-nmc.git] / Data / JsonRpc.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module JsonRpc  ( JsonRpcRequestV1
4                 , JsonRpcRequestV2
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 JsonRpcRequestV1 = JsonRpcRequestV1 { jrpcReqMethod1  :: ByteString
14                                          , jrpcReqParams1  :: [ByteString]
15                                          , jrpcReqId1      :: ByteString
16                                          } deriving (Show)
17 instance ToJSON JsonRpcRequestV1 where
18   toJSON (JsonRpcRequestV1 method params id) =
19     object [ "method"  .= method
20            , "params"  .= params
21            , "id"      .= id ]
22     
23 data JsonRpcRequestV2 = JsonRpcRequestV2 { jrpcReqMethod2  :: ByteString
24                                          , jrpcReqParams2  :: [ByteString]
25                                          , jrpcReqId2      :: ByteString
26                                          } deriving (Show)
27 instance ToJSON JsonRpcRequestV2 where
28   toJSON (JsonRpcRequestV2 jrpcReqMethod2 jrpcReqParams2 jrpcReqId2) =
29     object [ "jsonrpc" .= toJSON ("2.0" :: ByteString)
30            , "method"  .= jrpcReqMethod2
31            , "params"  .= jrpcReqParams2
32            , "id"      .= jrpcReqId2 ]
33
34 data JsonRpcNotification = JsonRpcNotification { jrpcNtfMethod :: ByteString
35                                                , jrpcNtfParams :: [ByteString]
36                                                } deriving (Show)
37
38 data JsonRpcError = JsonRpcError { jrpcErrCode    :: Int
39                                  , jrpcErrMessage :: ByteString
40                                  , jrpcErrData    :: Maybe Value
41                                  } deriving (Show)
42
43 data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value
44                                        , jrpcRspError  :: JsonRpcError
45                                        , jrpcRspId     :: ByteString
46                                        } deriving (Show)