The problem you are facing is related to a mixup between JSONCollection
and BSONCollection
.
BSONCollection
is the default collection reactivemongo uses. This implementation needs an implementation of a BSONDocumentWriter
and a BSONReader
for a case class to get (de-)serialised.
JSONCollection
on the other hand is the default collection implementation that the play-reactive module uses. Since you defined the collection to be a JSONCollection
in db.collection[JSONCollection]("test")
you need to provide an implicit json format.
The json format you provide is
implicit val pointFormat = Json.format[Point]
This will serialize an object to the following format
{
"lon": -0.0015,
"lat": 51.0015
}
If you want to serialise your Point
to an array you need to replace the above implicit pointFormat
:
import play.api.libs.json._
import play.api.libs.json.Reads._
case class Point(lng: Double, lat: Double)
object Point {
val pointWrites = Writes[Point]( p => Json.toJson(List(p.lng, p.lat)))
val pointReads = minLength[List[Double]](2).map(l => Point(l(0), l(1)))
implicit val pointFormat = Format(pointReads, pointWrites)
}
You actualy don't need the BSONReader
and BSONDocumentWriter
.
Edit:
Here is a reads that also validates the type
attribute of the document:
val pointReads =
(__ \ 'type).read[String](constraints.verifying[String](_ == "Point")) andKeep
(__ \ 'coordinates).read[Point](minLength[List[Double]](2).map(l => Point(l(0), l(1))))