Skip to content

Commit

Permalink
Merge pull request #84 from metosin/clojure12
Browse files Browse the repository at this point in the history
fix delay serialization on clojure 1.12
  • Loading branch information
opqdonut authored Nov 1, 2024
2 parents 28e5e3d + 6245c6e commit 94a4721
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
9 changes: 4 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
:dependencies [[com.fasterxml.jackson.core/jackson-core "2.17.2"]
[com.fasterxml.jackson.core/jackson-databind "2.17.2"]
[com.fasterxml.jackson.datatype/jackson-datatype-jsr310 "2.17.2"]]
:profiles {:provided {:dependencies [[org.clojure/clojure "1.11.3"]]}
:dev {:dependencies [[org.clojure/clojure "1.11.3"]
:profiles {:provided {:dependencies [[org.clojure/clojure "1.12.0"]]}
:dev {:dependencies [[org.clojure/clojure "1.12.0"]
[jmh-clojure/jmh-clojure "0.4.1"]
[com.fasterxml.jackson.datatype/jackson-datatype-joda "2.17.2"]
[cheshire "5.13.0"]
Expand All @@ -34,12 +34,11 @@
[com.clojure-goes-fast/clj-async-profiler "1.2.2"]
[criterium "0.4.6"]]
:global-vars {*warn-on-reflection* true}}
:1.8 {:dependencies [[org.clojure/clojure "1.11.3"]]}
:1.9 {:dependencies [[org.clojure/clojure "1.11.3"]]}
:1.11 {:dependencies [[org.clojure/clojure "1.11.3"]]}
:jmh {:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}
:perf {:jvm-opts ^:replace ["-server"
"-Xmx4096m"
"-Dclojure.compiler.direct-linking=true"]}}
:aliases {"all" ["with-profile" "default:dev:default:dev,1.8:dev,1.9"]
:aliases {"all" ["with-profile" "default:dev:default:dev,1.11"]
"perf" ["with-profile" "default,dev,perf"]
"repl" ["with-profile" "default,dev" "repl"]})
4 changes: 3 additions & 1 deletion src/clj/jsonista/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
(:import
(jsonista.jackson
DateSerializer
DelaySerializer
FunctionalKeyDeserializer
FunctionalSerializer
KeywordSerializer
Expand All @@ -70,7 +71,7 @@
(java.net URL)
(com.fasterxml.jackson.datatype.jsr310 JavaTimeModule)
(java.util Iterator List Map Date)
(clojure.lang Keyword Ratio Symbol)))
(clojure.lang Delay Keyword Ratio Symbol)))

(defn- ^Module clojure-module
"Create a Jackson Databind module to support Clojure datastructures.
Expand All @@ -87,6 +88,7 @@
(.addSerializer Date (if date-format
(DateSerializer. date-format)
(DateSerializer.)))
(.addSerializer Delay (DelaySerializer.))
(as-> module
(doseq [[type encoder] encoders]
(cond
Expand Down
24 changes: 24 additions & 0 deletions src/java/jsonista/jackson/DelaySerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jsonista.jackson;

import clojure.lang.Delay;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/** Serializer that mimics the behaviour we had prior to Clojure 1.12.
The Jackson default serializer now barfs on the Delay objects after Clojure 1.12. */
public class DelaySerializer extends StdSerializer<Delay> {

public DelaySerializer() {
super(DateSerializer.class, true);
}

@Override
public void serialize(Delay value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeBooleanField("realized", value.isRealized());
gen.writeEndObject();
}
}
12 changes: 12 additions & 0 deletions test/jsonista/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,15 @@
(j/write-values-as-array file eduction)
(is (= expected (slurp file)))
(.delete file)))

;; clojure 1.12 seems to have changed delay so that jackson barfs on it
;; this test documents the old behaviour that we preserve with our custom DelaySerializer
(deftest test-delay
(let [d (delay 1)
d2 (delay (list :a 1))]
(is (= "{\"realized\":false}" (j/write-value-as-string d)))
(is (= "{\"realized\":false}" (j/write-value-as-string d2)))
(force d)
(force d2)
(is (= "{\"realized\":true}" (j/write-value-as-string d)))
(is (= "{\"realized\":true}" (j/write-value-as-string d2)))))

0 comments on commit 94a4721

Please sign in to comment.