Created
May 4, 2014 19:47
-
-
Save jdaily/4c415a560ba556215e5d to your computer and use it in GitHub Desktop.
Play-ReactiveMongo DateTime implicit read/write
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jdaily | |
import play.api.libs.json._ | |
import org.joda.time.{DateTimeZone, DateTime, LocalDateTime} | |
/** | |
* Created by jdaily on 5/04/14. | |
*/ | |
object MongoDateTime { | |
implicit val localDateTimeRead: Reads[LocalDateTime] = | |
(__ \ "$date").read[Long].map { dateTime => new LocalDateTime(dateTime, DateTimeZone.UTC) } | |
implicit val localDateTimeWrite: Writes[LocalDateTime] = new Writes[LocalDateTime] { | |
def writes(dateTime: LocalDateTime): JsValue = Json.obj( | |
"$date" -> dateTime.toDateTime(DateTimeZone.UTC).getMillis | |
) | |
} | |
implicit val dateTimeRead: Reads[DateTime] = | |
(__ \ "$date").read[Long].map { dateTime => | |
new DateTime(dateTime, DateTimeZone.UTC) | |
} | |
implicit val dateTimeWrite: Writes[DateTime] = new Writes[DateTime] { | |
def writes(dateTime: DateTime): JsValue = Json.obj( | |
"$date" -> dateTime.getMillis | |
) | |
} | |
implicit val dateTimeFormats = Format(dateTimeRead, dateTimeWrite) | |
implicit val localDateTimeFormats = Format(localDateTimeRead, localDateTimeWrite) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment