JSON Read/Write Error: Application Does Not Take Parameters
JSON Read/Write Error: Application Does Not Take Parameters
我正在尝试为一个案例 class 编写一个 Read/Write 值,其中许多值可能为空。按照 Play Framework 的教程,我将其写为
implicit val incomingMessageWrites: Writes[IncomingMessage] = (
(JsPath \ "stageNum").write[Int] and
(JsPath \ "stage").write[String] and
(JsPath \ "stageTime").write[Long] and
(JsPath \ "vendorId").write[String] and
(JsPath \ "text").writeNullable[String] and
(JsPath \ "campaignCode").writeNullable[String] and
(JsPath \ "osTotals").writeNullable[OSTotals] and
(JsPath \ "splitSegment").writeNullable[Int] and
(JsPath \ "segmentNum").writeNullable[Int] and
(JsPath \ "osBatchStatus").writeNullable[OSBatchStatus]
)(unlift(IncomingMessage.unapply))
但是,我一直遇到同样的问题,IntelliJ 告诉我提升方法不起作用,因为 "Application Does Not Take Parameters"。
案例 class 是
case class IncomingMessage(
stageNum: Int,
stage: String,
stageTime: Long,
vendorId: String,
text: String,
campaignCode: String,
osTotals: OSTotals,
splitSegment: Int,
segmentNum: Int,
osBatchStatus: OSBatchStatus) {
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, 0, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, splitSegment: Int, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, splitSegment, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String, osTotals: OSTotals) =
this(stageNum, stage, stageTime, vendorId, null, null, osTotals, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, oSBatchStatus: OSBatchStatus) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, 0, osBatchStatus)
}
另一种情况也是如此class
case class OSBatchStatus(os: String, success: Int, failure: Int)
object OSBatchStatus {
implicit val oSBatchStatusReads: Reads[OSBatchStatus] = (
(JsPath \ "os").readNullable[String] and
(JsPath \ "success").readNullable[Int] and
(JsPath \ "failure").readNullable[Int]
)(OSBatchStatus.apply _)
}
我写了读取和写入值,但为了保存而省略了它们 space。
我们将不胜感激任何帮助。
如果您的输入 JSON 文件中有可选字段,那么您的案例 class 的相应字段也应该是可选的。您为 OSBatchStatus
定义的 Reads
假定所有字段都是可选的,因此您的案例 class 应该如下所示:
case class OSBatchStatus(os: Option[String], success: Option[Int], failure: Option[Int])
同样的规则适用于您的 IncomingMessage
案例 class(四个字段是必填字段,其他字段应该是一个 Option[T]
)。此外,由于 JSON 文件中的字段与案例 class 中的字段名称完全相同,您可以使用 play-json 方法自动生成 Reads/Writes/Format
你:
import play.api.libs.functional.syntax._
import play.api.libs.json._
implicit val incomingMessageWrites = Json.writes[IncomingMessage]
implicit val osBatchStatusReads = Json.reads[OSBatchStatus]
implicit val osTotalsFormat = Json.format[OSTotals] // Will generate both `Reads` and `Writes`
我正在尝试为一个案例 class 编写一个 Read/Write 值,其中许多值可能为空。按照 Play Framework 的教程,我将其写为
implicit val incomingMessageWrites: Writes[IncomingMessage] = (
(JsPath \ "stageNum").write[Int] and
(JsPath \ "stage").write[String] and
(JsPath \ "stageTime").write[Long] and
(JsPath \ "vendorId").write[String] and
(JsPath \ "text").writeNullable[String] and
(JsPath \ "campaignCode").writeNullable[String] and
(JsPath \ "osTotals").writeNullable[OSTotals] and
(JsPath \ "splitSegment").writeNullable[Int] and
(JsPath \ "segmentNum").writeNullable[Int] and
(JsPath \ "osBatchStatus").writeNullable[OSBatchStatus]
)(unlift(IncomingMessage.unapply))
但是,我一直遇到同样的问题,IntelliJ 告诉我提升方法不起作用,因为 "Application Does Not Take Parameters"。
案例 class 是
case class IncomingMessage(
stageNum: Int,
stage: String,
stageTime: Long,
vendorId: String,
text: String,
campaignCode: String,
osTotals: OSTotals,
splitSegment: Int,
segmentNum: Int,
osBatchStatus: OSBatchStatus) {
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, 0, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, splitSegment: Int, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, splitSegment, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String, osTotals: OSTotals) =
this(stageNum, stage, stageTime, vendorId, null, null, osTotals, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, oSBatchStatus: OSBatchStatus) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, 0, osBatchStatus)
}
另一种情况也是如此class
case class OSBatchStatus(os: String, success: Int, failure: Int)
object OSBatchStatus {
implicit val oSBatchStatusReads: Reads[OSBatchStatus] = (
(JsPath \ "os").readNullable[String] and
(JsPath \ "success").readNullable[Int] and
(JsPath \ "failure").readNullable[Int]
)(OSBatchStatus.apply _)
}
我写了读取和写入值,但为了保存而省略了它们 space。
我们将不胜感激任何帮助。
如果您的输入 JSON 文件中有可选字段,那么您的案例 class 的相应字段也应该是可选的。您为 OSBatchStatus
定义的 Reads
假定所有字段都是可选的,因此您的案例 class 应该如下所示:
case class OSBatchStatus(os: Option[String], success: Option[Int], failure: Option[Int])
同样的规则适用于您的 IncomingMessage
案例 class(四个字段是必填字段,其他字段应该是一个 Option[T]
)。此外,由于 JSON 文件中的字段与案例 class 中的字段名称完全相同,您可以使用 play-json 方法自动生成 Reads/Writes/Format
你:
import play.api.libs.functional.syntax._
import play.api.libs.json._
implicit val incomingMessageWrites = Json.writes[IncomingMessage]
implicit val osBatchStatusReads = Json.reads[OSBatchStatus]
implicit val osTotalsFormat = Json.format[OSTotals] // Will generate both `Reads` and `Writes`