简体   繁体   English

Scala jackson 反序列化

[英]Scala jackson deserialize

I want to deserialize json with jackson in scala我想用 scala 中的 jackson 反序列化 json

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator; 



case class Metadata(@JsonProperty("hive_type_string") hive_type_string: String)

case class Field(@JsonProperty("name") name: String, @JsonProperty("type") typeField: String, @JsonProperty("nullable") nullable: Boolean, @JsonProperty("metadata") metadata: Metadata) 

case class StructTable(@JsonProperty("type") typeField: String, @JsonProperty("fields") fields: Seq[Field])


val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
val struct = mapper.readValue[StructTable](json_struct)

json_struct: json_struct:

{ "type":"struct", "fields":[ { "name":"code_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } }, { "name":"libelle_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } } ] } { "type":"struct", "fields":[ { "name":"code_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } }, { "name":"libelle_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } } ] }

This is the error: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of StructTable : non-static inner classes like this can only by instantiated using default, no-argument constructor这是错误: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造StructTable的实例:像这样的非静态内部类只能通过使用默认的无参数构造函数进行实例化

Can someone help me?有人能帮我吗?

Thanks.谢谢。

If you can pick another library like jsoniter-scala then add to your dependencies:如果您可以选择另一个库,例如jsoniter-scala ,然后添加到您的依赖项中:

libraryDependencies ++= Seq(
  // Use the %%% operator instead of %% for Scala.js  
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core"   % "2.6.2",
  // Use the "provided" scope instead when the "compile-internal" scope is not supported in your build tool
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.6.2" % "compile-internal"
)

Here is a code snippet to parse your input and print parsed data:这是解析输入并打印解析数据的代码片段:

import com.github.plokhotnyuk.jsoniter_scala.macros._
import com.github.plokhotnyuk.jsoniter_scala.core._

case class Metadata(@named("HIVE_TYPE_STRING") hive_type_string: String)

case class Field(name: String, @named("type") typeField: String, nullable: Boolean, metadata: Metadata)

case class StructTable(@named("type") typeField: String, fields: Seq[Field])

implicit val codec: JsonValueCodec[StructTable] = JsonCodecMaker.make

val json_struct = """{ "type":"struct", "fields":[ { "name":"code_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } }, { "name":"libelle_role", "type":"string", "nullable":true, "metadata":{ "HIVE_TYPE_STRING":"string" } } ] }""";
val struct = readFromString(json_struct)

println(struct)

The expected output is:预期的 output 为:

StructTable(struct,List(Field(code_role,string,true,Metadata(string)), Field(libelle_role,string,true,Metadata(string))))

Also you can run it online with Scastie here .您也可以在此处使用 Scastie 在线运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM