]> www.average.org Git - pdns-pipe-nmc.git/blobdiff - Data/JsonRpc.hs
wip JsonRpc response
[pdns-pipe-nmc.git] / Data / JsonRpc.hs
index b1879bccef89f57638ad44a122ad2d93f1c892c1..9b3ff48965fe47a3731567e70652831aae9e6ba7 100644 (file)
@@ -1,20 +1,44 @@
-module JsonRpc  ( JsonRpcRequest(..)
-                , JsonRpcNotification(..)
-                , JsonRpcResponse(..)
+{-# LANGUAGE OverloadedStrings #-}
+
+module JsonRpc  ( JsonRpcVersion(JsonRpcV1, JsonRpcV2)
+                , JsonRpcRequest
+                , JsonRpcNotification
+                , JsonRpcError
+                , JsonRpcResponse
+                , parseJsonRpc
                 ) where
 
-import Data.ByteString (ByteString)
+import Data.ByteString.Lazy (ByteString)
 import Control.Applicative ((<$>), (<*>), empty)
+import Data.Either
 import Data.Aeson
 
-data JsonRpcRequest = JsonRpcRequest { jrpcReqMethod :: ByteString
-                                     , jrpcReqParams :: [ByteString]
-                                     , jrpcReqId     :: ByteString
+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)
+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 JsonRpcError = JsonRpcError { jrpcErrCode    :: Int
                                  , jrpcErrMessage :: ByteString
@@ -26,4 +50,5 @@ data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value
                                        , jrpcRspId     :: ByteString
                                        } deriving (Show)
 
-
+parseJsonRpc :: ByteString -> Either JsonRpcError JsonRpcResponse
+parseJsonRpc _ = Left $ JsonRpcError (-1) "someerror" Nothing