【发布时间】:2017-02-03 19:17:02
【问题描述】:
我正在尝试将 POST 请求传递给使用 Play 开发的 REST API! (2.5) 一个我想使用 Scala Graph 的对象(来自 graph-core 依赖项)。 看起来该图已经具有基于 Lift-json 的 JSON 序列化/反序列化方法,但我不确定如何将其“插入”到 Play Json 库中。到目前为止,我一直在使用隐式转换器(使用读/写方法),但我想避免为图形部分编写自己的方法,因为它已经是库本身的一部分。
例如,假设我有这个代码:
import java.util.UUID
import scalax.collection.Graph
case class Task(
id: UUID,
status: String)
case class Stuff(
id: UUID = UUID.randomUUID(),
name: String,
tasks: Option[Graph[Task, DiEdge]])
implicit val stuffWrites: Writes[Stuff] = (
(JsPath \ "id").write[UUID] and
(JsPath \ "name").write[String]
)(unlift(Stuff.unapply))
implicit val stuffReads: Reads[Stuff] = (
(JsPath \ "id").read[UUID] and
(JsPath \ "name").read[String]
)(Stuff.apply _)
implicit val taskWrite: Writes[Task] = (
(JsPath \ "id").write[UUID] and
(JsPath \ "status").write[String]
)(unlift(Task.unapply))
implicit val taskReads: Reads[Task] = (
(JsPath \ "id").read[UUID] and
(JsPath \ "status").read[String]
)(Task.apply _)
我错过了序列化图表和育儿的部分。我应该从头开始重写所有内容,还是可以依赖 scalax.collection.io.json 中的 toJson/fromJson 方法?
【问题讨论】:
标签: json scala playframework-2.0