注意:请参阅问题底部的代码以查看答案
我将DataNode添加为自身的List属性,并开始收到以下编译错误。是否可以拥有该类所属的属性?
我收到编译错误:
ItemInquiry.scala:21:找不到类型为List [models.DataNode]的Json格式化程序。尝试为此类型实现隐式格式。 [错误](__ \“dataNodes”)。format [List [DataNode]]
package models
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class ItemSearchResult(criteria: String, site: String, searchTime: Int, exact: Boolean, searchResults: List[Item])
case class Item(name: String, upc: String, description: String, dataNodes: List[DataNode])
case class DataNode(typeVal: String, title: String, subTitle: String, requiredRole: String, dataNodes: List[DataNode])
object ItemSearchResult {
implicit val dataNode = (
(__ \ "type" ).format[String] and
(__ \ "title" ).format[String] and
(__ \ "requiredRole").format[String] and
(__ \ "subTitle" ).format[String] and
(__ \ "dataNodes" ).format[List[DataNode]]
)(DataNode.apply _, unlift(DataNode.unapply _))
implicit val item = ((__ \ "name").format[String] and
(__ \ "upc").format[String] and
(__ \ "description").format[String] and
(__ \ "dataNodes").format[List[DataNode]])(Item.apply _, unlift(Item.unapply _))
implicit val itemSearchResult = (
(__ \ "criteria").format[String] and
(__ \ "site").format[String] and
(__ \ "searchTime").format[Int] and
(__ \ "exact").format[Boolean] and
(__ \ "searchResults").format[List[Item]])(ItemSearchResult.apply _, unlift(ItemSearchResult.unapply _))
}
更新使用修复我的问题的确切代码:
package models
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class ItemSearchResult(criteria: String, site: String, searchTime: Int, exact: Boolean, searchResults: List[Item])
case class Item(name: String, upc: String, description: String, dataNodes: List[DataNode])
case class DataNode(typeVal: String, title: String, subTitle: String, requiredRole: String, dataNodes: List[DataNode])
object ItemSearchResult {
implicit val itemSearchResultFormat : Writes[ItemSearchResult] =(
(__ \'criteria).write[String] and
(__ \'site).write[String] and
(__ \'searchTime).write[Int] and
(__ \'exact).write[Boolean] and
(__ \'searchResults).lazyWrite(Writes.traversableWrites[Item](itemFormat))
)(unlift(ItemSearchResult.unapply))
implicit val itemFormat : Writes[Item] =(
(__ \'name).write[String] and
(__ \'upc).write[String] and
(__ \'description).write[String] and
(__ \'dataNodes).lazyWrite(Writes.traversableWrites[DataNode](dataNodeFormat))
)(unlift(Item.unapply))
implicit val dataNodeFormat : Writes[DataNode] =(
(__ \'type).write[String] and
(__ \'title).write[String] and
(__ \'subTitle).write[String] and
(__ \'requiredRole).write[String] and
(__ \'dataNodes).lazyWrite(Writes.traversableWrites[DataNode](dataNodeFormat))
)(unlift(DataNode.unapply))
}
答案 0 :(得分:2)
当然,你可以,这是我的代码可以正常工作:
在模型中:
case class Category(
id : Int,
name: String,
subcutegory : List[Category]
)
object Category{
implicit val categoryFormat : Writes[Category] =(
(__ \'id).write[Int] and
(__ \'name).write[String]and
(__ \'subcategory).lazyWrite(Writes.traversableWrites[Category](categoryFormat))
)(unlift(Category.unapply))
}
因此,关键时刻是如何为字段subcategory
实现json格式化程序。
然后,使用Json.toJson(....)