json - Scala Play 2.4 Serialize with Parameter Type -
i took at: scala type deferring, looks close problem can't resolve answer, unfortunately.
so, here's code:
my genericmodel
abstract class genericmodel[t] { val _id: option[bsonobjectid] def withid(newid: bsonobjectid): t }
my implemented model
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] }
my dao, using case class
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 } } }
i got error
no json serializer jsobject found type t. try implement implicit owrites or oformat type
here, during insert method
db.collection[jsoncollection](collectionname).insert(beaconwithid)
like said before, tried implicit writes. help, hope didn't missed on referenced topic on beggining.
the idea add [t : writes]
, question where. can try :
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[t : writes](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 } } }
or perhaps (or both) :
abstract class genericmodel[t : writes] { val _id: option[bsonobjectid] def withid(newid: bsonobjectid): t }
Comments
Post a Comment