Gre*_*g R 19 json scala reactivemongo
我有以下代码在引用时在控制台应用程序中工作 "org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23"
当我更新对我的引用时"org.reactivemongo" % "play2-reactivemongo_2.11" % "0.11.0.play23-M3":
没有为类型为play.api.libs.json.JsObject的JsObject找到Json序列化程序.尝试为此类型实现隐式OWrites或OFormat.
import org.joda.time.DateTime
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._
case class GoogleToken
(
id: Option[BSONObjectID],
name: String,
emailAddress: String,
refreshToken: String,
expires: DateTime
)
object GoogleToken {
import play.api.libs.json.Json
// Generates Writes and Reads
implicit val googleTokenFormat = Json.format[GoogleToken]
}
Run Code Online (Sandbox Code Playgroud)
然后
val collection = db.collectionJSONCollection
val query = Json.obj()
val cursor = collection.find(query).
cursor[GoogleToken](ReadPreference.nearest).
collect[List]()
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
cch*_*tep 21
ReactiveMongo 0.11的最终版本已发布("org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play23").
如更新文档所示,对于默认的BSON/JSON转换,建议使用:import play.modules.reactivemongo.json._, ImplicitBSONHandlers._.
在我的情况下,我insert用JsValue反而不是a来喂ReactiveMongo()JsObject.为了解决这个问题,后面加入import play.modules.reactivemongo.json._,我也不得不改变我隐Writes在OWrites:
从
implicit val myWrites: Writes[A] = new Writes[A] {
def writes(a: A) = Json.obj(...)
Run Code Online (Sandbox Code Playgroud)
至
implicit val myWrites: OWrites[A] = new OWrites[A] { <-- NOTE THE 'O' before 'Writes'
def writes(a: A) = Json.obj(...)
Run Code Online (Sandbox Code Playgroud)
小智 6
我在添加之后计算出:import play.modules.reactivemongo.json._ import play.modules.reactivemongo.json.collection._