From: Prashanth Mundkur Date: Thu, 4 Jun 2009 07:18:24 +0000 (-0700) Subject: Add a jsonrpc classifier for a json value. X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=8e38a477227c41e00ba6de0d92b65d34546f4195;p=xenclient%2Ftoolstack.git Add a jsonrpc classifier for a json value. --- diff --git a/libs/jsonrpc/jsonrpc.ml b/libs/jsonrpc/jsonrpc.ml index bfc6215..ca6ca11 100644 --- a/libs/jsonrpc/jsonrpc.ml +++ b/libs/jsonrpc/jsonrpc.ml @@ -59,9 +59,10 @@ type rpc_request = params: Json.t } +type rpc_error = int * string * Json.t option (* code, message, optional_data *) type rpc_response_payload = | Result of Json.t - | Error of (int * string * Json.t option) (* code, message, optional data *) + | Error of rpc_error type rpc_response = { @@ -146,3 +147,24 @@ let response_to_json resp = | Error e -> error_to_json resp.response_id e let response_to_string resp = Json.to_string (response_to_json resp) + + +type rpc_type = + | Non_JSONRPC + | Request of (* id *) Json.t * rpc_request + | Notification of rpc_request + | Response of rpc_response + +let rpc_type_of_json j = + (* First try request/notification. *) + try + let req = request_of_json j in + match req.request_id with + | None -> Notification req + | Some id -> Request (id, req) + with _ -> + (* Next, try a response. *) + try + Response (response_of_json j) + with _ -> + Non_JSONRPC diff --git a/libs/jsonrpc/jsonrpc.mli b/libs/jsonrpc/jsonrpc.mli index d672cff..46ef332 100644 --- a/libs/jsonrpc/jsonrpc.mli +++ b/libs/jsonrpc/jsonrpc.mli @@ -42,9 +42,10 @@ type rpc_request = params : Json.t; } +type rpc_error = int * string * Json.t option (* code, message, optional_data *) type rpc_response_payload = | Result of Json.t - | Error of (int * string * Json.t option) (* code, message, optional_data *) + | Error of rpc_error type rpc_response = { @@ -67,3 +68,12 @@ val request_of_string: string -> rpc_request val request_to_string: rpc_request -> string val response_of_string: string -> rpc_response val response_to_string: rpc_response -> string + +(* determine what type of jsonrpc object a json value is *) +type rpc_type = + | Non_JSONRPC + | Request of (* id *) Json.t * rpc_request + | Notification of rpc_request + | Response of rpc_response + +val rpc_type_of_json: Json.t -> rpc_type