Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SerializeObject & StreamWriter created file not complete

here is my code :

string json = JsonConvert.SerializeObject(stocksArticles,Formatting.Indented);
StreamWriter sw = new StreamWriter("Export\\Stocks.json");
sw.Write(json);

it’s working fine but the json file is not complete it’s look like the StreamWriter stop before the end, this is the end of the file :

{ "Reference": "999840", "Stocks": { "S": 0.0 } }, { "

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i dont understand why it’s stopping, i tried to define the buffersize for the StreamWriter but no effect.

>Solution :

This is a common sign of not flushing/closing the stream (it is quite common for streams to maintain some buffer for performance reasons). You can do something like:

StreamWriter sw = new StreamWriter("Export\\Stocks.json");
sw.Write(json);
sw.Close();

But far more correct approach is to use using which will dispose the resource correctly:

using (StreamWriter sw = new StreamWriter("Export\\Stocks.json"))
{
    sw.Write(json);
}

Read more:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading