From b1471ba87f299347e78a9e91b002df2971fd93b1 Mon Sep 17 00:00:00 2001 From: Chris Nuernberger Date: Wed, 30 Sep 2020 11:06:44 -0600 Subject: [PATCH] Fixes #128 - robust serialization for java.time.Instant --- src/taoensso/nippy/java_time.clj | 23 +++++++++++++++++++++++ test/taoensso/nippy/tests/main.clj | 7 +++++++ 2 files changed, 30 insertions(+) create mode 100644 src/taoensso/nippy/java_time.clj diff --git a/src/taoensso/nippy/java_time.clj b/src/taoensso/nippy/java_time.clj new file mode 100644 index 0000000..c07b4f6 --- /dev/null +++ b/src/taoensso/nippy/java_time.clj @@ -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)))) + ) diff --git a/test/taoensso/nippy/tests/main.clj b/test/taoensso/nippy/tests/main.clj index 7ac88ba..89be9b0 100644 --- a/test/taoensso/nippy/tests/main.clj +++ b/test/taoensso/nippy/tests/main.clj @@ -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