gpt4 book ai didi

json - 在 Play2 scala 应用程序中执行 json 读取和写入时如何排除值

转载 作者:行者123 更新时间:2023-12-03 17:52:53 25 4
gpt4 key购买 nike

我正在浏览:
http://www.playframework.com/documentation/2.1.1/ScalaJsonCombinators和搜索
关于如何在读写时排除值。它可能在那里,但我不能
找到它。

所以假设我有一个案例类 Booking 有一堆参数,一个 创建时间 预订编号 .从对象写入 Json 时,我想写入所有参数 但是从 Json 读取对象时,例如在我的 Controller 中对 createdBooking 进行 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 中,我不想使用 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")
}

如何指定 customBookingReads排除值 预订编号 创建时间 .
我还想保留原始的“bookingReads”,以便我可以将其用于其他用途。

最佳答案

经过一番深思熟虑后,我意识到在这种情况下,预订和请求实际上是两个不同的东西,因此我不应该尝试将这两者混为一谈。

我创建了一个 BookingRequest,从中可以创建一个 Booking。

预订请求:

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))
}

Controller :
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)))
}
}
}

但是我仍然很好奇如何在进行读取和写入时排除值

关于json - 在 Play2 scala 应用程序中执行 json 读取和写入时如何排除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com