3
votes

Using spray-json (as I'm using spray-client) in order to get a latitude,longitude object from the google maps API I need to have the whole response structure set up:

case class AddrComponent(long_name: String, short_name: String, types: List[String])
case class Location(lat: Double, lng: Double)
case class ViewPort(northeast: Location, southwest: Location)
case class Geometry(location: Location, location_type: String, viewport: ViewPort)
case class EachResult(address_components: List[AddrComponent],
                      formatted_address: String,
                      geometry: Geometry,
                      types: List[String])
case class GoogleApiResult[T](status: String, results: List[T])

object AddressProtocol extends DefaultJsonProtocol {
    implicit val addrFormat = jsonFormat3(AddrComponent)
    implicit val locFormat = jsonFormat2(Location)
    implicit val viewPortFormat = jsonFormat2(ViewPort)
    implicit val geomFormat = jsonFormat3(Geometry)
    implicit val eachResFormat = jsonFormat4(EachResult)
    implicit def GoogleApiFormat[T: JsonFormat] = jsonFormat2(GoogleApiResult.apply[T])
}
import AddressProtocol._

Is there any way I can just get Location from the json in the response and avoid all this gumph?

The spray-client code:

implicit val system = ActorSystem("test-system")
import system.dispatcher

private val pipeline = sendReceive ~> unmarshal[GoogleApiResult[EachResult]]

def getPostcode(postcode: String): Point = {
    val url = s"http://maps.googleapis.com/maps/api/geocode/json?address=$postcode,+UK&sensor=true"
    val future = pipeline(Get(url))
    val result = Await.result(future, 10 seconds)
    result.results.size match {
        case 0 => throw new PostcodeNotFoundException(postcode)
        case x if x > 1 => throw new MultipleResultsException(postcode)
        case _ => {
            val location = result.results(0).geometry.location
            new Point(location.lng, location.lat)
        }
    }
}

Or alternatively how can I use jackson with spray-client?

1
You could try github.com/jrudolph/json-lenses which allows you to extract data from a JSON ast quite easily.jrudolph
Awesome, took a bit of fiddling but it looks great now. Is that going to be merged into spray-json? It should be!!!shmish111
Merging it with spray-json is the plan but we've currently got no capacity to do the move.jrudolph

1 Answers

2
votes

Following jrudolph's advice to json-lenses I also got in quite a bit of fiddling but finally got things to work. I found it quite difficult (as a newbie) and also I am sure this solution is far from the most elegant - nevertheless I think this might help people or inspire others for improvements.

Given JSON:

{
    "status": 200,
    "code": 0,
    "message": "",
    "payload": {
        "statuses": {
            "emailConfirmation": "PENDING",
            "phoneConfirmation": "DONE",
        }
    }
}

And case class for unmarshalling statuses only:

case class UserStatus(emailConfirmation: String, phoneConfirmation: String)

One can do this to unmarshal response:

import scala.concurrent.Future
import spray.http.HttpResponse
import spray.httpx.unmarshalling.{FromResponseUnmarshaller, MalformedContent}
import spray.json.DefaultJsonProtocol
import spray.json.lenses.JsonLenses._
import spray.client.pipelining._

object UserStatusJsonProtocol extends DefaultJsonProtocol {
  implicit val userStatusUnmarshaller = new FromResponseUnmarshaller[UserStatus] {
    implicit val userStatusJsonFormat = jsonFormat2(UserStatus)
    def apply(response: HttpResponse) = try {
      Right(response.entity.asString.extract[UserStatus]('payload / 'statuses))
    } catch { case x: Throwable =>
      Left(MalformedContent("Could not unmarshal user status.", x))
    }
  }
}
import UserStatusJsonProtocol._

def userStatus(userId: String): Future[UserStatus] = {
  val pipeline = sendReceive ~> unmarshal[UserStatus]
  pipeline(Get(s"/api/user/${userId}/status"))
}