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

Default date serialization drops trailing zeros in the microseconds date portion. #1511

Closed
avvi00 opened this issue Nov 22, 2017 · 5 comments

Comments

@avvi00
Copy link

avvi00 commented Nov 22, 2017

Hello,

I picked this problem up when I was parsing a date serialized by newtonsoft in another system.

I have a date from my source (.net system) as follows:

2017-11-21 05:55:27.064100

When this date is serialized by newtonsoft it comes out like this:

{"DateCreated":"2017-11-21T05:55:27.0641"}

Note the trailing zeros were truncated. This breaks deserialization which is expecting microseconds in my java app using this format specifier: "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX"

I expect date to be serialized like this:
{"DateCreated":"2017-11-21T05:55:27.064100"}

I have attached code to reproduce.
newtonsoft-date-issue.zip

Thanks,

Avner

@tmenier
Copy link

tmenier commented Nov 29, 2017

I just discovered this myself. Truncating those 0's also compromises sortability of the default date format.

I understand this is just a default and it is easy to override, but does anyone have any historical perspective on how this decision was reached? (I wanted to mimic Json.NET's default date serialization behavior in a library of my own, assuming it represents best practices, but now I'm not so sure.)

@JamesNK
Copy link
Owner

JamesNK commented Jan 1, 2018

There is no way to know how many zeros a deserialized date has so it can't be re-serialized automatically in the exact same format.

Modify it here if you want to be explicit - https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_DateFormatString.htm

@JamesNK JamesNK closed this as completed Jan 1, 2018
@Eli-Black-Work
Copy link

It looks like this is because Newtonsoft is using "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK" instead of "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK". Using fffffff will keep the digits, even if they're 0, but FFFFFFF won't 🙂

See https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#FFFFFF_Specifier

@Eli-Black-Work
Copy link

I've filed a new issue for this: #2780

@Eli-Black-Work
Copy link

After some thought, I think that the current default is probably best, and if we need datetimes that are sortable/comparable, we should override the time format string. See #2780 (comment) for more details 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants