用扩展java类在scala中解析Json



我有一个java类,如下

public class JobConfig implements Serializable {
private String appName;
//some other params
public JobConfig() {
}
// getters setters 

我正在尝试在scala 中扩展上面的java类

class CsvJobConfig extends JobConfig(){
var delimiter =null
//other attributes

现在在main类中,我试图解析一个json并将其分配回CsvJobConfig。

val is = fileReader.getInputStreamFor(configFileName, configPath, spark)
var jobConfigJson = IOUtils.toString(is, Charset.defaultCharset.name)
val mapper = new ObjectMapper
val config = mapper.readValue(jobConfigJson, classOf[CsvJobConfig]).asInstanceOf[CsvJobConfig]

下面是我试图解析的示例json

{
"appName":"abc",
"delimiter": "\|\|"
}

虽然它在从java尝试时工作得很好,但它在scala 中给了我以下的错误

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "delimiter" (class CsvJobConfig), not marked as ignorable (1 known properties:  ["appName"])
"[truncated 405 chars]; line: 29, column: 17] (through reference chain: CsvJobConfig["delimiter"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:52)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
at com.mastercard.dw.etl.spark.FlatFileProcessor$.delayedEndpoint$com$mastercard$dw$etl$spark$FlatFileProcessor$1(FlatFileProcessor.scala:30)
at com.mastercard.dw.etl.spark.FlatFileProcessor$delayedInit$body.apply(FlatFileProcessor.scala:11)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)

ObjectMapper必须配置为不为FAIL_ON_UNNOWN_PROPERTIES。请参阅下面的代码,并在scala类中分配变量的类型。

class CsvJobConfig extends JobConfig {
var delimiter: String = null
}
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
val config: CsvJobConfig = mapper.readValue("{"appName":"abc", "delimiter": "\\|\\|"}", classOf[CsvJobConfig])
println(config)
println(config.delimiter)

值得尝试

var delimiter: String = null

在代码中,变量分隔符的类型为Null类型,这可能会导致此异常。

添加映射程序.registerModule(DefaultScalaModule(开设案例课解决了这个问题。此外,在scala类中手动添加setter方法可以解决这个问题。

相关内容

  • 没有找到相关文章

最新更新