I'm developing a Play application, and I'm trying to use a Joda DateTime object into my case class.
package model
import org.joda.time.DateTime
import play.api.libs.json._
case class User(name: String, created: DateTime)
object User {
implicit val yourJodaDateReads = Reads.jodaDateReads("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
implicit val yourJodaDateWrites = Writes.jodaDateWrites("yyyy-MM-dd'T'HH:mm:ss.SSSZ'")
implicit val userFormat = Json.format[User]
def main(args: Array[String]) {
val value = Json.parse("{ \"name\" : \"hello\" , \"created\" : \"2015-07-16T20:32:04.046+02:00\" }")
println(Json.toJson(new User("user", new DateTime())))
println(Json.fromJson(value))
}
}
Based on this solution, I'm getting this error:
Error:(18, -1) Play 2 Compiler:
/activator-1.3.2/notifier-app/app/model/Test.scala:18: ambiguous implicit values:
both value yourJodaDateReads in object User of type => play.api.libs.json.Reads[org.joda.time.DateTime]
and value userFormat in object User of type => play.api.libs.json.OFormat[model.User]
I'm using Activator 1.3.2 and Play 2.3.8.
Could you please advice me ?
Thanks in advance.
update
I understand there is a conflict with the implicit value in play.api.libs.json.Reads
implicit val DefaultJodaDateReads = jodaDateReads("yyyy-MM-dd")
How can I resolve this issue ?
In play 2.6, the canonical way to serialize/deserialize joda DateTime json is by using the play-json-joda library. Import the library by updating your build.sbt
. Then create json reader and json writers like this :
import play.api.libs.json.JodaWrites
implicit val dateTimeWriter: Writes[DateTime] = JodaWrites.jodaDateWrites("dd/MM/yyyy HH:mm:ss")
import play.api.libs.json.JodaReads
implicit val dateTimeJsReader = JodaReads.jodaDateReads("yyyyMMddHHmmss")
I know this question has been answered for a while, but I found a more concise answer
val pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
implicit val dateFormat = Format[DateTime](Reads.jodaDateReads(pattern), Writes.jodaDateWrites(pattern))
implicit val userFormat = Json.format[User]
Expecting a better alternative, here my workaround:
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
val jodaDateReads = Reads[DateTime](js =>
js.validate[String].map[DateTime](dtString =>
DateTime.parse(dtString, DateTimeFormat.forPattern(dateFormat))
)
)
val jodaDateWrites: Writes[DateTime] = new Writes[DateTime] {
def writes(d: DateTime): JsValue = JsString(d.toString())
}
val userReads: Reads[User] = (
(JsPath \ "name").read[String] and
(JsPath \ "created").read[DateTime](jodaDateReads)
)(User.apply _)
val userWrites: Writes[User] = (
(JsPath \ "name").write[String] and
(JsPath \ "created").write[DateTime](jodaDateWrites)
)(unlift(User.unapply))
implicit val userFormat: Format[User] = Format(userReads, userWrites)
Try these:
implicit val dateWrites = jodaDateWrites("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
implicit val dateReads = jodaDateReads("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
https://github.com/playframework/playframework/blob/master/framework/src/play-json/src/main/scala/play/api/libs/json/Writes.scala#L411
https://github.com/playframework/playframework/blob/master/framework/src/play-json/src/main/scala/play/api/libs/json/Reads.scala#L645
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With