The same fix for data.json compatibility we had in clojurewerkz.support.json
This commit is contained in:
parent
3657549cc0
commit
9c05ee4ac9
2 changed files with 43 additions and 19 deletions
|
|
@ -12,24 +12,56 @@
|
||||||
monger.json
|
monger.json
|
||||||
(:import org.bson.types.ObjectId))
|
(:import org.bson.types.ObjectId))
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Implementation
|
||||||
|
;;
|
||||||
|
|
||||||
|
;; copied from clojure.reducers
|
||||||
|
(defmacro ^:private compile-if
|
||||||
|
"Evaluate `exp` and if it returns logical true and doesn't error, expand to
|
||||||
|
`then`. Else expand to `else`.
|
||||||
|
|
||||||
|
(compile-if (Class/forName \"java.util.concurrent.ForkJoinTask\")
|
||||||
|
(do-cool-stuff-with-fork-join)
|
||||||
|
(fall-back-to-executor-services))"
|
||||||
|
[exp then else]
|
||||||
|
(if (try (eval exp)
|
||||||
|
(catch Throwable _ false))
|
||||||
|
`(do ~then)
|
||||||
|
`(do ~else)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; API
|
;; API
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(require 'clojurewerkz.support.json)
|
(require 'clojurewerkz.support.json)
|
||||||
|
|
||||||
(try
|
;; all this madness would not be necessary if some people cared about backwards
|
||||||
(require 'clojure.data.json)
|
;; compatiblity of the libraries they maintain. Shame on the clojure.data.json maintainer. MK.
|
||||||
(catch Throwable t
|
(compile-if (and (find-ns 'clojure.data.json)
|
||||||
false))
|
clojure.data.json/JSONWriter)
|
||||||
|
(try
|
||||||
|
(extend-protocol clojure.data.json/JSONWriter
|
||||||
|
ObjectId
|
||||||
|
(-write [^ObjectId object out]
|
||||||
|
(clojure.data.json/write (.toString object) out)))
|
||||||
|
(catch Throwable _
|
||||||
|
false))
|
||||||
|
(comment "Nothing to do, clojure.data.json is not available"))
|
||||||
|
|
||||||
|
(compile-if (and (find-ns 'clojure.data.json)
|
||||||
|
clojure.data.json/Write-JSON)
|
||||||
|
(try
|
||||||
|
(extend-protocol clojure.data.json/Write-JSON
|
||||||
|
ObjectId
|
||||||
|
(write-json [^ObjectId object out escape-unicode?]
|
||||||
|
(clojure.data.json/write-json (.toString object) out escape-unicode?)))
|
||||||
|
(catch Throwable _
|
||||||
|
false))
|
||||||
|
(comment "Nothing to do, clojure.data.json 0.1.x is not available"))
|
||||||
|
|
||||||
(try
|
|
||||||
(extend-protocol clojure.data.json/Write-JSON
|
|
||||||
ObjectId
|
|
||||||
(write-json [^ObjectId object out escape-unicode?]
|
|
||||||
(clojure.data.json/write-json (.toString object) out escape-unicode?)))
|
|
||||||
(catch Throwable _
|
|
||||||
false))
|
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(require 'cheshire.custom)
|
(require 'cheshire.custom)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
com.mongodb.DBObject)
|
com.mongodb.DBObject)
|
||||||
(:require monger.json
|
(:require monger.json
|
||||||
monger.joda-time
|
monger.joda-time
|
||||||
[clojure.data.json :as json]
|
|
||||||
[clj-time.core :as t]
|
[clj-time.core :as t]
|
||||||
[cheshire.custom :as json2]))
|
[cheshire.custom :as json2]))
|
||||||
|
|
||||||
|
|
@ -14,20 +13,13 @@
|
||||||
(deftest ^{:integration true} serialization-of-joda-datetime-to-json
|
(deftest ^{:integration true} serialization-of-joda-datetime-to-json
|
||||||
(let [dt (t/date-time 2011 10 13 23 55 0)]
|
(let [dt (t/date-time 2011 10 13 23 55 0)]
|
||||||
(is (= "\"2011-10-13T23:55:00.000Z\""
|
(is (= "\"2011-10-13T23:55:00.000Z\""
|
||||||
(json/json-str dt)
|
|
||||||
(json2/encode dt)))))
|
(json2/encode dt)))))
|
||||||
|
|
||||||
(deftest ^{:integration true} serialization-of-joda-date-to-json
|
(deftest ^{:integration true} serialization-of-joda-date-to-json
|
||||||
(let [d (.toDate (t/date-time 2011 10 13 23 55 0))]
|
(let [d (.toDate (t/date-time 2011 10 13 23 55 0))]
|
||||||
(is (= "\"2011-10-13T23:55:00.000Z\""
|
|
||||||
(json/json-str d)))
|
|
||||||
(is (= "\"2011-10-13T23:55:00Z\""
|
(is (= "\"2011-10-13T23:55:00Z\""
|
||||||
(json2/encode d)))))
|
(json2/encode d)))))
|
||||||
|
|
||||||
(deftest ^{:integration true} serialization-of-object-id-to-json-with-clojure-data-json
|
|
||||||
(is (= "\"4ec2d1a6b55634a935ea4ac8\"" (json/json-str (ObjectId. "4ec2d1a6b55634a935ea4ac8")))))
|
|
||||||
|
|
||||||
|
|
||||||
(deftest ^{:integration true} conversion-of-joda-datetime-to-db-object
|
(deftest ^{:integration true} conversion-of-joda-datetime-to-db-object
|
||||||
(let [d (to-db-object (t/date-time 2011 10 13 23 55 0))]
|
(let [d (to-db-object (t/date-time 2011 10 13 23 55 0))]
|
||||||
(is (instance? java.util.Date d))
|
(is (instance? java.util.Date d))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue