【发布时间】:2015-10-13 20:03:14
【问题描述】:
我已经看过了:Scala type deferring,它看起来很接近我的问题,但很遗憾,我无法用答案解决它。
所以,这里是代码:
我的通用模型
abstract class GenericModel[T] {
val _id: Option[BSONObjectID]
def withId(newId: BSONObjectID): T
}
我的实现模型
case class Push
(_id: Option[BSONObjectID], text: String)
extends GenericModel[Push]
{
override def withId(newId: BSONObjectID) = this.copy(_id = Some(newId))
}
object Push{
implicit val pushFormat = Json.format[Push]
}
我的 DAO,使用案例类
trait GenericDao[T <: GenericModel[T]] {
val db: DB
val collectionName: String
/**
* Inserts new object
* @param newobject
* @return Some(stringified bsonID) or None if error
*/
def insert(newobject: T)(implicit tjs: Writes[T]): Future[Option[BSONObjectID]] = {
val bsonId = BSONObjectID.generate
val beaconWithId = newobject.withId(bsonId)
db.collection[JSONCollection](collectionName).insert(beaconWithId).map{ lastError =>
if(lastError.ok)
Some(bsonId)
else
None
}
}
}
我收到了错误
No Json serializer as JsObject found for type T. Try to implement an implicit OWrites or OFormat for this type
这里,在插入方法中
db.collection[JSONCollection](collectionName).insert(beaconWithId)
就像我之前说的,我已经尝试过隐式写入。 感谢您的帮助,我希望我没有错过关于开始时引用的主题的任何内容。
【问题讨论】:
-
我实际上面临着几乎相同的问题..你已经弄清楚了吗?
-
没有找到解决办法。我的插入方法现在在实现 DAO 中,而不是在通用 DAO 中,不幸的是,就像我搜索的那样......对不起
标签: json scala serialization playframework parameterized-types