Json.net has the async functions for converting an object to json like:
json = await JsonConvert.DeserializeObjectAsync<T>
But when I want to write an object to a json file it seems better to me to do it directly using a file Stream.
So i think it should be something like this:
var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
{
using (StreamWriter sw = new StreamWriter(fileStream.AsStreamForWrite()))
{
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, obj);
}
}
But on the JsonSerzializer Object I can't find async methods. Also I think that IO operations shouldn't be placed in a own thread.
What is the recommended approach ?