【问题标题】:Converting a Heterogeneous List to and from Json in Play for Scala在 Play for Scala 中将异构列表与 Json 相互转换
【发布时间】:2016-02-15 06:02:42
【问题描述】:

我正在尝试获取具有共同特征的项目列表,并将它们与 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)
  }
}

(文件 Train.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))
      }
    )
  }
}

(文件 Trains.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)
   )
 }

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

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

【问题讨论】:

    标签: json scala playframework


    【解决方案1】:

    运行您的 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 的解串器,因此您需要为Vehicle 添加Reads/Writes(或Format)。这只会委托给该类型的实际Format

    writes 的实现非常简单,只需对类型进行模式匹配即可。对于 reads,我正在寻找 json 中的一个显着属性,以指示要委托给的 Reads

    请注意,play-json 提供了帮助程序,因此您不必为案例类手动实现 Writes/Reads,因此您可以编写 val engineLoadWrites : Writes[Engine] = Json.writes[Engine]。这在下面的示例中使用。

    //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))) */
    

    【讨论】:

    • 这成功了!如果您同意,我现在建议 Play 框架中的人员将其作为示例包含在他们的文档中。我还想在文档中添加一个枚举示例。
    【解决方案2】:

    我相信你的问题是你在Train.scala 中使用了一个可变的List,而你在Trains.scala 中使用了一个不可变的变体。我什至不确定 Play 是否为可变列表提供了格式化程序,但我确信它可以为不可变列表提供。

    尝试删除

    import scala.collection.mutable
    

    看看它是否有效。

    顺便说一句,如果它们直接对应于您的案例类,则无需手动定义读取和写入。 Play 为案例类提供默认的ReadsWritesFormats(如果同时需要):

    implicit val engineFormat = Json.format[Engine]
    

    【讨论】:

    • 这有帮助。 Json.format[] 大大减少了处理 Json 所需的代码量。
    猜你喜欢
    • 2012-08-12
    • 2019-02-20
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多