When we deserialize a document from DocumentDB, all of our DateTimeOffset fields get converted to local time. This behavior is documented in #235 and #197. In addition, I am unable to override this behavior. Using a JsonConverter fails because the converter is only run after the token has been parsed, so it is already a DateTime object and my data is already lost. I'm also unable to get to the raw value to parse myself. Similarly, using a string serialization surrogate doesn't work because it gets the ToString representation of the DateTime object that has already stripped the time zone. Both issues have been closed, but no workable solution is available
Probably the reason of the issue is inside Json.Net.
@ermontgo What version of the SDK are you using?
@ealsur This is running the Microsoft.Azure.DocumentDB 1.15.0 NuGet package
Note:
the document is stored as expected the problem is when you read the document (at least using dynamic).
When reading from DocDb, the property seems to be managed as DateTime with DateTimeKind.Unspecified so when is returned by and MVC action it is transformed again to string using the default (local server CurrentUICulture).
In MVC core we can use
.AddJsonOptions(options =>
{
options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
options.SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset;
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind;
});
but, again, the problem is the source... I'm working on it... I be back here
DONE
As said at the begin the problem is the configuration of Json.Net
The solution is
store.RegisterSingleton(c => new DocumentClient(new Uri(docDbConfiguration(c).EndPoint)
, docDbConfiguration(c).AuthKey
, new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = DateParseHandling.DateTimeOffset,
DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
}
, c.GetInstance<ConnectionPolicy>() ?? new ConnectionPolicy { EnableEndpointDiscovery = false })
);
The same configuration of JSON.NET have to be used everywhere from the front-end to the storage and viceversa.
I think you can close the issue and document the solution somewhere.
Thanks.
Most helpful comment
DONE
As said at the begin the problem is the configuration of Json.Net
The solution is
store.RegisterSingleton(c => new DocumentClient(new Uri(docDbConfiguration(c).EndPoint) , docDbConfiguration(c).AuthKey , new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind } , c.GetInstance<ConnectionPolicy>() ?? new ConnectionPolicy { EnableEndpointDiscovery = false }) );The same configuration of JSON.NET have to be used everywhere from the front-end to the storage and viceversa.
I think you can close the issue and document the solution somewhere.
Thanks.