Skip to content

JsonString node doesn't round-trip DateTimeOffset correctly #31135

Closed
dotnet/corefx
#41940

Description

@msmolka

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

 public JsonString(DateTimeOffset value) => Value = value.ToString("s", CultureInfo.InvariantCulture);

Which is not producing right value. It totally ignores offset.

Activity

  1. msmolka commented on Oct 10, 2019

    @msmolka
    Author

    Invalid

  2. ahsonkhan commented on Oct 11, 2019

    @ahsonkhan
    Contributor

    @msmolka - for the benefit of readers, can you explain what was your reasoning for closing the issue? Why is it invalid?

  3. msmolka commented on Oct 11, 2019

    @msmolka
    Author

    @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

  4. ahsonkhan commented on Oct 14, 2019

    @ahsonkhan
    Contributor

    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?

    Thanks.

  5. msmolka commented on Oct 14, 2019

    @msmolka
    Author

    @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.

  6. ahsonkhan commented on Oct 14, 2019

    @ahsonkhan
    Contributor

    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.

    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.Json package, version 5.0.0-alpha1.19513.1
    https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json

    https://github.com/dotnet/corefx/#daily-builds

  7. changed the title [-]System.Text.Json DateTimeOffset serialization and deserialization work wrongly[/-] [+]JsonString node doesn't round-trip DateTimeOffset correctly[/+] on Oct 14, 2019
  8. ahsonkhan commented on Oct 14, 2019

    @ahsonkhan
    Contributor

    Updated the title since the issue is not related to serialization/deserialization.

  9. khellang commented on Oct 14, 2019

    @khellang
    Member

    From The Sortable ("s") Format Specifier docs (emphasis mine):

    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)?

  10. watfordgnf commented on Oct 14, 2019

    @watfordgnf
    Contributor

    It looks like GUID's are correctly using D which matches Utf8Formatter.TryFormat used by Utf8JsonWriter.

  11. msmolka commented on Oct 14, 2019

    @msmolka
    Author

    @watfordgnf Be carefull with "O" format for parsing. It is very strict. JsonReader is parsing datetimeoffset in different way.

  12. watfordgnf commented on Oct 14, 2019

    @watfordgnf
    Contributor

    Fair point, but more correct than "s".

    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.

  13. Clockwork-Muse commented on Oct 15, 2019

    @Clockwork-Muse
    Contributor

    @khellang

    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).
  14. transferred this issue fromdotnet/corefxon Feb 1, 2020
  15. added this to the 5.0 milestone on Feb 1, 2020
  16. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions