Skip to content

Option to not truncate datetime when serializing using System.Text.Json #36937

Description

@joshlangs

Original issue is here with more context: Azure/azure-cosmos-dotnet-v3#1468

System.Text.Json docs here: https://docs.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#the-extended-iso-8601-12019-profile-in-systemtextjson

Basically, System.Text.Json will truncate a serialized DateTime to avoid writing extra zeroes.

2020-05-02T18:42:34.9787000Z
would become:
2020-05-02T18:42:34.9787Z

The specific problem is with Cosmos. I want to be able to write queries with order by SomeDate. But I can't with default serialization, because unless the same number of decimals are outputted, it's not sortable.

We've already worked around the problem by introducing a custom Cosmos serializer.

But I'll submit it for consideration here, too. If it's a no-go, no problem :)

One potential suggestion would be a serializer option such as bool SkipDateTimeCompression or bool AlwaysOutput7FractionalSecondsDigitsPlease.

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on May 24, 2020
  2. layomia commented on May 28, 2020

    @layomia
    Contributor

    This is achievable with a custom converter as described in the doc linked above. Built-in extensibility for writing/parsing DateTime/DateTimeOffset is not on the roadmap for System.Text.Json.

  3. removed
    untriagedNew issue has not been triaged by the area owner
    on May 28, 2020
  4. added this to the 5.0 milestone on May 28, 2020
  5. ghost locked as resolved and limited conversation to collaborators on Dec 9, 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

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions