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 encoder doesn't preserve milliseconds #43

Open
BrunoBonacci opened this issue Jan 12, 2021 · 2 comments
Open

Default Date encoder doesn't preserve milliseconds #43

BrunoBonacci opened this issue Jan 12, 2021 · 2 comments

Comments

@BrunoBonacci
Copy link

BrunoBonacci commented Jan 12, 2021

Hi,

I've noticed that the default encoder for dates doesn't preserve the milliseconds precision

  ;; milliseconds are truncated
  (json/write-value-as-string {:date (java.util.Date.)})
  ;;=> "{\"date\":\"2021-01-12T10:19:44Z\"}"

  (json/write-value-as-string {:date (java.util.Date.)}
    (json/object-mapper {:date-format "yyyy-MM-dd'T'HH:mm:ss.SSSX"}))
  ;;=> "{\"date\":\"2021-01-12T10:19:44.596Z\"}"

Is this a deliberate decision? wouldn't be better for the default encoder to preserve data as much as possible rather than truncating it?

@ikitommi
Copy link
Member

ikitommi commented Jan 12, 2021

Can't recall why, but some questions:

  • what does Jackson do to dates by default?
  • --"-- with java.time things?
  • what happens with js?
  • what do the standards say about this?

@BrunoBonacci
Copy link
Author

BrunoBonacci commented Jan 12, 2021

The standard ISO 8601 (https://en.wikipedia.org/wiki/ISO_8601) doesn't define a precise format, hence the trouble.
It defines guidelines and a bunch of possibilities and then it is up to the implementor parties to pick and choose.
It doesn't even decide the decimal marker.

According to DZone Jackson default behaviour is to serialize dates as millisecond from epoch.

Interesting, java.time.Instant renders with 6 decimals (microseconds)

  (json/write-value-as-string {:date (java.time.Instant/now)})
  ;;=> "{\"date\":\"2021-01-12T14:53:15.658653Z\"}"

I have no idea what JS default behaviour is.

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

2 participants