title: .Net Core send/receive JSON over TCP
body: <p>I want to send and receive JSON over TCP. 
  QUESTION: I have to send and receive JSON in my TCP client-server. How can I achieve it? </p>
  
  <p>I use TcpListener and TcpClient to connect and I have this code:</p>
  
  <pre><code>    NetworkStream stream = client.GetStream();
  
      var serializer = new JsonSerializer();
      var sr = new StreamReader(stream, new UTF8Encoding(), false);
      var jsonTextReader = new JsonTextReader(sr);
      var data = serializer.Deserialize(jsonTextReader).ToString();
  
      Console.WriteLine("Received: {0}", data);
  
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);
      JsonWriter writer = new JsonTextWriter(sw);
      writer.WriteValue('1');
      byte[] buffer = Encoding.ASCII.GetBytes(writer.ToString());
      stream.Write(buffer, 0, buffer.Length);
  </code></pre>
  
  <p>Can I do it better? The client has to receive JSON(I use Newtonsoft.Json) and I don't know if it is even good code. Maybe you write me some good practices? Or maybe some tips. </p>
  
  <p><strong>EDIT.</strong>
  Now I wrote something like this: </p>
  
  <pre><code>public static T DeserializeFromStream&lt;T&gt;(Stream stream)
  {
      using (var sr = new StreamReader(stream))
      using (var jsonTextReader = new JsonTextReader(sr))
      {
          return new JsonSerializer().Deserialize&lt;T&gt;(jsonTextReader);
      }
  }
  </code></pre>
  
  <p>And it doesn't work because Java client send me array like:  [{"name" : "logo", "session" : "i3fnj34njn780"}] So how can I fix this problem? I want call it this way: <code>Method ar = DeserializeFromStream&lt;Method&gt;(client.GetStream());</code> Trim and Replace doesn't work for me here. </p>