1

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 !!!???
}
AndiR
  • 179
  • 10
  • 2
    You can use the answer from [Line delimited json serializing and de-serializing](https://stackoverflow.com/q/29729063) and [Deserialize multiple json objects from a stream using Newtonsoft Json](https://stackoverflow.com/q/48882653). If you need to serialize in the same format see [Serialize as NDJSON using Json.NET](https://stackoverflow.com/q/44787652) (though in your case you would skip the call to `textWriter.Write("\n");`). – dbc Aug 16 '18 at 19:32
  • 1
    Ok, this works so far with the environment from above. Will it work with Networkstream too? Due the packet characteristic of TCP/IP it can happen a partial telegram has been arrived first; the second part will be delivered than with the next packet. I would run the read loop in a thread. – AndiR Aug 16 '18 at 19:45
  • This I don't know for sure. [this answer](https://stackoverflow.com/a/4035247) suggests it should work, but [NetworkStream.Read - can it collect partial messages?](https://stackoverflow.com/q/5687266) (no accepted answer though) suggests not. [How to get all data from NetworkStream](https://stackoverflow.com/q/26058594) looks related but is actually somewhat different, and [What is the correct way to read from NetworkStream in .NET](https://stackoverflow.com/q/13097269) makes it all seem quite complex. Could you consider using a higher-level protocol such as a REST API? – dbc Aug 16 '18 at 20:27
  • I'd also suggest rewriting this question (or marking this one as a duplicate and asking a new question) so that it that includes a [mcve] showing how you are hooking a `JsonTextReader` up to a `NetworkStream` via a `StreamReader`, and asks whether it should work. You might also try it yourself by sequentially sending some very large objects. – dbc Aug 16 '18 at 20:29
  • 1
    I will check it tomorrow and post the result here. It will be maybe difficult to provide a short test fixture since NetworkStream is not easily mock'able. – AndiR Aug 16 '18 at 20:34
  • Mocking might not be the *initial* answer here. You might actually need to set up a simplified client and server and test what happens when you kill or suspend the server, or unplug the network cable. Later you can mock up the results of your tests. – dbc Aug 16 '18 at 21:05
  • You might also need to think about encrypting your streams for privacy and security reasons as explained in [Is NetworkStream Secure?](https://stackoverflow.com/q/46978480). Using a higher level protocol might help here, see e.g. [Working with SSL in Web API](https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/working-with-ssl-in-web-api). – dbc Aug 16 '18 at 21:05
  • 1
    JsonTextReader is buggy ans works unreliable under load. If I instead parse the incoming stream by myself and extract the Objects it works. – AndiR Aug 20 '18 at 08:59

0 Answers0