Json Scala Play 2.4使用参数类型序列化
我已经看了一眼:,看起来很接近我的问题,但不幸的是,我无法用答案解决它 下面是代码: 我的通用模型Json Scala Play 2.4使用参数类型序列化,json,scala,serialization,playframework,parameterized-types,Json,Scala,Serialization,Playframework,Parameterized Types,我已经看了一眼:,看起来很接近我的问题,但不幸的是,我无法用答案解决它 下面是代码: 我的通用模型 abstract class GenericModel[T] { val _id: Option[BSONObjectID] def withId(newId: BSONObjectID): T } 我的实现模型 case class Push (_id: Option[BSONObjectID], text: String) extends GenericModel[Push] {
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]
}
我的刀,使用case类
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
}
}
}
这里,在insert方法中
db.collection[JSONCollection](collectionName).insert(beaconWithId)
正如我之前所说,我已经尝试过隐式写作。
谢谢你的帮助,我希望我没有错过关于乞讨的参考主题。想法是添加
[t:Writes]
,问题是在哪里。
您可以尝试以下方法:
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
}
}
}
想法是添加
[T:Writes]
,问题是在哪里。
您可以尝试以下方法:
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
}
}
}
format扩展了format[A]特性,而format[A]特性又扩展了写入[A]和读取[A]的特性。所以错误是正确的,编译器无法找到序列化对象的OWrites
要解决此错误,而不是使用Json.format[Push]扩展OWrites和Reads
implicit object Push extends OWrites[Push] {
def writes(push: Push): JsObject = Json.obj(
"_id" -> push.id,
"text" -> push.text
}
implicit object pushReads extends Reads[Push] {
def reads(json: JsValue): JsResult[Push] = json match {
case obj: JsObject => try {
val id = (obj \ "_id").asOpt[String]
val text = (obj \ "text").as[String]
JsSuccess(Push(id, text))
} catch {
case cause: Throwable => JsError(cause.getMessage)
}
case _ => JsError("expected.jsobject")
}
}
而在道中,变化将[T]写入奥维特[T]
format扩展了format[A]特性,而format[A]特性又扩展了写入[A]和读取[A]的特性。所以错误是正确的,编译器无法找到序列化对象的OWrites
要解决此错误,而不是使用Json.format[Push]扩展OWrites和Reads
implicit object Push extends OWrites[Push] {
def writes(push: Push): JsObject = Json.obj(
"_id" -> push.id,
"text" -> push.text
}
implicit object pushReads extends Reads[Push] {
def reads(json: JsValue): JsResult[Push] = json match {
case obj: JsObject => try {
val id = (obj \ "_id").asOpt[String]
val text = (obj \ "text").as[String]
JsSuccess(Push(id, text))
} catch {
case cause: Throwable => JsError(cause.getMessage)
}
case _ => JsError("expected.jsobject")
}
}
在DAO中,change将[T]写入OWrites[T]我也遇到了同样的问题,我必须实现一种方法来告诉对象如何成为JsValue:
def toJs[T : Writes](o: T) = Json.toJson(o).as[JsObject]
因此,我的MongoRepository的插入部分如下所示:
trait MongoRepository[T <: MongoModel] {
lazy val db: DB = {
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.core.nodeset.Authenticate
import scala.collection.JavaConversions._
val driver = new MongoDriver
val connection = driver.connection(
config.getStringList("mongodb.servers"),
MongoConnectionOptions(),
Seq(Authenticate(
config.getString("mongodb.db"),
config.getString("mongodb.credentials.username"),
config.getString("mongodb.credentials.password")
)
)
)
connection.db(config.getString("mongodb.db"))
}
def collectionName: String
def collection = db(collectionName)
def ensureIndexes: Future[List[Boolean]]
def toJs[T : Writes](o: T) = Json.toJson(o).as[JsObject]
def insert(document: T)(implicit writer: Writes[T]): Future[Either[ServiceException, T]] = {
document._id match {
case None => document._id = Some(BSONObjectID.generate)
case Some(exist) => {}
}
document.created = Some(DateTime.now)
document.updated = Some(DateTime.now)
Logger.debug(s"Inserting document: [collection=$collectionName, data=$document]")
recoverOperation(collection.insert(toJs(document))) {
document
}
}
...
}
trait MongoRepository[T document.\u id=Some(BSONObjectID.generate)
案例部分(存在)=>{}
}
document.created=Some(DateTime.now)
document.updated=Some(DateTime.now)
debug(s“插入文档:[collection=$collectionName,data=$document]”)
恢复操作(collection.insert(toJs(document))){
文件
}
}
...
}
希望这有帮助:)我也有同样的问题,我必须实现一个方法来告诉对象如何成为JsValue:
def toJs[T : Writes](o: T) = Json.toJson(o).as[JsObject]
因此,我的MongoRepository的插入部分如下所示:
trait MongoRepository[T <: MongoModel] {
lazy val db: DB = {
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.core.nodeset.Authenticate
import scala.collection.JavaConversions._
val driver = new MongoDriver
val connection = driver.connection(
config.getStringList("mongodb.servers"),
MongoConnectionOptions(),
Seq(Authenticate(
config.getString("mongodb.db"),
config.getString("mongodb.credentials.username"),
config.getString("mongodb.credentials.password")
)
)
)
connection.db(config.getString("mongodb.db"))
}
def collectionName: String
def collection = db(collectionName)
def ensureIndexes: Future[List[Boolean]]
def toJs[T : Writes](o: T) = Json.toJson(o).as[JsObject]
def insert(document: T)(implicit writer: Writes[T]): Future[Either[ServiceException, T]] = {
document._id match {
case None => document._id = Some(BSONObjectID.generate)
case Some(exist) => {}
}
document.created = Some(DateTime.now)
document.updated = Some(DateTime.now)
Logger.debug(s"Inserting document: [collection=$collectionName, data=$document]")
recoverOperation(collection.insert(toJs(document))) {
document
}
}
...
}
trait MongoRepository[T document.\u id=Some(BSONObjectID.generate)
案例部分(存在)=>{}
}
document.created=Some(DateTime.now)
document.updated=Some(DateTime.now)
debug(s“插入文档:[collection=$collectionName,data=$document]”)
恢复操作(collection.insert(toJs(document))){
文件
}
}
...
}
希望这有帮助:)None起作用。总是相同的错误。如果我在GenericModel中修改T,然后按格式崩溃…None起作用。总是相同的错误。如果我在GenericModel中修改T,然后按格式崩溃…我实际上面临着几乎相同的问题。你已经解决了吗?没有找到解决方案。我的insert方法在中现在实现DAO,而不是在泛型DAO中不幸的是,就像我搜索的那样…很抱歉,我实际上面临着几乎相同的问题..你已经解决了吗?没有找到解决方案。我的插入方法现在在实现DAO中,而不是在泛型DAO中不幸的是,就像我搜索的那样…对不起