简体   繁体   English

如何在Play Json中使用Joda DateTime

[英]How to use Joda DateTime with Play Json

I'm developing a Play application, and I'm trying to use a Joda DateTime object into my case class. 我正在开发一个Play应用程序,我正在尝试将Joda DateTime对象用于我的case类。

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. 我正在使用Activator 1.3.2和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 我知道play.api.libs.json.Reads中的隐含值存在冲突

implicit val DefaultJodaDateReads = jodaDateReads("yyyy-MM-dd") 

How can I resolve this issue ? 我该如何解决这个问题?

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)

In play 2.6, the canonical way to serialize/deserialize joda DateTime json is by using the play-json-joda library. 在游戏2.6中,使用play-json-joda库序列化/反序列化joda DateTime json的规范方法。 Import the library by updating your build.sbt . 通过更新build.sbt 导入库 Then create json reader and json writers like this : 然后像这样创建json reader和json writer:

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]

I think you should set the User type in Json.toJson and Json.fromJson functions. 我认为你应该在Json.toJsonJson.fromJson函数中设置User类型。 Instead of 代替

println(Json.toJson(new User("user", new DateTime())))
println(Json.fromJson(value))

try: 尝试:

println(Json.toJson[User](new User("user", new DateTime())))
println(Json.fromJson[User](value))

When you set the type explicitly framework will know what reads/writes to use. 当您明确设置类型时,框架将知道要使用的读/写内容。

Update: It is not necessarily to set type for Json.toJson function because you pass User object as function argument and framework determines the type in runtime. 更新:不一定要为Json.toJson函数设置类型,因为您将User对象作为函数参数传递,框架确定运行时的类型。 But for Json.fromJson[User] you must set the type, otherwise framework doesn't know type of the object you want to read. 但对于Json.fromJson[User]您必须设置类型,否则框架不知道您要读取的对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM