Windows Phone 8.1 WinRT Json Serialize + DeSerialize

private static string JSON_MARK_FILE_NAME = "markJsonData.json";

Serialize object to a storage file

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile = await localFolder.CreateFileAsync(JSON_MARK_FILE_NAME, CreationCollisionOption.ReplaceExisting);
using(Stream stream = await textFile.OpenStreamForWriteAsync()) {
	string content = await Newtonsoft.Json.JsonConvert.SerializeObjectAsync(grades);
	using(StreamWriter streamWriter = new StreamWriter(stream))
	await streamWriter.WriteAsync(content);
}

DeSerialize a storage file to a object

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textFile = await localFolder.GetFileAsync(JSON_MARK_FILE_NAME);
using(IRandomAccessStream stream = await textFile.OpenReadAsync())
using(StreamReader streamReader = new StreamReader(stream.AsStreamForRead()))
return await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync < List < Grade >> ((await streamReader.ReadToEndAsync()));

Check if the storage file exists before you call GetFileAsync. (Which is ignored at the example above)

0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments