我是 scala 新手,来自 Ruby 背景,所以在我使用 scalatra、mongodb 和 liftweb mongo 记录以及 argonaut 进行 JSon 序列化和反序列化的 Web 服务中呈现 json 响应时遇到问题。
但是,根据http://argonaut.io/给出的示例,我无法弄清楚在使用 net.liftweb.mongo.record 库时这是如何工作的。
在编译这个我得到一个错误,上面写着type mismatch
. 错误描述跟在代码片段后面。
首先打包scalatraapp 导入 org.scalatra 导入 net.liftweb.mongodb._ 导入 net.liftweb.mongodb.record.MongoRecord 导入 net.liftweb.mongodb.record.field.ObjectIdPk 导入 net.liftweb.record.field.StringField 导入 net.liftweb.record.field.IntField 导入 net.liftweb.record.field.PasswordField 导入 net.liftweb.record.field.DateTimeField 导入 net.liftweb.mongodb.record.MongoMetaRecord 进口argonaut._ 导入 Argonaut._ case class Person private 用 ObjectIdPk[Person] 扩展 MongoRecord[Person] { def meta = 人 对象年龄扩展 IntField(this, 3) 对象名称扩展 StringField(this, 29) 对象 created_at 扩展 DateTimeField(this) 对象密码扩展 PasswordField(this) } 对象 Person 使用 MongoMetaRecord[Person] { 隐式 def PersonCodecJson: CodecJson[Person] = casecodec3(Person.apply, Person.unapply)("name", "age", "things") }
我得到的错误是
[error] found : () => firstscalatraapp.Person
[error] required: (?, ?, ?) => ?
[error] casecodec3(Person.apply, Person.unapply)("name", "age", "things")
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
这似乎是合乎逻辑的,因为构造函数不接受任何参数,并且 mongo 库似乎正在val
为我需要的类生成字段(我仍然不完全理解 lift mongo 包装器的作用)。
那么我如何定义隐式以便能够找到序列化人员类型的对象。
另外,在处理集合时如何定义序列化功能。例如,当我有一个List[Person]
.
提前致谢。我真的很感激我能得到的任何帮助。