Json Rpc - unwrapping responses

Mar 15, 2012 at 10:54 PM

I am implementing a Json-Rpc interface over sockets.  When I get a message response I need to deserialize to see the transaction id, to match it up with the sender.

Here is the send message.

{"id":"1deb1149d65b8946","method":"listMethods","arguments":[]}

Here is a response.

{"error":null,"response":[{"arguments":{"echoString":"*"},"methodName":"echo","returnType":"string[]"},{"arguments":{},"methodName":"listMethods","returnType":"dct[]"},{"arguments":{"themeID":"int","path":"string"},"methodName":"addTheme","returnType":"boolean"},{"arguments":{"properties":"xml"},"methodName":"setProjectProperties","returnType":"boolean"},{"arguments":{"libraryID":"int","path":"string"},"methodName":"loadLibrary","returnType":"boolean"},{"arguments":{"pageID":"int","properties":"xml"},"methodName":"addPage","returnType":"boolean"}],"id":"1deb1149d65b8946"}

When I get the response, I need to peek into get the ID to match it up with the send message and then it is dispatched to where the message was initiated.

So I deserialize the response string into RpcResponse , the .response is of type object and ends up being JArray (in this case).  Note that until I deserialize the response and match back to the send message I don't really know how to handle the result and what the desired target type should be.

public class RpcResponse
{
	public String id { get; set; }
	public Object response { get; set; }
	public Object error { get; set; }
}

So now I have the RpcResponse built up and matched back to the sender, I know what type to deserialize the response to.  I'm very comfortable using the JsonConvert.Deserialize but that takes strings. It's already at least partially parsed into .response (JArray, JObject, etc).  Is there a way to Deserialize from JArray, JObject to my target type?  OR take a brute force approach and create another RpcResponse object with a strongly typed .response property (according to the desired target type).

The key problem that I'd like to avoid is deserializing twice.  Perhaps, you have a simple way to deserialize just enough to get the id only.

Thanks for your help.