gpt4 book ai didi

json - 单个字段类的 Scala json 解析

转载 作者:行者123 更新时间:2023-12-04 16:08:35 24 4
gpt4 key购买 nike

我有以下类(class)和 Reads Writes 用于 json 解析/写入

import play.api.libs.json._
import play.api.libs.functional.syntax._

import scala.collection.immutable.{Seq => ISeq, Set => ISet}

case class FileExcInc( isExclude: Boolean, isFile: Boolean, pattern: String) { }

implicit val fEIReads: Reads[FileExcInc] = (
(__ \ "isExclude").read[Boolean] and
(__ \ "isFile").read[Boolean] and
(__ \ "pattern").read[String]
)(FileExcInc.apply _)

implicit val fEIWrites: Writes[FileExcInc] = (
(__ \ "isExclude").write[Boolean] and
(__ \ "isFile").write[Boolean] and
(__ \ "pattern").write[String]
)(unlift(FileExcInc.unapply))

case class DirConf(sourceDir: String, csdataFile: String, fileExcIncSeq: ISeq[FileExcInc]) { }

implicit val dCReads: Reads[DirConf] = (
(__ \ "sourceDir").read[String] and
(__ \ "csdataFile").read[String] and
(__ \ "fileExcIncSeq").read[ISeq[FileExcInc]]
)(DirConf.apply _)

implicit val dCWrites: Writes[DirConf] = (
(__ \ "sourceDir").write[String] and
(__ \ "csdataFile").write[String] and
(__ \ "fileExcIncSeq").write[ISeq[FileExcInc]]
)(unlift(DirConf.unapply))

这可以编译,但下面不能:

  case class Conf(dirConfSeq: ISeq[DirConf]) {  }


implicit val cReads: Reads[Conf] = (
(__ \ "dirConfSeq").read[ISeq[DirConf]]
)(Conf.apply _)

implicit val cWrites: Writes[Conf] = (
(__ \ "dirConfSeq").write[ISeq[DirConf]]
)(unlift(Conf.unapply))

它产生以下错误:

[info] Compiling 17 Scala sources and 1 Java source to /home/jk/workspace/Gen/target/scala-2.12/classes...
[error] /home/jk/workspace/Gen/src/main/scala/com/example/CTool.scala:341: overloaded method value read with alternatives:
[error] (t: scala.collection.immutable.Seq[com.example.CTool.DirConf])play.api.libs.json.Reads[scala.collection.immutable.Seq[com.example.CTool.DirConf]] <and>
[error] (implicit r: play.api.libs.json.Reads[scala.collection.immutable.Seq[com.example.CTool.DirConf]])play.api.libs.json.Reads[scala.collection.immutable.Seq[com.example.CTool.DirConf]]
[error] cannot be applied to (scala.collection.immutable.Seq[com.example.CTool.DirConf] => com.example.CTool.Conf)
[error] (__ \ "dirConfSeq").read[ISeq[DirConf]]
[error] ^
[error] /home/jk/workspace/Gen/src/main/scala/com/example/CTool.scala:345: overloaded method value write with alternatives:
[error] (t: scala.collection.immutable.Seq[com.example.CTool.DirConf])(implicit w: play.api.libs.json.Writes[scala.collection.immutable.Seq[com.example.CTool.DirConf]])play.api.libs.json.OWrites[play.api.libs.json.JsValue] <and>
[error] (implicit w: play.api.libs.json.Writes[scala.collection.immutable.Seq[com.example.CTool.DirConf]])play.api.libs.json.OWrites[scala.collection.immutable.Seq[com.example.CTool.DirConf]]
[error] cannot be applied to (com.example.CTool.Conf => scala.collection.immutable.Seq[com.example.CTool.DirConf])
[error] (__ \ "dirConfSeq").write[ISeq[DirConf]]

最近的读取、写入有什么问题?如何为只有一个字段的类创建读取、写入?

最佳答案

要为只有一个字段的案例类创建Reads/Writes,请使用mapcontramap将字段与案例类相互转换。

implicit val cReads: Reads[Conf] =
(__ \ "dirConfSeq").read[ISeq[DirConf]].map(Conf)

implicit val cWrites: Writes[Conf] =
(__ \ "dirConfSeq").write[ISeq[DirConf]].contramap(_.dirConfSeq)

更简单的方法是使用 Json.format 宏来生成所有的 Reads/Writes 样板文件。它处理具有任意数量字段的案例类,前提是字段名称与 JSON 匹配。 Format[T] 既是 Reads[T] 又是 Writes[T]

implicit val fEIFormat: Format[FileExcInc] = Json.format[FileExcInc]
implicit val dCFormat: Format[DirConf] = Json.format[DirConf]
implicit val cFormat: Format[Conf] = Json.format[Conf]

参见:https://www.playframework.com/documentation/2.6.x/ScalaJsonAutomated

关于json - 单个字段类的 Scala json 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126763/

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