斯卡拉·杰克逊(Scala Jackson)的反序列化失败,“非静态内部类”错误版本Jackson2.10
我试图从杰克逊-2.5升级到2.10
下面的避难代码对我有效,但在升级后,解决方案失败了以下错误:
[ Cannot construct instance of `$line11.$read$$iw$$iw$RegularMetric$1` (although at least one Creator exists): non-static inner classes like this can only by instantiated using default, no-argument constructor
at [Source: (String)".... (through reference chain: com.fasterxml.jackson.module.scala.deser.GenericFactoryDeserializerResolver$BuilderWrapper[0]) ]
metricsjson是一个有效的JSON,以前工作正常。
添加代码片段供参考。
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
case class ThresholdExpression(@JsonProperty("term") term: String,
@JsonProperty("comparisonOperator") comparisonOperator: String,
@JsonProperty("thresholdValue") thresholdValue: String,
@JsonProperty("prorated") prorated: Boolean
){
def getThreshold(proratedFactor : Double = 1): String = {
..
..
}
}
case class RegularMetric(@JsonProperty("name") name: String,
@JsonProperty("premiumPlusThreshold") premiumPlusThreshold: List[ThresholdExpression],
@JsonProperty("premiumThreshold") premiumThreshold: List[ThresholdExpression],
@JsonProperty("standardThreshold") standardThreshold: List[ThresholdExpression]){
def getMetricTierColumnName() : String = {
s"${name}Metric"
}
def getMetricRateColumnName() : String = {
s"${name}"
}
}
object Classification {
private def getRegularMetrics(metricsConfigJson: String): List[RegularMetric] = {
JsonUtils.fromJson[List[RegularMetric]](metricsJson)
}
object JsonUtils {
def fromJson[T](json: String)(implicit m : Manifest[T]): T = {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.readValue[T](json)
}
}
}
在Deep Dive上,我发现有了Jackson-Databind 2.10,就可以实现一些紧密的检查,因为我的复杂对象是由列表组成的,但该错误是有点误导的,我无法弄清楚什么是什么我在这里做错了。
I am trying to upgrade from Jackson-2.5 to 2.10
Below deserialization code was working for me, before but post upgrade the solution is failing with following error:
[ Cannot construct instance of `$line11.$read$iw$iw$RegularMetric$1` (although at least one Creator exists): non-static inner classes like this can only by instantiated using default, no-argument constructor
at [Source: (String)".... (through reference chain: com.fasterxml.jackson.module.scala.deser.GenericFactoryDeserializerResolver$BuilderWrapper[0]) ]
metricsJson is a valid Json which was working fine before.
Adding code snippet for reference.
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
case class ThresholdExpression(@JsonProperty("term") term: String,
@JsonProperty("comparisonOperator") comparisonOperator: String,
@JsonProperty("thresholdValue") thresholdValue: String,
@JsonProperty("prorated") prorated: Boolean
){
def getThreshold(proratedFactor : Double = 1): String = {
..
..
}
}
case class RegularMetric(@JsonProperty("name") name: String,
@JsonProperty("premiumPlusThreshold") premiumPlusThreshold: List[ThresholdExpression],
@JsonProperty("premiumThreshold") premiumThreshold: List[ThresholdExpression],
@JsonProperty("standardThreshold") standardThreshold: List[ThresholdExpression]){
def getMetricTierColumnName() : String = {
s"${name}Metric"
}
def getMetricRateColumnName() : String = {
s"${name}"
}
}
object Classification {
private def getRegularMetrics(metricsConfigJson: String): List[RegularMetric] = {
JsonUtils.fromJson[List[RegularMetric]](metricsJson)
}
object JsonUtils {
def fromJson[T](json: String)(implicit m : Manifest[T]): T = {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.readValue[T](json)
}
}
}
On deep dive I found that with Jackson-databind 2.10 there are some tight type check implemented because of which my complex object, which consists of lists is not being deserialized, but the error is a bit misleading and I am not able to figure what am I doing wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论