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

Support Java8 dates oob with EDN & Transit too #92

Open
ikitommi opened this issue Nov 30, 2018 · 5 comments
Open

Support Java8 dates oob with EDN & Transit too #92

ikitommi opened this issue Nov 30, 2018 · 5 comments

Comments

@ikitommi
Copy link
Member

ikitommi commented Nov 30, 2018

Muuntaja depends on Java8. Java8 has java.time package with the de facto new Classes for different date & time representations. Muuntaja should support serialization & deserialization of the classes for all default formats: JSON, EDN and Transit. Jsonista already has support, EDN and Transit should be verified & implemented.

relevant discussion: https://clojureverse.org/t/migrating-from-clj-time-to-java-time/3251/6

@miikka miikka changed the title Support Java8 datas oob with EDN & Transit too Support Java8 dats oob with EDN & Transit too Nov 30, 2018
@miikka miikka changed the title Support Java8 dats oob with EDN & Transit too Support Java8 dates oob with EDN & Transit too Nov 30, 2018
@kanwei
Copy link

kanwei commented Jan 8, 2019

I'd like to help with this, but it's unclear where it would best go. Is the right approach to extend a protocol with the JDK8 datetime classes?

@kanwei
Copy link

kanwei commented Jan 8, 2019

I think this is the general gist of it:

(def write-handler {java.time.OffsetDateTime  (cognitect.transit/write-handler "java.time.OffsetDateTime" (fn [dt] (str dt)))
                    java.time.Instant         (cognitect.transit/write-handler "java.time.Instant" (fn [dt] (str dt)))})

(cognitect.transit/write (cognitect.transit/writer out :json {:handlers write-handler})
                         (java-time/offset-date-time))

I guess it's not too hard to implement - the real question is what tagged value names are appropriate?

@miikka
Copy link
Contributor

miikka commented Jan 9, 2019

I think that we should use the default tags where possible: in EDN, java.time.Instant should be tagged as #inst and in Transit it should use one of the time tags (m/t). I suppose neither has a suitable default representation for OffsetDateTime.

@holyjak
Copy link

holyjak commented Apr 6, 2020

FYI This is what I did for Instant:

(def transit-write-handlers
  {java.time.Instant
   (cognitect.transit/write-handler
     "t"
     (fn [^java.time.Instant inst]
       (.format
         (com.cognitect.transit.impl.AbstractParser/getDateTimeFormat)
         (java.util.Date/from inst))))})

; => send `{:handlers transit-write-handlers}` to `writer`

it is a hack because it relies on implementation details of transit-java but it guarantees that any existing client will be able to read it as a Date.

@deobald
Copy link

deobald commented Mar 30, 2021

In the interest of helping others who stumble on the workarounds described in this thread, here's a concrete implementation wired into reitit:

  1. Muuntaaja instance with custom writer
  2. Configuring a reitit router with that instance

I've used @holyjak 's hack because the default toString from java.time.Instant provides ISO_INSTANT formatting, which can differ from Transit's formatter. The latter only contains 3-digit sub-second precision. Folks who've moved to java.time probably don't care anyway, but it's possible this will save some parsing confusion.

This blog post is old, but was handy for context since I'm unfamiliar with all these libraries: https://increasinglyfunctional.com/2014/09/02/custom-transit-writers-clojure-joda-time.html

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

No branches or pull requests

5 participants