Need json deserializer for 3rd

I'm implementing a SecureSocial service for my Scala Play! application. I'm using the ReactiveMongoPlugin to access the MongoDB store. Here is the code...

lazy val users:  JSONCollection = ReactiveMongoPlugin.db.collection[JSONCollection]("users")

def find(providerId: String, userId: String): Future[Option[BasicProfile]] = {
  users
    .find(Json.arr(Json.obj("providerId" -> providerId), Json.obj("userId" -> userId)))
    .cursor[BasicProfile]
    .headOption
}

BasicProfile is a SecureSocial case class that does not implement the json serializer/deserializer. So predictably I'm getting...

No Json deserializer found for type securesocial.core.BasicProfile

I know how to implement reads/writes for my own case classes but I'm too new to Scala to know how to do this for library case classes like BasicProfile. How can I add json read/write to BasicProfile?


Adding Reads and Writes to library case classes is really similar to adding them to your own classes. You will need to create a Read and a Write for each library case class. For example, let's say we had these case classes:

case class Example(a: String, b: ExampleB)
case class ExampleB(c: Int)

They should be parsed using these:

implicit val exampleWrites: Writes[Example] = (
  (JsPath  "a").write[String] and
  (JsPath  "b").write[ExampleB]
)

implicit val exampleBWrites: Writes[ExampleB] = (
  (JsPath  "c").write[Int]
)

implicit val exampleReads: Reads[Example] = (
  (JsPath  "a").read[String] and
  (JsPath  "b").read[ExampleB]
)

implicit val exampleBReads: Reads[ExampleB] = (
  (JsPath  "c").read[Int]
)

Since these Reads and Writes are implicit, importing them should automatically make them work where you need them.

See the documentation for more information.

链接地址: http://www.djcxy.com/p/68276.html

上一篇: 将json解析为scala case类时出错

下一篇: 需要json解串器3