斯卡拉·杰克逊(Scala Jackson)的反序列化失败,“非静态内部类”错误版本Jackson2.10

发布于 2025-01-28 11:37:44 字数 2282 浏览 2 评论 0原文

我试图从杰克逊-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文