This project has moved. For the latest updates, please go here.

Json Rpc - unwrapping responses

Mar 15, 2012 at 9: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.


Here is a response.


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.