从给定的 json 字符串和大小写类型 class 将 json 字符串转换为大小写 class 对象

convert json string to case class object from given json string and type of case class

要求是在给定 json 字符串和大小写类型 class.

的 scala 中将 json 字符串转换为大小写 class 对象

我尝试了 Gson 和 jackson 库,但无法解决给定的要求。

package eg.json

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.gson.Gson
import com.typesafe.scalalogging.LazyLogging

case class Person(name : String, age : Int)

case class Address(street : String, buildingNumber : Int, zipCode : Int)

case class Rent(amount : Double, month : String)

//there are many other case classes

object JsonToObject extends LazyLogging{

  import logger._

  def toJsonString(ref : Any) : String = {
    val gson = new Gson()
    val jsonString = gson.toJson(ref)
    jsonString
  }

  def main(args: Array[String]): Unit = {
    val person = Person("John", 35)
    val jsonString = toJsonString(person)

    //here requirement is to convert json string to case class instance, provided the type of case class instance

    val gsonObj = toInstanceUsingGson( jsonString, Person.getClass )
    debug(s"main : object deserialized using gson : $gsonObj")

    val jacksonObj = toInstanceUsingJackson( jsonString, Person.getClass )
    debug(s"main : object deserialized using gson : $jacksonObj")

  }

  def toInstanceUsingGson[T](jsonString : String, caseClassType : Class[T]) : T = {

    val gson = new Gson()
    val ref = gson.fromJson(jsonString, caseClassType)
    ref
  }

  def toInstanceUsingJackson[T](jsonString : String, caseClassType : Class[T]) : T = {

    val mapper = new ObjectMapper()
    val ref = mapper.readValue(jsonString, caseClassType)
    ref
  }
}

以上代码的执行输出为:-

01:32:52.369 [main] DEBUG eg.json.JsonToObject$ - main : object deserialized using gson : Person
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "name" (class eg.json.Person$), not marked as ignorable (0 known properties: ])
 at [Source: (String)"{"name":"John","age":35}"; line: 1, column: 10] (through reference chain: eg.json.Person$["name"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at eg.json.JsonToObject$.toInstanceUsingJackson(JsonToObject.scala:49)
    at eg.json.JsonToObject$.main(JsonToObject.scala:34)
    at eg.json.JsonToObject.main(JsonToObject.scala)

请提出建议,如何使用 gson 或 jackson 实现此目的,或者建议其他带有示例的库。

上面的简化问题在 github :-

https://github.com/moglideveloper/JsonToScalaObject

尝试使用 Cats 的 circe。

  • 将 circe 添加到您的项目(https://circe.github.io/circe/ - 快速入门)。
  • 创建一个案例 class,代表您要从 json 构建的内容。
  • 声明解码器

https://circe.github.io/circe/codecs/semiauto-derivation.html https://github.com/circe/circe

import io.circe.parser.decode
import io.circe.syntax._

case class DataToDecode(name : String,
                        age : Int,
                        street : String,
                        buildingNumber : Int,
                        zipCode : Int, 
                        amount : Double, 
                        month : String)
object DataToDecode{
implicit val dataToDecode: Decoder[DataToDecode] = deriveDecoder

def decodeData(data: Json) : DataToDecode {
  data.as[DataToDecode].right.get
  }
}

很好的例子here

使用 Jackson,您可以这样做:

import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

val mapper = new ObjectMapper() with ScalaObjectMapper
//this line my be needed depending on your case classes
mapper.registerModule(DefaultScalaModule)

def fromJson[T](json: String)(implicit m: Manifest[T]): T = {
    mapper.readValue[T](json)
  }

我认为 Jackson lib 真的很干净。 用法是这样的:

val json: String = ???
val personObject: Person = fromJson[Person](json)