Fixes #128 - robust serialization for java.time.Instant

This commit is contained in:
Chris Nuernberger 2020-09-30 11:06:44 -06:00
parent d0ad2884a7
commit b1471ba87f
2 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,23 @@
(ns taoensso.nippy.java-time
(:require [taoensso.nippy :as nippy])
(:import [java.time Instant]))
(nippy/extend-freeze
Instant :java.time/instant
[^Instant instant out]
(nippy/-freeze-without-meta! [(.getEpochSecond instant)
(long (.getNano instant))] out))
(nippy/extend-thaw
:java.time/instant
[in]
(let [[seconds nanos] (nippy/thaw-from-in! in)]
(Instant/ofEpochSecond seconds nanos)))
(comment
(let [inst (Instant/now)]
(= inst (nippy/thaw (nippy/freeze inst))))
)

View file

@ -6,6 +6,8 @@
[clojure.test.check.properties :as tc-props]
[taoensso.encore :as enc :refer ()]
[taoensso.nippy :as nippy :refer (freeze thaw)]
;;java-time support for nippy
[taoensso.nippy.java-time]
[taoensso.nippy.benchmarks :as benchmarks]))
(comment (test/run-tests))
@ -333,6 +335,11 @@
"Metadata successfully excluded by freeze"))
(deftest java-time-extension-test
(let [inst (java.time.Instant/now)]
(is (= inst (nippy/thaw (nippy/freeze inst))))))
;;;; Benchmarks
(deftest _benchmarks