cljs-time icon indicating copy to clipboard operation
cljs-time copied to clipboard

Add transit serialisers

Open andrewmcveigh opened this issue 11 years ago • 11 comments

andrewmcveigh avatar Nov 11 '14 22:11 andrewmcveigh

This might be a starting point: https://gist.github.com/Deraen/eb3f650c472fb1abe970#file-transit-cljc

danielcompton avatar Sep 03 '15 21:09 danielcompton

(def transit-verbose-format (ftime/formatter "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))

(deftype VerboseDateHandler []
  Object
  (tag [_ v] "t")
  (rep [_ v] (str (ftime/unparse transit-verbose-format (to-utc v)) "Z"))
  (stringRep [h v] (.rep h v)))

(deftype DateHandler []
  Object
  (tag [_ v] "m")
  (rep [_ v] (.valueOf v))
  (stringRep [h v] (str (.rep h v)))
  (getVerboseHandler [_ v] (VerboseDateHandler.)))

(def date-read-handlers
  {"m" #(ms->date (if (number? %) % (js/parseInt %)))
   "t" #(to-local (ftime/parse transit-verbose-format %))})

(def date-write-handlers
  {UtcDateTime (DateHandler.)
   DateTime (DateHandler.)})

pangloss avatar Sep 04 '15 01:09 pangloss

; (In cljs) :date-time is faster than using :basic-date-time ; good.date.UtcDateTime.fromIsoString is MUCH faster than either ; For a random data set, transit/read: ; :basic-date-time 1.24sec ; :date-time 700ms ; good.date.UtcDateTime.fromIsoString 70ms

https://gist.github.com/Deraen/eb3f650c472fb1abe970#file-transit-cljc-L8-L13

It might be better to go with native goog.date functions for performance here.

danielcompton avatar Sep 04 '15 01:09 danielcompton

Agreed that the goog.date native parsers are better. The code I pasted works well but I haven't tried to performance optimize it.

pangloss avatar Sep 04 '15 01:09 pangloss

Thanks for this.

I guess I've never needed these, so I've been a bit lazy about getting around to it.

andrewmcveigh avatar Sep 09 '15 08:09 andrewmcveigh

I've worked on this recently, so I'll put a PR together once I'm happy with our changes.

danielcompton avatar Sep 09 '15 08:09 danielcompton

Cool, appreciated! :smiley:

andrewmcveigh avatar Sep 09 '15 09:09 andrewmcveigh

@danielcompton any chance you have a PR ready? :smile:

stuarth avatar Jan 21 '16 14:01 stuarth

Hey, I don't have the headspace/time to put together a proper PR for this, but here's the code that we're using:

Clojure:

(ns my.ns
  (:require [cognitect.transit :as transit]])
  (:import [org.joda.time DateTime ReadableInstant]))

(def transit-writers {:handlers {DateTime (transit/write-handler
                                            (constantly "m")
                                            (fn [v] (-> ^ReadableInstant v .getMillis))
                                            (fn [v] (-> ^ReadableInstant v .getMillis .toString)))}})

(def transit-readers {:handlers {"m" (transit/read-handler
                                       (fn [s] (DateTime. (Long/parseLong s))))}})

ClojureScript

(ns my.ns
  (:require [cognitect.transit :as transit])
  (:import [goog.date UtcDateTime]))

(def transit-readers
  {:handlers
   {"m" (transit/read-handler (fn [s] (UtcDateTime.fromTimestamp s)))}})


(def transit-writers
  {:handlers
   {UtcDateTime (transit/write-handler
                  (constantly "m")
                  (fn [v] (.getTime v))
                  (fn [v] (str (.getTime v))))}})

(def packer (sente-transit/->TransitPacker :json transit-writers transit-readers))

Happy for someone to take this and run with it. Some credit also goes to http://increasinglyfunctional.com/2014/09/02/custom-transit-writers-clojure-joda-time/

danielcompton avatar Jan 21 '16 20:01 danielcompton

Thansk, @danielcompton. Any idea how to make timestamps work with sort-by,(sort-by :some/time coll)?

theronic avatar Nov 08 '17 10:11 theronic

I would expect it to work fine after requiring cljs-time.extend?

danielcompton avatar Nov 08 '17 21:11 danielcompton