使用 Scalatra 序列化 Java 和 Scala 对象
Serialisation of Java and Scala objects with Scalatra
我继承了一个提供 REST API 的遗留 Scalatra 应用程序。如果 returned 对象是一个 case class 构建在其他 case classes 上,那么 returned 对象的序列化工作完美。但是如果 return 从 Java 或 Scala class 创建的对象,它不会被 Scalatra 序列化。我只会得到 Object.toString() 的结果。那么我需要做什么才能正确序列化非大小写 classes?
这是我的class
class Snafu(sna: String, foo: String) {
}
这是我的 servlet:
class HealthServlet(implicit inj: Injector)
extends ScalatraServlet with SLF4JLogging
with JacksonJsonSupport
with Injectable with InternalViaLocalhostOnlySupport {
protected implicit val jsonFormats: Formats = DefaultFormats
val healthStateCheck = inject[HealthStateCheck]
before("/") {
}
get("/") {
Ok(new Snafu("4", "2"))
}
}
json4s 默认不支持非大小写 class 序列化。您需要为 classes 添加一个 CustomSerializer。
class IntervalSerializer extends CustomSerializer[Interval](format => (
{
// Deserialize
case JObject(JField("start", JInt(s)) :: JField("end", JInt(e)) :: Nil) =>
new Interval(s.longValue, e.longValue)
},
{
// Serialize
case x: Interval =>
JObject(JField("start", JInt(BigInt(x.startTime))) ::
JField("end", JInt(BigInt(x.endTime))) :: Nil)
}
))
您还需要将这些序列化器添加到正在使用的json格式中。
protected implicit lazy val jsonFormats: Formats = DefaultFormats + FieldSerializer[Interval]()
这是 json4s 文档中的示例,该示例已修改为显示一个工作 servlet 从常规 class.
返回序列化 json
import org.json4s._
import org.json4s.JsonAST.{JInt, JField, JObject}
import org.scalatra.json.JacksonJsonSupport
class Interval(start: Long, end: Long) {
val startTime = start
val endTime = end
}
class IntervalSerializer extends CustomSerializer[Interval](format => (
{
// Deserialize
case JObject(JField("start", JInt(s)) :: JField("end", JInt(e)) :: Nil) =>
new Interval(s.longValue, e.longValue)
},
{
// Serialize
case x: Interval =>
JObject(JField("start", JInt(BigInt(x.startTime))) ::
JField("end", JInt(BigInt(x.endTime))) :: Nil)
}
))
class IntervalServlet extends ScalatraServlet with ScalateSupport with JacksonJsonSupport {
get("/intervalsample") {
contentType = "application/json"
val interval = new Interval(1, 2)
Extraction.decompose(interval)
}
protected implicit lazy val jsonFormats: Formats = DefaultFormats + FieldSerializer[Interval]()
}
我继承了一个提供 REST API 的遗留 Scalatra 应用程序。如果 returned 对象是一个 case class 构建在其他 case classes 上,那么 returned 对象的序列化工作完美。但是如果 return 从 Java 或 Scala class 创建的对象,它不会被 Scalatra 序列化。我只会得到 Object.toString() 的结果。那么我需要做什么才能正确序列化非大小写 classes?
这是我的class
class Snafu(sna: String, foo: String) {
}
这是我的 servlet:
class HealthServlet(implicit inj: Injector)
extends ScalatraServlet with SLF4JLogging
with JacksonJsonSupport
with Injectable with InternalViaLocalhostOnlySupport {
protected implicit val jsonFormats: Formats = DefaultFormats
val healthStateCheck = inject[HealthStateCheck]
before("/") {
}
get("/") {
Ok(new Snafu("4", "2"))
}
}
json4s 默认不支持非大小写 class 序列化。您需要为 classes 添加一个 CustomSerializer。
class IntervalSerializer extends CustomSerializer[Interval](format => (
{
// Deserialize
case JObject(JField("start", JInt(s)) :: JField("end", JInt(e)) :: Nil) =>
new Interval(s.longValue, e.longValue)
},
{
// Serialize
case x: Interval =>
JObject(JField("start", JInt(BigInt(x.startTime))) ::
JField("end", JInt(BigInt(x.endTime))) :: Nil)
}
))
您还需要将这些序列化器添加到正在使用的json格式中。
protected implicit lazy val jsonFormats: Formats = DefaultFormats + FieldSerializer[Interval]()
这是 json4s 文档中的示例,该示例已修改为显示一个工作 servlet 从常规 class.
返回序列化 jsonimport org.json4s._
import org.json4s.JsonAST.{JInt, JField, JObject}
import org.scalatra.json.JacksonJsonSupport
class Interval(start: Long, end: Long) {
val startTime = start
val endTime = end
}
class IntervalSerializer extends CustomSerializer[Interval](format => (
{
// Deserialize
case JObject(JField("start", JInt(s)) :: JField("end", JInt(e)) :: Nil) =>
new Interval(s.longValue, e.longValue)
},
{
// Serialize
case x: Interval =>
JObject(JField("start", JInt(BigInt(x.startTime))) ::
JField("end", JInt(BigInt(x.endTime))) :: Nil)
}
))
class IntervalServlet extends ScalatraServlet with ScalateSupport with JacksonJsonSupport {
get("/intervalsample") {
contentType = "application/json"
val interval = new Interval(1, 2)
Extraction.decompose(interval)
}
protected implicit lazy val jsonFormats: Formats = DefaultFormats + FieldSerializer[Interval]()
}