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
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.
…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
The
Utf8JsonWriter
usesUtf8Formatter
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 produces2019-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
The text was updated successfully, but these errors were encountered: