Cheshire support for monger.json and monger.joda-time
This commit is contained in:
parent
23a55420df
commit
33b8f54460
5 changed files with 59 additions and 24 deletions
10
ChangeLog.md
10
ChangeLog.md
|
|
@ -1,6 +1,14 @@
|
|||
## Changes between 1.2.0 and 1.3.0
|
||||
|
||||
No changes yet.
|
||||
### Cheshire Support
|
||||
|
||||
`monger.json` and `monger.joda-time` will now use [Cheshire](https://github.com/dakrone/cheshire) if it is available. [clojure.data.json](https://github.com/clojure/data.json)
|
||||
is no longer a hard dependency (but still supported if available).
|
||||
|
||||
|
||||
### ClojureWerkz Support 0.7.0
|
||||
|
||||
ClojureWerkz Support dependency has been updated to version `0.7.0`.
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
:dependencies [[org.clojure/clojure "1.4.0"]
|
||||
[org.mongodb/mongo-java-driver "2.9.0"]
|
||||
[com.novemberain/validateur "1.2.0"]
|
||||
[clojurewerkz/support "0.6.0"]
|
||||
[clojurewerkz/support "0.7.0"]
|
||||
[ragtime/ragtime.core "0.2.0"]]
|
||||
:test-selectors {:default (fn [m]
|
||||
(and (not (:performance m))
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
:cache :cache
|
||||
:gridfs :gridfs
|
||||
:command :command
|
||||
:integration :integration
|
||||
:performance :performance
|
||||
;; as in, edge mongodb server
|
||||
:edge-features :edge-features
|
||||
|
|
@ -41,6 +42,7 @@
|
|||
:dev {:resource-paths ["test/resources"]
|
||||
:dependencies [[clj-time "0.4.2" :exclusions [org.clojure/clojure]]
|
||||
[org.clojure/data.json "0.1.2" :exclusions [org.clojure/clojure]]
|
||||
[cheshire "4.0.2" :exclusions [org.clojure/clojure]]
|
||||
[org.clojure/tools.cli "0.2.1" :exclusions [org.clojure/clojure]]
|
||||
[org.clojure/core.cache "0.6.0" :exclusions [org.clojure/clojure]]
|
||||
[ring/ring-core "1.1.0"]]
|
||||
|
|
|
|||
|
|
@ -60,10 +60,4 @@
|
|||
;; JSON serialization
|
||||
;;
|
||||
|
||||
(try
|
||||
;; try to load clojure.data.json. If available, load CLJW Support
|
||||
;; extensions.
|
||||
(require 'clojure.data.json)
|
||||
(require 'clojurewerkz.support.json)
|
||||
(catch Throwable _
|
||||
false))
|
||||
(require 'clojurewerkz.support.json)
|
||||
|
|
|
|||
|
|
@ -10,15 +10,35 @@
|
|||
(ns ^{:doc "Provides clojure.data.json/Write-JSON protocol extension for MongoDB-specific types, such as
|
||||
org.bson.types.ObjectId"}
|
||||
monger.json
|
||||
(:import org.bson.types.ObjectId)
|
||||
(:require [clojure.data.json :as json]
|
||||
clojurewerkz.support.json))
|
||||
(:import org.bson.types.ObjectId))
|
||||
|
||||
;;
|
||||
;; API
|
||||
;;
|
||||
|
||||
(extend-protocol json/Write-JSON
|
||||
ObjectId
|
||||
(write-json [^ObjectId object out escape-unicode?]
|
||||
(json/write-json (.toString object) out escape-unicode?)))
|
||||
(require 'clojurewerkz.support.json)
|
||||
|
||||
(try
|
||||
(require 'clojure.data.json)
|
||||
(catch Throwable t
|
||||
false))
|
||||
|
||||
(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
|
||||
(require 'cheshire.custom)
|
||||
(catch Throwable t
|
||||
false))
|
||||
|
||||
(try
|
||||
(cheshire.custom/add-encoder ObjectId
|
||||
(fn [^ObjectId oid ^com.fasterxml.jackson.core.json.WriterBasedJsonGenerator generator]
|
||||
(.writeString generator (.toString oid))))
|
||||
(catch Throwable t
|
||||
false))
|
||||
|
|
|
|||
|
|
@ -7,36 +7,47 @@
|
|||
(:require monger.json
|
||||
monger.joda-time
|
||||
[clojure.data.json :as json]
|
||||
[clj-time.core :as t]))
|
||||
[clj-time.core :as t]
|
||||
[cheshire.custom :as json2]))
|
||||
|
||||
|
||||
(deftest serialization-of-joda-datetime-to-json-with-clojure-data-json
|
||||
(is (= "\"2011-10-13T23:55:00.000Z\"" (json/json-str (t/date-time 2011 10 13 23 55 0)))))
|
||||
(deftest ^{:integration true} serialization-of-joda-datetime-to-json
|
||||
(let [dt (t/date-time 2011 10 13 23 55 0)]
|
||||
(is (= "\"2011-10-13T23:55:00.000Z\""
|
||||
(json/json-str dt)
|
||||
(json2/encode dt)))))
|
||||
|
||||
(deftest serialization-of-object-id-to-json-with-clojure-data-json
|
||||
(deftest ^{:integration true} serialization-of-joda-date-to-json
|
||||
(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\""
|
||||
(json2/encode d)))))
|
||||
|
||||
(deftest ^{:integration true} serialization-of-object-id-to-json-with-clojure-data-json
|
||||
(is (= "\"4ec2d1a6b55634a935ea4ac8\"" (json/json-str (ObjectId. "4ec2d1a6b55634a935ea4ac8")))))
|
||||
|
||||
|
||||
(deftest 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))]
|
||||
(is (instance? java.util.Date d))
|
||||
(is (= 1318550100000 (.getTime ^java.util.Date d)))))
|
||||
|
||||
|
||||
(deftest conversion-of-joda-datemidnight-to-db-object
|
||||
(deftest ^{:integration true} conversion-of-joda-datemidnight-to-db-object
|
||||
(let [d (to-db-object (DateMidnight. (t/date-time 2011 10 13)))]
|
||||
(is (instance? java.util.Date d))
|
||||
(is (= 1318464000000 (.getTime ^java.util.Date d)))))
|
||||
|
||||
|
||||
(deftest conversion-of-java-util-date-to-joda-datetime
|
||||
(deftest ^{:integration true} conversion-of-java-util-date-to-joda-datetime
|
||||
(let [input (.toDate ^DateTime (t/date-time 2011 10 13 23 55 0))
|
||||
output (from-db-object input false)]
|
||||
(is (instance? org.joda.time.DateTime output))
|
||||
(is (= input (.toDate ^DateTime output)))))
|
||||
|
||||
|
||||
(deftest test-reader-extensions
|
||||
(deftest ^{:integration true} test-reader-extensions
|
||||
(let [^DateTime d (t/date-time 2011 10 13 23 55 0)]
|
||||
(binding [*print-dup* true]
|
||||
(pr-str d))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue