Skip to content

Commit

Permalink
Add JSON serialization of BSONTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
cymantic authored and michaelklishin committed May 17, 2015
1 parent 3e71b6f commit 602b6e3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions project.clj
Expand Up @@ -39,6 +39,7 @@
:dev {:resource-paths ["test/resources"]
:dependencies [[clj-time "0.8.0" :exclusions [org.clojure/clojure]]
[cheshire "5.3.1" :exclusions [org.clojure/clojure]]
[org.clojure/data.json "0.2.5" :exclusions [org.clojure/clojure]]
[org.clojure/tools.cli "0.3.1" :exclusions [org.clojure/clojure]]
[org.clojure/core.cache "0.6.3" :exclusions [org.clojure/clojure]]
[ring/ring-core "1.3.0" :exclusions [org.clojure/clojure]]
Expand Down
12 changes: 11 additions & 1 deletion src/clojure/monger/json.clj
Expand Up @@ -10,7 +10,8 @@
(ns monger.json
"Provides clojure.data.json/Write-JSON protocol extension for MongoDB-specific types, such as
org.bson.types.ObjectId"
(:import org.bson.types.ObjectId))
(:import org.bson.types.ObjectId
org.bson.types.BSONTimestamp))

;;
;; Implementation
Expand Down Expand Up @@ -47,6 +48,12 @@
ObjectId
(-write [^ObjectId object out]
(clojure.data.json/write (.toString object) out)))

(extend-protocol clojure.data.json/JSONWriter
BSONTimestamp
(-write [^BSONTimestamp object out]
(clojure.data.json/write {:time (.getTime object) :inc (.getInc object)} out)))

(catch Throwable _
false))
(comment "Nothing to do, clojure.data.json is not available"))
Expand All @@ -72,5 +79,8 @@
(cheshire.generate/add-encoder ObjectId
(fn [^ObjectId oid ^com.fasterxml.jackson.core.json.WriterBasedJsonGenerator generator]
(.writeString generator (.toString oid))))
(cheshire.generate/add-encoder BSONTimestamp
(fn [^BSONTimestamp ts ^com.fasterxml.jackson.core.json.WriterBasedJsonGenerator generator]
(cheshire.generate/encode-map {:time (.getTime ts) :inc (.getInc ts)} generator)))
(catch Throwable t
false))
16 changes: 16 additions & 0 deletions test/monger/test/json_cheshire_test.clj
@@ -0,0 +1,16 @@
(ns monger.test.json-cheshire-test
(:require [clojure.test :refer :all]
[monger.json]
[cheshire.core :refer :all])
(:import org.bson.types.ObjectId
org.bson.types.BSONTimestamp))

(deftest convert-dbobject-to-json
(let [input (ObjectId.)
output (generate-string input)]
(is (= (str "\"" input "\"") output))))

(deftest convert-bson-timestamp-to-json
(let [input (BSONTimestamp. 123 4)
output (generate-string input)]
(is (= "{\"time\":123,\"inc\":4}" output))))
16 changes: 16 additions & 0 deletions test/monger/test/json_test.clj
@@ -0,0 +1,16 @@
(ns monger.test.json-test
(:require [clojure.test :refer :all]
[monger.json]
[clojure.data.json :as json])
(:import org.bson.types.ObjectId
org.bson.types.BSONTimestamp))

(deftest convert-dbobject-to-json
(let [input (ObjectId.)
output (json/write-str input)]
(is (= (str "\"" input "\"") output))))

(deftest convert-bson-timestamp-to-json
(let [input (BSONTimestamp. 123 4)
output (json/write-str input)]
(is (= "{\"time\":123,\"inc\":4}" output))))

0 comments on commit 602b6e3

Please sign in to comment.