From 043b37211d415bc807d586d18501f5f1e723fd24 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Fri, 6 Nov 2020 15:32:36 +0100 Subject: [PATCH] Add native support for `java.time.Period` --- src/taoensso/nippy.clj | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 760f473..d9b9ba0 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -212,6 +212,7 @@ 79 :time-instant ; JVM 8+ 83 :time-duration ; '' + 84 :time-period ; '' ;;; DEPRECATED (only support thawing) 5 :reader-lg2 ; == :reader-lg, used only for back-compatible thawing @@ -1205,6 +1206,13 @@ (.writeInt out (.getNano x))) nil) +(enc/compile-if java.time.Period + (id-freezer java.time.Period id-time-period + (.writeInt out (.getYears x)) + (.writeInt out (.getMonths x)) + (.writeInt out (.getDays x))) + nil) + (freezer Object (when-debug (println (str "freeze-fallback: " (type x)))) (if-let [ff *freeze-fallback*] @@ -1707,6 +1715,20 @@ :class-name "java.time.Duration" :content {:seconds secs :nanos nanos}}})) + id-time-period + (let [years (.readInt in) + months (.readInt in) + days (.readInt in)] + + (enc/compile-if java.time.Period + (java.time.Period/of years months days) + {:nippy/unthawable + {:type :class + :cause :class-not-found + + :class-name "java.time.Period" + :content {:years years :months months :days days}}})) + ;; Deprecated ------------------------------------------------------ id-boolean-depr1 (.readBoolean in) id-sorted-map-depr1 (read-kvs-depr1 (sorted-map) in) @@ -2032,15 +2054,10 @@ :uuid (java.util.UUID/randomUUID) :date (java.util.Date.) - :time-instant ; JVM 8+ - (enc/compile-if java.time.Instant - (java.time.Instant/now) - nil) - - :time-duration ; JVM 8+ - (enc/compile-if java.time.Duration - (java.time.Duration/ofSeconds 100 100) - nil) + ;;; JVM 8+ + :time-instant (enc/compile-if java.time.Instant (java.time.Instant/now) nil) + :time-duration (enc/compile-if java.time.Duration (java.time.Duration/ofSeconds 100 100) nil) + :time-period (enc/compile-if java.time.Period (java.time.Period/of 1 1 1) nil) :objects (object-array [1 "two" {:data "data"}])