json-flatfile-datastore
json-flatfile-datastore copied to clipboard
DateTimeOffset gets converted to local (while DateTime doesn't)
I think it would make more sense if both DateTimeOffset and DateTime don't get converted to local time.
Here is my test code:
var myInstance = new MyClass()
{
DateTimeOffsetProperty = DateTimeOffset.UtcNow,
DateTimeProperty = DateTime.UtcNow
};
using (var store = new DataStore("data.json"))
{
IDocumentCollection<MyClass> collection = store.GetCollection<MyClass>();
collection.DeleteMany(_ => true); // Some cleaning
collection.InsertOne(myInstance); // Serializes DateTimeOffset with local offset and time (incorrect!)
// Serializes DateTime as Zulu/UTC and UTC time (correct)
}
using (var store = new DataStore("data.json"))
{
IDocumentCollection<MyClass> collection = store.GetCollection<MyClass>();
MyClass myInstanceDeserialized = collection.Find(_ => true).Single(); // DateTimeOffset offset and time are local (incorrect!)
// DateTime Kind property and the time are UTC (correct)
}
string serialized = JsonConvert.SerializeObject(myInstance); // JSON.NET serializes DateTimeOffset with offset 0 and UTC time (correct)
MyClass myInstanceDeserializedByJsonDotNet = JsonConvert.DeserializeObject<MyClass>(serialized); // JSON.NET deserializes DateTimeOffset to offset 0 and UTC time (correct)
When I manually change data.json to have UTC time and offset for the DateTimeOffset then it still gets deserialized to a local DateTimeOffset.
Thanks for the issue. We have to think about the correct solution for this.
Here is a test to validate the problem: https://github.com/ttu/json-flatfile-datastore/compare/master...105-fix-datetime-conversions