title: Newtonsoft Json.net - how to serialize content of a stream?
body: <p>I need to convert to JSON <em>arbitrary content</em> of a memory stream. Here is a quick example of what I am trying to do:</p>
  
  <pre><code>class Program
  {
      class TestClass { public int Test1;}
      static void Main(string[] args)
      {
          var ms = new MemoryStream();
          var writer = new StreamWriter(ms);
          writer.Write(new TestClass());
          writer.Flush();
          ms.Position = 0;
  
          var json = JsonConvert.SerializeObject(/*???*/, Formatting.Indented);
          Console.Write(json);
          Console.Read();
      }
  }
  </code></pre>
  
  <p>Not sure what to pass to the SerializeObject method. If I pass the MemoryStream (variable ms) I get an error:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'Newtonsoft.Json.JsonSerializationException' occurred in
    Newtonsoft.Json.dll</p>
    
    <p>Additional information: Error getting value from 'ReadTimeout' on
    'System.IO.MemoryStream'.</p>
  </blockquote>
  
  <p>Is that possible to serialize arbitrary content of a stream ?</p>
  
  <p>Thank you</p>