Created
May 4, 2014 19:44
-
-
Save jdaily/c26cd5840937fcab21db to your computer and use it in GitHub Desktop.
Play-Mongo GeoPoint implicit read/write
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jdaily | |
import play.api.Play | |
import play.api.Play.current | |
import play.api.libs.json._ | |
import play.api.libs.json.Reads._ | |
import play.api.libs.functional.syntax._ | |
/** | |
* Created by jdaily on 1/27/14. | |
*/ | |
case class MongoGeoPoint(Longitude: Double, Latitude: Double) | |
object MongoGeoPoint { | |
val pointWrites = Writes[MongoGeoPoint]( p => Json.obj("type" -> "Point", | |
"coordinates" -> Json.arr(JsNumber(p.Longitude), JsNumber(p.Latitude)))) | |
val pointReads = (__ \ 'type).read[String](constraints.verifying[String](_ == "Point")) andKeep | |
(__ \ 'coordinates).read[MongoGeoPoint](minLength[List[Double]](2).map(l => MongoGeoPoint(l(0), l(1)))) | |
// Conversions | |
implicit val pointFormat = Format(pointReads, pointWrites) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment