Fixes #128 - robust serialization for java.time.Instant
This commit is contained in:
parent
d0ad2884a7
commit
b1471ba87f
2 changed files with 30 additions and 0 deletions
23
src/taoensso/nippy/java_time.clj
Normal file
23
src/taoensso/nippy/java_time.clj
Normal 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))))
|
||||||
|
)
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
[clojure.test.check.properties :as tc-props]
|
[clojure.test.check.properties :as tc-props]
|
||||||
[taoensso.encore :as enc :refer ()]
|
[taoensso.encore :as enc :refer ()]
|
||||||
[taoensso.nippy :as nippy :refer (freeze thaw)]
|
[taoensso.nippy :as nippy :refer (freeze thaw)]
|
||||||
|
;;java-time support for nippy
|
||||||
|
[taoensso.nippy.java-time]
|
||||||
[taoensso.nippy.benchmarks :as benchmarks]))
|
[taoensso.nippy.benchmarks :as benchmarks]))
|
||||||
|
|
||||||
(comment (test/run-tests))
|
(comment (test/run-tests))
|
||||||
|
|
@ -333,6 +335,11 @@
|
||||||
|
|
||||||
"Metadata successfully excluded by freeze"))
|
"Metadata successfully excluded by freeze"))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest java-time-extension-test
|
||||||
|
(let [inst (java.time.Instant/now)]
|
||||||
|
(is (= inst (nippy/thaw (nippy/freeze inst))))))
|
||||||
|
|
||||||
;;;; Benchmarks
|
;;;; Benchmarks
|
||||||
|
|
||||||
(deftest _benchmarks
|
(deftest _benchmarks
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue