Search code examples
visual-studiojson.netjsonserializerimmediate-window

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


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?


Solution

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

    file.Close();
    

    Thanks!