You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in System.Text.Json serializing and deserializing datetimeoffset looses precision and most important part the offset part.
There is no different between datetime and datetimeoffset after serialization. In multi time environments it cases serious issues since all times are treated as UTC,
JsonString uses "s" format
publicJsonString(DateTimeOffset value)=> Value =value.ToString("s",CultureInfo.InvariantCulture);
Which is not producing right value. It totally ignores offset.
@ahsonkhan Hello
I was looking why my json deserialization is not accepting +1000. as time offset in string.
But indeed above issue is valid. JsonString definitely has wrong format when producing or reading DateTimeOffset. Is should support exactly the same rules as Serializer. Or at least o not s
To clarify (and correct me if I am mistaken), but the issue you have isn't about the serialization/deserialization of JSON strings (using JsonSerializer)? It's specifically about the JsonString node only, correct?
Can you provide a sample repro with your expected output/behavior of processing DateTimeOffset compared to what actually happens?
@ahsonkhan The issue is related to code I found in latest JsonString class. Which is not released yet. So I cannot easily create repo for that. If you look into public DateTimeOffset GetDateTimeOffset() => DateTimeOffset.ParseExact(_value, "s", CultureInfo.InvariantCulture);
and public JsonString(DateTimeOffset value) => Value = value.ToString("s", CultureInfo.InvariantCulture);
you will notice "s" formatter which is not right for DateTimeOffset. It is only suitable for DateTime.
changed the title [-]System.Text.Json DateTimeOffset serialization and deserialization work wrongly[/-][+]JsonString node doesn't round-trip DateTimeOffset correctly[/+]on Oct 14, 2019
The purpose of the "s" format specifier is to produce result strings that sort consistently in ascending or descending order based on date and time values. As a result, although the "s" standard format specifier represents a date and time value in a consistent format, the formatting operation does not modify the value of the date and time object that is being formatted to reflect its DateTime.Kind property or its DateTimeOffset.Offset value. For example, the result strings produced by formatting the date and time values 2014-11-15T18:32:17+00:00 and 2014-11-15T18:32:17+08:00 are identical.
IMO, that's problematic for DateTimeOffset (since it doesn't preserve the offset), but could it also be problematic for DateTime (since it doesn't consider its DateTime.Kind)?
Reviewing Utf8JsonReader's usage of JsonHelpers.TryParseAsISO it seems the read side would need a decent amount of work...or perhaps just fall back to DateTimeOffset.Parse without a format specifier.
IMO, that's problematic for DateTimeOffset (since it doesn't preserve the offset), but could it also be problematic for DateTime (since it doesn't consider its DateTime.Kind)?
DateTime.Kind is mostly worthless.
DateTimeKind.Local is almost certainly not going to match whatever the local timezone of the other party is. So you have to at least send a timezone id and/or an offset anyways (or at least specify it in API documentation). And if the other party is a server, the local timezone is almost always irrelevant (in preference for the zone of some configured entity).
DateTimeKind.Unspecified is shorthand for "nobody knows the status without more information", so you're in the same boat. That's ignoring issues converting from it.
DateTimeKind.Utc is at least unambiguous, but only covers logged (past) events, given that most people schedule things based on a local timezone (note, not the offset).
Activity
msmolka commentedon Oct 10, 2019
Invalid
ahsonkhan commentedon Oct 11, 2019
@msmolka - for the benefit of readers, can you explain what was your reasoning for closing the issue? Why is it invalid?
msmolka commentedon Oct 11, 2019
@ahsonkhan Hello
I was looking why my json deserialization is not accepting
+1000. as time offset in string.But indeed above issue is valid.
JsonStringdefinitely has wrong format when producing or readingDateTimeOffset. Is should support exactly the same rules asSerializer. Or at leastonotsahsonkhan commentedon Oct 14, 2019
To clarify (and correct me if I am mistaken), but the issue you have isn't about the serialization/deserialization of JSON strings (using JsonSerializer)? It's specifically about the
JsonStringnode only, correct?Can you provide a sample repro with your expected output/behavior of processing DateTimeOffset compared to what actually happens?
Thanks.
msmolka commentedon Oct 14, 2019
@ahsonkhan The issue is related to code I found in latest
JsonStringclass. Which is not released yet. So I cannot easily create repo for that. If you look intopublic DateTimeOffset GetDateTimeOffset() => DateTimeOffset.ParseExact(_value, "s", CultureInfo.InvariantCulture);and
public JsonString(DateTimeOffset value) => Value = value.ToString("s", CultureInfo.InvariantCulture);you will notice "s" formatter which is not right for
DateTimeOffset. It is only suitable forDateTime.ahsonkhan commentedon Oct 14, 2019
You could try using the nightly build/NuGet package that is being built out of master:
Add the following NuGet feed and use the
System.Text.Jsonpackage, version5.0.0-alpha1.19513.1https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json
https://github.com/dotnet/corefx/#daily-builds
[-]System.Text.Json DateTimeOffset serialization and deserialization work wrongly[/-][+]JsonString node doesn't round-trip DateTimeOffset correctly[/+]ahsonkhan commentedon Oct 14, 2019
Updated the title since the issue is not related to serialization/deserialization.
khellang commentedon Oct 14, 2019
From The Sortable ("s") Format Specifier docs (emphasis mine):
IMO, that's problematic for
DateTimeOffset(since it doesn't preserve the offset), but could it also be problematic forDateTime(since it doesn't consider itsDateTime.Kind)?watfordgnf commentedon Oct 14, 2019
JsonStringshould use theOformat specifier used by Utf8JsonWriter.https://github.com/dotnet/corefx/blob/7b90f41828abea1d9db6d55403a849c2613d996d/src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTime.cs#L109
watfordgnf commentedon Oct 14, 2019
Tests missed this because they did not include times or offsets for DateTimeOffset:
https://github.com/dotnet/corefx/blob/37b37395481b635c15eaa3454e52b72ab91de4ef/src/System.Text.Json/tests/JsonStringTests.cs#L88-L95
Use O format in JsonString for DateTime(Offset) #41706
watfordgnf commentedon Oct 14, 2019
It looks like GUID's are correctly using
Dwhich matchesUtf8Formatter.TryFormatused byUtf8JsonWriter.msmolka commentedon Oct 14, 2019
@watfordgnf Be carefull with
"O"format for parsing. It is very strict. JsonReader is parsing datetimeoffset in different way.watfordgnf commentedon Oct 14, 2019
Fair point, but more correct than
"s".Reviewing
Utf8JsonReader's usage ofJsonHelpers.TryParseAsISOit seems the read side would need a decent amount of work...or perhaps just fall back toDateTimeOffset.Parsewithout a format specifier.Clockwork-Muse commentedon Oct 15, 2019
@khellang
DateTime.Kindis mostly worthless.DateTimeKind.Localis almost certainly not going to match whatever the local timezone of the other party is. So you have to at least send a timezone id and/or an offset anyways (or at least specify it in API documentation). And if the other party is a server, the local timezone is almost always irrelevant (in preference for the zone of some configured entity).DateTimeKind.Unspecifiedis shorthand for "nobody knows the status without more information", so you're in the same boat. That's ignoring issues converting from it.DateTimeKind.Utcis at least unambiguous, but only covers logged (past) events, given that most people schedule things based on a local timezone (note, not the offset).