I have an object with a string property that happens to have a value that looks like an XML datetime. I serialize it using JsonConvert.SerializeObject() and deserialize it back using JsonConvert.DeserializeObject(). Normally this works fine. However, if the object implements ISerializable then during deserialization a different value is returned from info.GetString().
It appears that JSON.NET takes it upon itself to parse my string as a date and then convert the date back to a string. The behaviour should not differ based on implementing ISerializable or not. (Actually, I think it should not do this automatic conversion at all, ever, but this is already covered by #862)
The following code reproduces the problem. It can be run as a LINQPad snippet.
// Works if you comment out the ISerializable implementation, but with it AsString deserializes as "05/09/2016 12:34:56"
class MySerializable : ISerializable
{
public MySerializable()
{
}
protected MySerializable(SerializationInfo info, StreamingContext context)
{
AsString = info.GetString("AsString");
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("AsString", AsString);
}
public string AsString
{
get; set;
}
}
void Main()
{
var original = new MySerializable { AsString = "2016-05-09T12:34:56" };
var json = JsonConvert.SerializeObject(original);
var fromJson = JsonConvert.DeserializeObject<MySerializable>(json);
if (fromJson.AsString != original.AsString)
throw new ApplicationException("Mismatch! Deserialized as " + fromJson.AsString);
}
I have an object with a string property that happens to have a value that looks like an XML datetime. I serialize it using JsonConvert.SerializeObject() and deserialize it back using JsonConvert.DeserializeObject(). Normally this works fine. However, if the object implements ISerializable then during deserialization a different value is returned from info.GetString().
It appears that JSON.NET takes it upon itself to parse my string as a date and then convert the date back to a string. The behaviour should not differ based on implementing ISerializable or not. (Actually, I think it should not do this automatic conversion at all, ever, but this is already covered by #862)
The following code reproduces the problem. It can be run as a LINQPad snippet.