]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
Add a jsonrpc classifier for a json value.
authorPrashanth Mundkur <prashanth.mundkur@citrix.com>
Thu, 4 Jun 2009 07:18:24 +0000 (00:18 -0700)
committerPrashanth Mundkur <prashanth.mundkur@citrix.com>
Tue, 23 Jun 2009 16:24:48 +0000 (09:24 -0700)
libs/jsonrpc/jsonrpc.ml
libs/jsonrpc/jsonrpc.mli

index bfc621529daf111d1b789b8a8a59f55fa5e591db..ca6ca11ff2b91046c54ef9763ceac41f921c82f7 100644 (file)
@@ -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
index d672cff7a64cff44ef25603c3216512a54491312..46ef33279d332f2b9ef2d2f43bef313112b98d54 100644 (file)
@@ -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