emm*_*a90 7 json scala playframework slick play-json
我需要将一个 Json 字段保存为我的 Play 框架模型的一列。我在 DAO 中的表解析器是
class Table(tag: Tag) extends Table[Model](tag, "tablename") {
implicit val configFormat = Json.format[Config]
// Fields ...
def config = column[Config]("config", O.SqlType("JSON"))
// Fields ...
}
Run Code Online (Sandbox Code Playgroud)
Config
在 Play Model 文件夹中的 Model 中定义为一个案例类,并有他的伴生对象。此对象的字段是 Int、Double 或 String
case class Config ( // fields )
object Config {
implicit val readConfig: Reads[Config] = new Reads[Config]
for {
// fields
} yield Config(// fields)
implicit val configFormat = Json.format[Config]
}
Run Code Online (Sandbox Code Playgroud)
问题是由于这个错误我无法编译
Error:(28, 37) could not find implicit value for parameter tt:
slick.ast.TypedType[models.Config]
def config = column[Config]("config", O.SqlType("JSON"))
Run Code Online (Sandbox Code Playgroud)
有没有办法将配置模型保存为表中的 Json(将其读取为配置)?
您需要告诉 Slick 如何将您的Config
实例转换为数据库列:
implicit val configFormat = Json.format[Config]
implicit val configColumnType = MappedColumnType.base[Config, String](
config => Json.stringify(Json.toJson(config)),
column => Json.parse(column).as[Config]
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2666 次 |
最近记录: |