JSON-RPCリクエストを2つのステップで逆シリアル化したい:
ステップ1:解析id
しmethod
て基本クラスに変換する
ステップ2:params
メソッドに応じて2番目のクラスに解析する
例:
interface IAction
{
bool Exec();
}
class Request
{
[JsonProperty("method")]
public string Method;
[JsonProperty("params")]
public RawJson Params;
[JsonProperty("id")]
public RawJson Id;
}
class Whisper : IAction
{
[JsonProperty("to")]
public string To;
[JsonProperty("msg")]
public string Message;
public bool Exec()
{
// Perform the whisper action
return true;
}
}
class Say : IAction
{
[JsonProperty("msg")]
public string Message;
public bool Exec()
{
// Perform the say action
return true;
}
}
コード(のようなオブジェクトがあった場合RawJson
)
Request req = JsonConvert.DeserializeObject<Request>(s);
if( req.Method == "whisper" )
{
Whisper whisper = RawJson.DeserializeObject<Whisper>();
whisper.Exec();
}
それを解決する1つの方法は、Json.NETにある種の生のjsonオブジェクトがある場合ですが、それが見つかりません。
それで、私は生のオブジェクトを見逃しただけですか、それともこれを行うための別の良い方法がありますか?
追伸 値は、null、string、int、arrayなどの任意のid
値にすることができます。json-rpc応答で返されるだけです。