I have an example of a program:
using System;
using Newtonsoft.Json;
using System.IO;
public class Program
{
public static void Main()
{
using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
using (var jsonWriter = new JsonTextWriter(writer))
{
new JsonSerializer().Serialize(jsonWriter, new { name = "Jamie" });
Console.WriteLine("stream length: " + stream.Length); // stream length: 0
Console.WriteLine("stream position: " + stream.Position); // stream position: 0
Console.WriteLine("stream contents: (" + reader.ReadToEnd() + ")"); // stream contents: ()
}
}
}
It should (according to this page: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) make a stream containing a JSON representation of the object: obj
but in reality the stream appears to have length 0
and is an empty string when written out. What can I do to achieve the correct serialization?
Here is an example of the program running: https://dotnetfiddle.net/pi1bqE