Serialize / Deserialize DateTime

1

I'm saving to a text file a serialized object that only contains fields of type DateTime, but the date is saving wrong.

I'll demonstrate with an example what I'm doing.

My DataBaseViewModel object contains two fields of type DateTime

[Serializable]
public class DataBaseViewModel
{
    public DateTime ultimaAtualizacaoTfs { get; set; }
    public DateTime ultimaAtualizacaoSis { get; set; }
}

Adding values to my object

var datasBase = new DatasBaseViewModel();
datasBase.ultimaAtualizacaoSis = new DateTime(2015, 11, 9, 15, 10, 10);
datasBase.ultimaAtualizacaoTfs = new DateTime(2015, 11, 9, 15, 11, 11);

Then save the serialized object into text

using (Stream stream = File.Open(_pathToDatasBase, FileMode.Create))
using (StreamWriter streamWriter = new StreamWriter(stream))
{
    streamWriter.WriteLine(new JavaScriptSerializer().Serialize(dataBase));
}

At another time, I'll read the contents of my text file.

var datasBase = new DataBaseViewModel();
using (Stream stream = File.Open(_pathToDatasBase, FileMode.Open))
using (StreamReader streamReader = new StreamReader(stream))
{
    datasBase = new JavaScriptSerializer().Deserialize<DataBaseViewModel>(streamReader.ReadToEnd());
}

After reading and deserializing, my object is with the wrong date, different from what I saved.

In this case, I saved the dates 09/11/2015 15:10:10 and 09/11/2015 15:11:11 and in the deserialized object I have dates 09/11/2015 17:10:10 and 09/11/2015 17:11:11 .

The text file with the serialized object is saved in this way:

{"ultimaAtualizacaoTfs":"\/Date(1447089071000)\/","ultimaAtualizacaoSis":"\/Date(1447089010000)\/"}
    
asked by anonymous 09.11.2015 / 19:45

1 answer

3

If you change the type of the two properties of your class to DateTimeOffset you will be in debug that at the end of the value it will be -2, when decerializing the object and looking at the same value in the new class the value goes being 0, are exactly the two hours that are increasing.

Just change the way you create the date that it will work normal.

            datasBase.ultimaAtualizacaoTfs = new DateTime(2015, 11, 9, 15, 10, 10,  DateTimeKind.Utc);
            datasBase.ultimaAtualizacaoSis = new DateTime(2015, 11, 9, 15, 11, 11, DateTimeKind.Utc

You will also notice that the serialized value is different now because it is putting the date with the correct value.

You do not need to change the value of properties to DateTimeOffset , only if you are curious to see the difference I have made. It works normal with DateTime

            var dataCerta = new DateTime();
            var dataErrada = new DateTime();

            dataCerta = new DateTime(2015, 11, 9, 15, 10, 10,  DateTimeKind.Utc);
            dataErrada = new DateTime(2015, 11, 9, 15, 10, 10);


            var dataCertaSerializada = new JavaScriptSerializer().Serialize(dataCerta);
            var dataErradaSerializada = new JavaScriptSerializer().Serialize(dataErrada);

            Console.Write(String.Format("Valor serializado da data certa:  {0} \n", dataCertaSerializada.ToString()));
            Console.Write(String.Format("Valor serializado da data errada: {0} \n", dataErradaSerializada.ToString()));

            var dataCertaDecerializada = new JavaScriptSerializer().Deserialize<DateTime>(dataCertaSerializada);
            var dataErradaDecerializada = new JavaScriptSerializer().Deserialize<DateTime>(dataErradaSerializada);

            Console.Write(String.Format("Valor Deserializado da data certa:  {0} \n", dataCertaDecerializada.ToString()));
            Console.Write(String.Format("Valor Deserializado da data errada: {0} \n", dataErradaDecerializada.ToString()));
    
10.11.2015 / 02:41