尝试从客户端读取JSON数据并解析它,因此我可以使用insert方法将其插入到表中,但我对Play 2.5和Slick 3.1.1的隐式读取会为Option [BigDecimal]引发错误,为什么?
object RBooks {implicit val xReads: Reads[xRow] = (
(JsPath \ "bookId").read[Option[BigDecimal]] and
(JsPath \ "bookName").read[Option[String]] and
(JsPath \ "bookDesc").read[Option[String]] and
(JsPath \ "enabled").read[Option[Char]] and
(JsPath \ "primaryBook").read[Option[Char]] and
(JsPath \ "bookType").read[Option[String]] and
(JsPath \ "bookCurrency").read[Option[String]] and
(JsPath \ "startDate").read[Option[java.sql.Timestamp]] and
(JsPath \ "endDate").read[Option[java.sql.Timestamp]] and
(JsPath \ "allocationsEnabled").read[Option[Char]] and
(JsPath \ "arrPrefix").read[Option[String]] and
(JsPath \ "creationDate").read[Option[java.sql.Timestamp]] and
(JsPath \ "createdBy").read[Option[String]] and
(JsPath \ "lastUpdateDate").read[Option[Timestamp]] and
(JsPath \ "lastUpdatedBy").read[Option[String]]
)(slickLib.xRow.apply _)
我得到错误输出:
No Json deserializer found for type Option[BigDecimal]. Try to implement an implicit Reads or Format for this type.(xRow.apply _) this code is throwing a error: (JsPath \ "bookId").read[Option[BigDecimal]] and
Play Scala中的.read
不支持Option [BigDecimal]吗?
答案 0 :(得分:2)
不要使用read[Option[Anytype]]
。请改用readNullable[Anytype]
。
例如:
(JsPath \ "bookId").readNullable[BigDecimal]
对于不受支持的类型,您应该编写自己的解析器或从支持类型转换为您想要的类型。例如,您可以像这样处理java.sql.Timestamp
:
import java.sql.Timestamp
(__ \ 'creationDate).readNullable[Long].map(_.map(new Timestamp(_)))
答案 1 :(得分:-1)
Play 2.5 scala 12.11.x不支持选项[BigDecimal]。另一种方法是使用像json4s这样的库来实现ORM项目的光滑。