-
-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DateTime fails Serialization/Deserialization roundtrip when JsonCompatible is on #845
Comments
It’s due to the datetime kind you have set. If the kind is local it will use it as is. Otherwise it converts it to utc. since time zones offsets are not in the datetime object we can’t deserialize it back to the exact object that it was. That’s what the datetimeoffset type is for. With the type being set to unspecified there’s no way for the code to know exactly what it is, utc or local. It should probably have not cared and just wrote it out as is from the beginning. However, changing that now has the potential to cause big issues with current consumers expecting the current results so I’m very hesitant to do that. I would be fine with making an option with defaulting to the current behavior. |
It seems that converter is only used when YamlDotNet/YamlDotNet/Serialization/SerializerBuilder.cs Lines 329 to 339 in 1a73db7
which is a bit weird. I suppose it's ok if you say that it's an expected behavior. On the other hand, that deserialize of serialized value yields identical result seems like a very natural invariant. For example, Console.WriteLine(JsonSerializer.Serialize(new DateTime(656788608000000000L, DateTimeKind.Unspecified)));
Console.WriteLine(JsonSerializer.Serialize(new DateTime(656788608000000000L, DateTimeKind.Local)));
Console.WriteLine(JsonSerializer.Serialize(new DateTime(656788608000000000L, DateTimeKind.Utc))); outputs:
And that's basically what var ser = new SerializerBuilder().Build();
Console.WriteLine(ser.Serialize(new DateTime(656788608000000000L, DateTimeKind.Unspecified)));
Console.WriteLine(ser.Serialize(new DateTime(656788608000000000L, DateTimeKind.Local)));
Console.WriteLine(ser.Serialize(new DateTime(656788608000000000L, DateTimeKind.Utc))); --->
|
We can add that in as an option. It would be good to be able to round trip deserialize/serialize. It needs to be an option defaulted to current behavior though. |
I admit I don't understand motivation here. Is |
I see where you're coming from. |
Hi!
The following test case appears to fail:
Values seem to differ by my local timezone offset, so if your timezone is UTC then it may work, I guess? It also seems to work correctly with
DateTimeKind.Utc
or whenJsonCompatible()
is not present.Tested with:
The text was updated successfully, but these errors were encountered: