我使用杰克逊 Scala 模块。
我创建了一个小序列化工具来处理 Play2 框架接收到的 json 有效负载。
def unserializePayloadAs[T](implicit requestContext: RequestContext[JsValue]): T = {
val json: String = Json.stringify(requestContext.request.body)
unserialize(json)
}
def unserialize[T](json: String): T = {
objectMapper.readValue(json)
}
Jackson Scala 模块的 readValue 具有签名:
def readValue[T: Manifest](content: String): T = {
readValue(content, constructType[T])
}
尝试使用我的反序列化代码时,我有一个堆栈。
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate abstract type [simple type, class scala.runtime.Nothing$] (need to add/enable type information?)
at [Source: java.io.StringReader@7bb78579; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) ~[jackson-databind-2.2.0.jar:2.2.0]
at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:77) ~[jackson-databind-2.2.0.jar:2.2.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) ~[jackson-databind-2.2.0.jar:2.2.0]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888) ~[jackson-databind-2.2.0.jar:2.2.0]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041) ~[jackson-databind-2.2.0.jar:2.2.0]
at com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper$class.readValue(ScalaObjectMapper.scala:157) ~[jackson-module-scala_2.10-2.2.0.jar:2.2.0]
at utils.CustomSerializer$$anon$1.readValue(CustomSerializer.scala:17) ~[na:na]
at utils.CustomSerializer$.unserialize(CustomSerializer.scala:39) ~[na:na]
at utils.CustomSerializer$.unserializePayloadAs(CustomSerializer.scala:35) ~[na:na]
正如预期的那样,当我在代码中添加清单时它工作正常:
def unserializePayloadAs[T: Manifest](implicit requestContext: RequestContext[JsValue]): T = {
val json: String = Json.stringify(requestContext.request.body)
unserialize(json)
}
def unserialize[T: Manifest](json: String): T = {
objectMapper.readValue(json)
}
有人可以解释那里发生了什么吗?当我们调用具有Manifest
上下文绑定的方法时,使用带有 no 的参数化方法Manifest
,那么 ManifestNothing
会提供给第一个方法吗?
我本来预计会出现某种编译错误,告诉我我正在readValue
使用没有Manifest
或类似的参数化类型调用 ,这似乎更快失败。