0

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?

user33276346
  • 1,501
  • 1
  • 19
  • 38
  • 2
    You are probably looking at the results on disk before the call to `file.Close()`. Before the file is closed some of the JSON may be buffered in memory. Also, in your real code you should close the `file` via a `using` statement rather than via an explicit `Close()` as this guarantees unmanaged resources will be disposed of in the event of an exception, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement – dbc Apr 30 '20 at 13:33
  • But we really need to see a [mcve] to give a definitive answer. – dbc Apr 30 '20 at 17:22

1 Answers1

0

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

file.Close();

Thanks!

user33276346
  • 1,501
  • 1
  • 19
  • 38