According to Reading multiple JSON objects on a JSON-RPC connection it shall be possible to send multiple requests one after another. My problem is how to parse the requests with Newtonsoft.Json? The JsonSerializer reads obviously more bytes than neccessary for deserializing the first request. The following snippet shows the problem:
class JsonRpcRequest
{
public int? id;
public string jsonrpc;
public string method;
public object @params;
}
class AddParam
{
public int a;
public int b;
}
static void Main(string[] args)
{
var p = new AddParam()
{
a = 100,
b = 200,
};
var request = new JsonRpcRequest()
{
jsonrpc = "2.0",
method = "Add",
@params = p,
};
var stream = new MemoryStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
var ser = new JsonSerializer();
request.id = 100;
ser.Serialize(writer, request);
writer.Flush();
// stream.Position is 68
request.id = 101;
ser.Serialize(writer, request);
writer.Flush();
// stream.Position is 136
stream.Position = 0;
// Stream holds
// {"id":100,"jsonrpc":"2.0","method":"Add","params":{"a":100,"b":200}}{"id":101,"jsonrpc":"2.0","method":"Add","params":{"a":100,"b":200}}
var r1 = ser.Deserialize(reader, typeof(JsonRpcRequest));
// r1 holds first Request
// But stream.Position is already 136
var r2 = ser.Deserialize(reader, typeof(JsonRpcRequest));
// r2 is null !!!???
}