I have implemented a JSON-RPC with Json.NET. The result of the response I convert with:
T data = ((JToken)value).ToObject<T>();
This work if the result is an object. But it does not work if the result is an primitive like Int64 or an string. Then the value is not a JToken.
I have found the method:
static object Convert(object initialValue, CultureInfo culture, Type targetType)
which seems to do the needed job. But the class ConvertUtils is internal. It there any public method to convert any parsed JSON result to any target type?
A more completely sample with the JSON-RPC handling:
JsonRpcResult result = serializer.Deserialize<JsonRpcResult>(jsonTextReader);
Type type = requests[result.id];
object value = result.result;
return ((JToken)value).ToObject(type);
A typical JSON string:
{"jsonrpc":"2.0","result":DynamicValidJsonDependsOnID,"id":1}
The JSON content of DynamicValidJsonDependsOnID depends on the id and can be any valid JSON. There are no limits.
The class JsonRpcResult:
[JsonObject(MemberSerialization.Fields)]
internal class JsonRpcResult
{
private string jsonrpc;
private object result;
private Dictionary<String,Object> error;
private object id;
}