问题
I am writing a RESTful interface and I would like to marshall and unmarshall JSON ready for Ember Data. The wrinkle is that Ember Data wants the entity name and the two libraries I've tried, spray-json and json4s, don't appear to do this easily.
Desired Ember Data format
{
"coursePhoto": {
"photoId": 1
}
}
Current default format:
{"photoId":15}
This should come from a case class:
case class CoursePhoto(photoId: Long)
I did get it running with the following custom code:
object PtolemyJsonProtocol extends DefaultJsonProtocol {
implicit object CoursePhotoFormat extends RootJsonFormat[CoursePhoto] {
def write(cp: CoursePhoto) =
JsObject("CoursePhoto" -> JsObject("photoId" -> JsNumber(cp.photoId)))
def read(value: JsValue) = value match {
case coursePhotoJsObject: JsObject => {
CoursePhoto(coursePhotoJsObject.getFields("CoursePhoto")(0).asJsObject
.getFields("photos")(0).asInstanceOf[JsArray].elements(0)
.asInstanceOf[JsNumber].value.toLong)
}
case _ => deserializationError("CoursePhoto expected")
}
}
That code seems horrifyingly fragile and ugly with all the asInstanceOf
and (0)
.
Given that I'm writing in Spray with Scala what's the nice way to get named root JSON output? I am quite happy to do this with any JSON library that integrates nicely with Spray and is reasonably performant.
回答1:
Is the following solving your problem?
scala> import spray.json._
import spray.json._
scala> import DefaultJsonProtocol._
import DefaultJsonProtocol._
scala> case class CoursePhoto(photoId: Long)
defined class CoursePhoto
scala> case class CoursePhotoEmber(coursePhoto: CoursePhoto)
defined class CoursePhotoEmber
scala> implicit val jsonFormatCoursePhoto = jsonFormat1(CoursePhoto)
jsonFormatCoursePhoto: spray.json.RootJsonFormat[CoursePhoto] = spray.json.ProductFormatsInstances$$anon$1@6f5d66b6
scala> implicit val jsonFormatCoursePhotoEmber = jsonFormat1(CoursePhotoEmber)
jsonFormatCoursePhotoEmber: spray.json.RootJsonFormat[CoursePhotoEmber] = spray.json.ProductFormatsInstances$$anon$1@401a0d22
scala> """{ "coursePhoto": { "photoId": 1 } }""".parseJson.convertTo[CoursePhotoEmber]
res0: CoursePhotoEmber = CoursePhotoEmber(CoursePhoto(1))
scala> res0.toJson
res1: spray.json.JsValue = {"coursePhoto":{"photoId":1}}
回答2:
This problem made me wonder if it were possible to do it in a re-usable way. I believe I've figured out a reasonable way to do this for multiple types.
object PtolemyJsonProtocol extends DefaultJsonProtocol {
implicit val CoursePhotoFormat = new NamedRootFormat("CoursePhoto", jsonFormat1(CoursePhoto))
}
import PtolemyJsonProtocol._
class NamedRootFormat[T](rootName: String, delegate: RootJsonFormat[T]) extends RootJsonFormat[T] {
def write(obj: T): JsValue = {
JsObject((rootName, delegate.write(obj)))
}
def read(json: JsValue): T = json match {
case parentObject: JsObject => {
delegate.read(parentObject.getFields(rootName).head)
}
case _ => deserializationError("CoursePhoto expected")
}
}
来源:https://stackoverflow.com/questions/24097871/unmarshall-json-with-named-root-for-ember-data-using-scala-case-class-on-spray