Skip to content

Json serialization format for Datetime #556

Closed

Description

@iSazonov
var a= new DateTime(2019,12,5,1,2,3,44);
a.ToString("O");

// Output: 2019-12-05T01:02:03.0440000

System.Text.Json.JsonSerializer.Serialize(a, typeof(DateTime));

// Output "2019-12-05T01:02:03.044"

Should in second case "O" format be used with "fffffff"?

Activity

  1. Clockwork-Muse commented on Dec 5, 2019

    @Clockwork-Muse
    Contributor

    Without having looked at the code, it's probably truncating to the last non-zero digit (or to the nearest digit triple - millis, micros, etc). Since the amount is/should be exact, there's no information lost here.

  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Dec 5, 2019
  3. added this to the 5.0 milestone on Dec 5, 2019
  4. layomia commented on Dec 5, 2019

    @layomia
    Contributor

    The underlying Utf8JsonWriter writes DateTime and DateTimeOffset instances according to the extended ISO 8601 profile (not roundtrippable "O", which is too strict on deserialization). Trailing second fraction zeros are omitted, so the writer produces the smallest output required to roundtrip. See some test data showing how the writer trims.

    I've filed an issue to add this information to the official docs: dotnet/docs#16128.

  5. ghost locked as resolved and limited conversation to collaborators on Dec 11, 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