I need to serialize and deserialize a list and write it on a JSON file to use later.
I successfully desirialize the file but failed to write after serialization. How can I do that?
Here is the code I have written.
StorageFile savedFile = await storageFolder.GetFileAsync("SavedData.json");
string text = await FileIO.ReadTextAsync(savedFile);
var serializer = new DataContractJsonSerializer(typeof(DataFormat));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(text));
List<DataFormat> data = (List<DataFormat>)serializer.ReadObject(ms);
        if (data == null)
        {
            data = new List<DataFormat>();
        }
        data.Add(new DataFormat
        {
            firstName = fnbox.Text,
            lastName = lnbox.Text,
            country = cbox.Text
        });
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataFormat));
ser.WriteObject(stream, data);
DataFormat Class -
[DataContract]
public class DataFormat : IEquatable<DataFormat>
{
    [DataMember]
    public string firstName{ get; set; }
    [DataMember]
    public string lastName { get; set; }
    [DataMember]
    public string country { get; set; }
    public bool Equals(DataFormat other)
    {
        if (other == null)
        {
            return false;
        }
        return (firstName.Equals(other.firstName));
    }
}
Additionally If there is any way to just add lines into an existing file without replacing all the text, please let me know.
 
     
     
     
    