Skip to content

Commit 52cb46c

Browse files
slipsetpuredanger
authored andcommitted
DJSON-54 Add custom fallback fn for writing unknown types
Signed-off-by: Alex Miller <alex.miller@cognitect.com>
1 parent ab13445 commit 52cb46c

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/main/clojure/clojure/data/json.clj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@
610610
(defn- write-generic [x out options]
611611
(if (.isArray (class x))
612612
(-write (seq x) out options)
613-
(throw (Exception. (str "Don't know how to write JSON of " (class x))))))
613+
((:default-write-fn options) x out options)))
614614

615615
(defn- write-ratio [x out options]
616616
(-write (double x) out options))
@@ -648,13 +648,17 @@
648648
;; Maybe a Java array, otherwise fail
649649
(extend java.lang.Object JSONWriter {:-write write-generic})
650650

651+
(defn- default-write-fn [x out options]
652+
(throw (Exception. (str "Don't know how to write JSON of " (class x)))))
653+
651654
(def default-write-options {:escape-unicode true
652655
:escape-js-separators true
653656
:escape-slash true
654657
:sql-date-converter default-sql-date->instant-fn
655658
:date-formatter java.time.format.DateTimeFormatter/ISO_INSTANT
656659
:key-fn default-write-key-fn
657660
:value-fn default-value-fn
661+
:default-write-fn default-write-fn
658662
:indent false
659663
:indent-depth 0 ;; internal, to track nesting depth
660664
})
@@ -713,6 +717,13 @@
713717
returns itself, the key-value pair will be omitted from the
714718
output. This option does not apply to non-map collections.
715719
720+
:default-write-fn function
721+
722+
Function to handle types which are unknown to data.json. Defaults
723+
to a function which throws an exception. Expects to be called with
724+
three args, the value to be serialized, the output stream, and the
725+
options map.
726+
716727
:indent boolean
717728
718729
If true, indent json while writing (default = false)."

src/test/clojure/clojure/data/json_test.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,13 @@
423423
(dotimes [_ 1000]
424424
(assert (= (json/read-str pass1-string)
425425
(json/read-str (json/write-str (json/read-str pass1-string)))))))))
426+
427+
(defn djson-54-default-write-fn [x out options]
428+
(#'json/write-string (str x) out options))
429+
430+
(deftest DJSON-54-test
431+
(is (thrown? Exception (json/write-str {:foo (java.net.URI. "http://clojure.org")})))
432+
(try (json/write-str {:foo (java.net.URI. "http://clojure.org")})
433+
(catch Exception e
434+
(is (= "Don't know how to write JSON of class java.net.URI" (.getMessage e)))))
435+
(is (= "{\"foo\":\"http:\\/\\/clojure.org\"}" (json/write-str {:foo (java.net.URI. "http://clojure.org")} :default-write-fn djson-54-default-write-fn))))

0 commit comments

Comments
 (0)