Serializing an IEnumerable of objects with Newtonsoft in Immediate Window chops the results

169 Views Asked by At

We have tried this code that works in the class but fails in the Immediate Window:

System.IO.StreamWriter file = System.IO.File.CreateText("z:\\file.json");
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Serialize(file, myCollection);
file.Close();

Even if the Immediate Window returns "Expression has been evaluated and has no value" after each command, the file only has a part of the Json: the file ends abruptly in the middle of a word. Any idea on how to solve this?

1

There are 1 best solutions below

0
user33276346 On BEST ANSWER

As @dbc pointed out in a comment, the problem was a premature file inspection before properly calling

file.Close();

Thanks!