我正在浏览: http://www.playframework.com/documentation/2.1.1/ScalaJsonCombinators并搜索 用于在执行读写操作时如何排除值。它可能在那里,但我不能 找到它。
因此,假设我有一个案例类预订,其中包含一系列参数, creationTime 和 bookingId 。当从对象写入Json时,我想在从Json读取对象时编写所有参数但,例如在我的控制器中创建POST时发生POST:
def createBooking = Action(parse.json) {
implicit request => {
request.body.validate[Booking].map {
case (booking) => {
Logger.info("" + booking)
Ok("ONLY TEST")
}
}.recoverTotal {
e => BadRequest("Detected error:" + JsError.toFlatJson(e))
}
}
}
我不想在Json POST中提供值 creationTime 和 bookingId ,我不想用我的Reads impl读取值:
package models.booking
import java.util.UUID
import org.joda.time.{DateTime}
import play.api.libs.functional.syntax._
import play.api.libs.json._
case class Booking (bookingId: UUID,
rId: Long,
creationTime: DateTime,
user: User,
dateTime: BookingTime,
numOfGuest: Int,
status: BookingState.BookingState) {
def accepted(): Booking = {
this.copy(status = BookingState.ACCEPTED)
}
def sent(): Booking = {
this.copy(status = BookingState.SENT)
}
def denied(): Booking = {
this.copy(status = BookingState.DENIED)
}
def denyWithNewTimeSuggestion(): Booking = {
this.copy(status = BookingState.DENIED_NEW_TIME_SUGGESTED)
}
def timeout(): Booking = {
this.copy(status = BookingState.TIMED_OUT)
}
def sendOnOpening(): Booking = {
this.copy(status = BookingState.ON_HOLD)
}
}
object Booking {
implicit object UUIDFormat extends Format[UUID] {
def writes(uuid: UUID): JsValue = JsString(uuid.toString())
def reads(json: JsValue): JsResult[UUID] = json match {
case JsString(x) => JsSuccess(UUID.fromString(x))
case _ => JsError("Expected UUID as JsString")
}
}
val pattern = "yyyy-M-dd"
implicit val dateFormat =
Format[DateTime](Reads.jodaDateReads(pattern), Writes.jodaDateWrites(pattern))
import utils.EnumUtils.enumReads
implicit val bookingStateReads = enumReads(BookingState)
import play.api.libs.json.Reads._
implicit val bookingReads: Reads[Booking] = (
(__ \ "bookingId").read[UUID] and
(__ \ "rId").read[Long] and
(__ \ "creationTime").read[DateTime] and
(__ \ "user").read[User] and
(__ \ "dateTime").read[BookingTime] and
(__ \ "numOfGuest").read[Int] and
(__ \ "status").read[BookingState.BookingState]
)(Booking.apply _)
import utils.EnumUtils.enumWrites
import play.api.libs.json.Writes._
implicit val bookingWrites: Writes[Booking] = (
(__ \ "bookingId").write[UUID] and
(__ \ "rId").write[Long] and
(__ \ "creationTime").write[DateTime] and
(__ \ "user").write[User] and
(__ \ "dateTime").write[BookingTime] and
(__ \ "numOfGuest").write[Int] and
(__ \ "status").write[BookingState.BookingState]
)(unlift(Booking.unapply))
}
object BookingState extends Enumeration {
type BookingState = Value
val NEW = Value("NEW")
val SENT = Value("SENT")
val ACCEPTED = Value("ACCEPTED")
val DENIED = Value("DENIED")
val DENIED_NEW_TIME_SUGGESTED = Value("DENIED_NEW_TIME_SUGGESTED")
val TIMED_OUT = Value("TIMED_OUT")
val ON_HOLD = Value("ON_HOLD")
}
如何指定排除值 bookingId 和 creationTime 的customBookingReads
。
我还想保留原来的'bookingReads',以便我可以将其用于其他事情。
答案 0 :(得分:0)
经过一番深思熟虑,我意识到在这种情况下,预订和请求实际上是两件不同的事情,因此我不应该尝试将这两者混合在一起。
我创建了一个BookingRequest,可以从中创建预订。
<强> BookingRequest:强>
case class BookingRequest(rId: Long,
user: User,
bookingTime: BookingTime,
numOfGuest: Int) {
def createBooking(): Booking = {
Booking(UUID.randomUUID(), this.rId, new DateTime(), this.user, this.bookingTime, this.numOfGuest, BookingState.NEW)
}
}
object BookingRequest {
import play.api.libs.json.Reads._
implicit val bookingRequestReads: Reads[BookingRequest] = (
(__ \ "rId").read[Long] and
(__ \ "user").read[User] and
(__ \ "bookingTime").read[BookingTime] and
(__ \ "numOfGuest").read[Int]
)(BookingRequest.apply _)
import play.api.libs.json.Writes._
implicit val bookingRequestWrites: Writes[BookingRequest] = (
(__ \ "rId").write[Long] and
(__ \ "user").write[User] and
(__ \ "bookingTime").write[BookingTime] and
(__ \ "numOfGuest").write[Int]
)(unlift(BookingRequest.unapply))
}
<强>控制器:强>
def createBooking = Action(parse.json) {
implicit request => {
request.body.validate[BookingRequest].map {
case (bookingRequest) => {
Logger.info("Booking" + bookingRequest)
Logger.info("BookingRequest" + bookingRequest.createBooking())
// SAVE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Ok(Json.obj("status" -> "OK"))
}
}.recoverTotal {
e => BadRequest(Json.obj("status" ->"KO", "message" -> JsError.toFlatJson(e)))
}
}
}
但我仍然很好奇如何在执行读写操作时排除值
答案 1 :(得分:0)
我认为获得所需内容的最简单方法是制作bookingId
和creationTime
字段Option
。
case class Booking (bookingId: Option[UUID],
rId: Long,
creationTime: Option[DateTime],
user: User,
dateTime: BookingTime,
numOfGuest: Int,
status: BookingState.BookingState)
这样,您的reads
仍然可以尝试来阅读这些字段,但如果它们丢失,则只会None
。即使使用标准Json.format[Booking]
,我也可以做到这一点并且效果很好 - 无需自定义代码。