gpt4 book ai didi

json - 在 Play for Scala 中将异构列表与 Json 相互转换

转载 作者:行者123 更新时间:2023-12-04 21:10:34 24 4
gpt4 key购买 nike

我正在尝试列出具有共同特征的项目列表,并将它们与 Json 相互转换。我在这里展示的例子是一列带有引擎和汽车的火车。创建火车分为三个类:Engines、Passenger Cars 和 Freight Cars。 (我认为一个简单的基于现实的例子最容易理解,它也没有我试图解决的问题那么复杂。)

火车的部分定义如下:

package models

sealed trait Vehicle {
val kind: String
val maxSpeed: Int = 0
def load: Int
}

case class Engine(override val maxSpeed: Int, val kind: String,
val power: Float, val range: Int) extends Vehicle {
override val load: Int = 0
}

case class FreightCar(override val maxSpeed: Int, val kind: String,
val load: Int) extends Vehicle {}

case class PassengerCar(override val maxSpeed: Int, val kind: String,
val passengerCount: Int) extends Vehicle {
override def load: Int = passengerCount * 80
}

(文件 Vehicle.scala)

火车定义为:
package models

import scala.collection.mutable
import play.api.Logger
import play.api.libs.json._

case class Train(val name: String, val cars: List[Vehicle]) {
def totalLoad: Int = cars.map(_.load).sum
def maxSpeed: Int = cars.map(_.maxSpeed).min
}

object Train {
def save(train: Train) {
Logger.info("Train saved ~ Name: " + train.name)
}
}

(文件火车.scala)

如您所见,通过将“汽车”添加到存储在 Train 中的列表来创建火车。

将我的火车转换为 Json 或尝试从 Json 读取它时,我的问题出现了。这是我当前执行此操作的代码:
package controllers

import play.api.mvc._
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.data.validation.ValidationError
import play.api.libs.functional.syntax._
import models.Vehicle
import models.Engine
import models.FreightCar
import models.PassengerCar
import models.Train

class Trains extends Controller {

implicit val JsPathWrites = Writes[JsPath](p => JsString(p.toString))

implicit val ValidationErrorWrites =
Writes[ValidationError](e => JsString(e.message))

implicit val jsonValidateErrorWrites = (
(JsPath \ "path").write[JsPath] and
(JsPath \ "errors").write[Seq[ValidationError]]
tupled
)

implicit object engineLoadWrites extends Writes[Engine] {
def writes(e: Engine) = Json.obj(
"maxSpeed" -> Json.toJson(e.maxSpeed),
"kind" -> Json.toJson(e.kind),
"power" -> Json.toJson(e.power),
"range" -> Json.toJson(e.range)
)
}

implicit object freightCarLoadWrites extends Writes[FreightCar] {
def writes(fc: FreightCar) = Json.obj(
"maxSpeed" -> Json.toJson(fc.maxSpeed),
"kind" -> Json.toJson(fc.kind),
"load" -> Json.toJson(fc.load)
)
}

implicit object passengerCarLoadWrites extends Writes[PassengerCar] {
def writes(pc: PassengerCar) = Json.obj(
"maxSpeed" -> Json.toJson(pc.maxSpeed),
"kind" -> Json.toJson(pc.kind),
"passengerCount" -> Json.toJson(pc.passengerCount)
)
}

implicit object trainWrites extends Writes[Train] {
def writes(t: Train) = Json.obj(
"name" -> Json.toJson(t.name),
"cars" -> Json.toJson(t.cars) // Definitely not correct!
)
}

/* --- Writes above, Reads below --- */

implicit val engineReads: Reads[Engine] = (
(JsPath \ "maxSpeed").read[Int] and
(JsPath \ "kind").read[String] and
(JsPath \ "power").read[Float] and
(JsPath \ "range").read[Int]
)(Engine.apply _)

implicit val freightCarReads: Reads[FreightCar] = (
(JsPath \ "maxSpeed").read[Int] and
(JsPath \ "kind").read[String] and
(JsPath \ "load").read[Int]
)(FreightCar.apply _)

implicit val passengerCarReads: Reads[PassengerCar] = (
(JsPath \ "maxSpeed").read[Int] and
(JsPath \ "kind").read[String] and
(JsPath \ "passengerCount").read[Int]
)(PassengerCar.apply _)

implicit val joistReads: Reads[Train] = (
(JsPath \ "name").read[String](minLength[String](2)) and
(JsPath \ "cars").read[List[Cars]] // Definitely not correct!
)(Train.apply _)

/**
* Validates a JSON representation of a Train.
*/
def save = Action(parse.json) { implicit request =>
val json = request.body
json.validate[Train].fold(
valid = { train =>
Train.save(train)
Ok("Saved")
},
invalid = {
errors => BadRequest(Json.toJson(errors))
}
)
}
}

(文件火车.scala)

为 FreightCars、Engines 列表创建和使用 Json 的所有代码都可以工作,但我无法创建 Json 来同时处理所有三种类型,例如:
implicit object trainWrites extends Writes[Train] {
def writes(t: Train) = Json.obj(
"name" -> Json.toJson(t.name),
"cars" -> Json.toJson(t.cars)
)
}

List 的 Json.toJson 根本不起作用;它的阅读对应物也没有。当我用类 Engines 或我的任何单个具体类替换上面代码中的 t.cars 时,一切正常。

我该如何优雅地解决这个问题以使我的 Json 读者和作家工作?或者,如果 Scala Play 的 Json 编码器解码器不是此类任务的好选择,是否有更合适的 Json 库?

最佳答案

运行您的Writes代码返回以下错误(它提供了有关修复内容的线索):

Error:(78, 27) No Json deserializer found for type Seq[A$A90.this.Vehicle]. Try to implement an implicit Writes or Format for this type.
"cars" -> Json.toJson(t.cars) // Definitely not correct!
^

未找到 Vehicle 的解串器,所以你需要添加一个 Reads/Writes (或 Format )对于 Vehicle .这只会委托(delegate)给实际的 Format对于类型。
writes 的实现非常简单。 ,可以只对类型进行模式匹配。对于 reads我正在寻找 json 中的一个显着属性来指示 Reads委托(delegate)给。

Note that play-json provides helpers so you don't have to manually implement Writes/Reads for case classes, so you can write val engineLoadWrites : Writes[Engine] = Json.writes[Engine]. This is used in the sample below.


//Question code above, then ...

val engineFormat = Json.format[Engine]
val freightCarFormat = Json.format[FreightCar]
val passengerCarFormat = Json.format[PassengerCar]

implicit val vehicleFormat = new Format[Vehicle]{
override def writes(o: Vehicle): JsValue = {
o match {
case e : Engine => engineFormat.writes(e)
case fc : FreightCar => freightCarFormat.writes(fc)
case pc : PassengerCar => passengerCarFormat.writes(pc)
}
}

override def reads(json: JsValue): JsResult[Vehicle] = {
(json \ "power").asOpt[Int].map{ _ =>
engineFormat.reads(json)
}.orElse{
(json \ "passengerCount").asOpt[Int].map{ _ =>
passengerCarFormat.reads(json)
}
}.getOrElse{
//fallback to FreightCar
freightCarFormat.reads(json)
}
}
}


implicit val trainFormat = Json.format[Train]


val myTrain = Train(
"test",
List(
Engine(100, "e-1", 1.0.toFloat, 100),
FreightCar(100, "f-1", 20),
PassengerCar(100, "pc", 10)
)
)

val myTrainJson = trainFormat.writes(myTrain)
/** => myTrainJson: play.api.libs.json.JsObject = {"name":"test","cars":[{"maxSpeed":100,"kind":"e-1","power":1.0,"range":100},{"maxSpeed":100,"kind":"f-1","load":20},{"maxSpeed":100,"kind":"pc","passengerCount":10}]} */

val myTrainTwo = myTrainJson.as[Train]
/* => myTrainTwo: Train = Train(test,List(Engine(100,e-1,1.0,100), FreightCar(100,f-1,20), PassengerCar(100,pc,10))) */

关于json - 在 Play for Scala 中将异构列表与 Json 相互转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35397766/

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