Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utf8JsonWriter DateTime(Offset) formatting should produce smallest output that round trips. #28412

Closed
ahsonkhan opened this issue Jan 14, 2019 · 0 comments · Fixed by dotnet/corefx#35966
Assignees
Milestone

Comments

@ahsonkhan
Copy link
Member

The Utf8JsonWriter uses Utf8Formatter with the standard format "O" to write the date time to the output.

Instead, it should use custom formatter that rounds the fractional part of the second to the nearest second to create the smallest round-trippable output. This is particularly important for trailing zero seconds which are omitted by other APIs like XmlConvert.ToString(new DateTime(2019, 1, 1, 2, 42, 42, 100), XmlDateTimeSerializationMode.RoundtripKind) which produces 2019-01-01T02:42:42.1.

Use the following implementation without writing the fractional seconds digit. In stead, if the fractional part is >= 0.5 seconds, increment value.Second before writing it to the output. Otherwise, just omit it.
https://github.com/dotnet/corefx/blob/77096755443730962810662501f82550fdb4656d/src/Common/src/CoreLib/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.O.cs#L69-L72

For more info: dotnet/corefx#34425 (comment)

cc @jkotas, @JamesNK

@ahsonkhan ahsonkhan self-assigned this Jan 14, 2019
@ahsonkhan ahsonkhan assigned layomia and unassigned ahsonkhan Feb 22, 2019
layomia referenced this issue in dotnet/corefx Apr 1, 2019
…est output that roundtrips (#35966)

* Modify Utf8JsonWriter to write DateTime(Offset) values with the smallest output that roundtrips

This change addresses https://github.com/dotnet/corefx/issues/34576.

We replace calls to `Utf8Formatter.TryParse`, which formats to the roundtrippable custom format "O", with internal formatting logic that:
- drops the fraction part of the output if the milliseconds part of the date is zero, else
- writes the fraction part up to 7 decimal places with no trailing zeros.

* Format to a stackalloc'd span and then trim it if necessary, then copy to the actual destination

* Address review feedback

* Re-merge trim implementation

* Use DivMod and correct buffer length checks
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants