To serialize joda.time.DateTime to JSON, use ISO date time format
This commit is contained in:
parent
f94bc0abc3
commit
7d79866eff
2 changed files with 32 additions and 6 deletions
|
|
@ -1,5 +1,6 @@
|
|||
(ns monger.joda-time
|
||||
(:import (org.joda.time DateTime DateTimeZone))
|
||||
(:import [org.joda.time DateTime DateTimeZone ReadableInstant]
|
||||
[org.joda.time.format ISODateTimeFormat])
|
||||
(:use [monger.conversion])
|
||||
(:require [clojure.data.json :as json]))
|
||||
|
||||
|
|
@ -9,7 +10,7 @@
|
|||
|
||||
(extend-protocol ConvertToDBObject
|
||||
org.joda.time.DateTime
|
||||
(to-db-object [^org.joda.time.DateTime input]
|
||||
(to-db-object [^DateTime input]
|
||||
(to-db-object (.toDate input))))
|
||||
|
||||
(extend-protocol ConvertFromDBObject
|
||||
|
|
@ -20,7 +21,5 @@
|
|||
|
||||
(extend-protocol json/Write-JSON
|
||||
org.joda.time.DateTime
|
||||
(write-json [^org.joda.time.DateTime object out escape-unicode?]
|
||||
;; TODO: use .printTo(Writer) here instead of ignoring
|
||||
;; out parameter. MK.
|
||||
(.print (org.joda.time.format.ISODateTimeFormat/dateTime) object)))
|
||||
(write-json [^DateTime object out escape-unicode?]
|
||||
(json/write-json (.print (ISODateTimeFormat/dateTime) ^ReadableInstant object) out escape-unicode?)))
|
||||
|
|
|
|||
27
test/monger/test/lib_integration.clj
Normal file
27
test/monger/test/lib_integration.clj
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(ns monger.test.lib-integration
|
||||
(:use [clojure.test]
|
||||
[monger.json]
|
||||
[monger.joda-time]
|
||||
[monger.conversion])
|
||||
(:import [org.joda.time ReadableInstant]
|
||||
[org.joda.time.format ISODateTimeFormat]
|
||||
[java.io StringWriter PrintWriter])
|
||||
(:require [clojure.data.json :as json]
|
||||
[clj-time.core :as t]))
|
||||
|
||||
|
||||
(deftest serialization-of-joda-datetime-to-json
|
||||
(is (= "\"2011-10-13T23:55:00.000Z\"" (json/json-str (t/date-time 2011 10 13 23 55 0)))))
|
||||
|
||||
|
||||
(deftest 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-java-util-date-to-joda-datetime
|
||||
(let [input (.toDate (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 output)))))
|
||||
Loading…
Reference in a new issue