我有一个接受Either
的值类,我想为其生成一个Play for Scala v2.5.6 JSON Format
:
import org.joda.time.{DateTime, Duration}
case class When(when: Either[DateTime, Duration]) extends AnyVal
我想我已经找到了writes
方法;我遇到的问题是使用reads
方法。我尝试了两种方法,都因各种原因而失败。
尝试#1,同时显示reads
和writes
方法:
import play.api.libs.json._
import play.api.libs.json.Json.obj
object When {
def apply(dateTime: DateTime): When = When(Left(dateTime))
def apply(duration: Duration): When = When(Right(duration))
implicit val whenFormat = new Format[When] {
def reads(json: JsValue): JsResult[When] = {
val reads = (__ \ "dateTime").read[Long] { (millis: Long) =>
When(Left(new DateTime(millis)))
} | (__ \ "duration").read[Long] { (millis: Long) =>
When(Right(new Duration(millis)))
}
reads.reads(json)
}
def writes(o: When): JsValue = obj(
o.when.fold(
duration => "duration" -> duration.getMillis,
dateTime => "dateTime" -> dateTime.getMillis
)
)
}
}
错误消息是:
overloaded method value read with alternatives:
[error] (t: Long)play.api.libs.json.Reads[Long] <and>
[error] (implicit r: play.api.libs.json.Reads[Long])play.api.libs.json.Reads[Long]
[error] cannot be applied to (Long => When)
[error] val reads = (__ \ "dateTime").read[Long] { (millis: Long) =>
[error] overloaded method value read with alternatives:
[error] (t: Long)play.api.libs.json.Reads[Long] <and>
[error] (implicit r: play.api.libs.json.Reads[Long])play.api.libs.json.Reads[Long]
[error] cannot be applied to (Long => When)
[error] } | (__ \ "duration").read[Long] { (millis: Long) =>
尝试#2,只显示reads
方法:
def reads(json: JsValue): JsResult[When] =
JsSuccess(
When(Left(new DateTime((__ \ "dateTime").read[Long]))) ||
When(Right(new Duration((__ \ "duration").read[Long])))
)
错误消息是:
value || is not a member of When
[error] Note: implicit value whenFormat is not applicable here because it comes after the application point and it lacks an explicit result type
[error] Error occurred in an application involving default arguments.
我只是喜欢有用的东西,而且我不关心使用什么方法(即使是我没有展示的方法),只要它是可维护和有效的。了解每种方法的错误也是有帮助的。
答案 0 :(得分:8)
以下是如何执行此操作的示例:
import org.joda.time.{DateTime, Duration}
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
object When {
def apply(dateTime: DateTime): When = When(Left(dateTime))
def apply(duration: Duration): When = When(Right(duration))
val reads: Reads[When] =
(__ \ "dateTime").read[Long].map(millis => When(Left(new DateTime(millis)))) |
(__ \ "duration").read[Long].map(millis => When(Right(new Duration(millis))))
val writes: Writes[When] = new Writes[When] {
override def writes(o: When): JsValue = Json.obj(
o.when.fold(
duration => "duration" -> duration.getMillis,
dateTime => "dateTime" -> dateTime.getMillis
)
)
}
implicit val format = Format(reads, writes)
}
基本上你应该映射读数
(__ \ "dateTime").read[Long]
为您提供Reads[Long]
,然后您可以将结果映射到When
。你只是传递参数。此参数可以是Long
,只是忽略读取的内容并返回该值,或者implicit
读取很长时间,您可能不想更改,并且应该让它保持隐式。
然后以类似的方式,您可以创建另一个持续时间的读取并将它们与替代(|
)组合并完成,您已经读取。
你的第二种方法毫无意义。要么使用读取和组合它们,要么只是手动检查是否有东西,如果没有返回不同的结果,但是不值得这样做,只需采用默认方法。