如何创建告诉Jackson ObjectMapper来创建泛型类型?

时间:2017-03-03 04:53:24

标签: scala generics jackson apache-kafka objectmapper

对我来说,与卡夫卡和斯卡拉一起玩这是一个好玩的练习。我的目标是创建一个简单的消息类型来发送kafka主题。这是我尝试使用类型参数[A]的通用/可重用串行器。

import java.util.{Map => jMap}
import scala.reflect.runtime.universe._    
import org.apache.kafka.common.serialization.{Deserializer, Serializer}   
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.DeserializationFeature._    
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule    

class MySerializer[A : TypeTag]() extends Serializer[A] with Deserializer[A] {

  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)

  override def close() = {/*Do Nothing*/}
  override def configure(configs: jMap[String, _], isKey: Boolean) = {/*Do Nothing*/}
  override def serialize(topic: String, subject: A): Array[Byte] =
    mapper.writeValueAsBytes(subject)
  override def deserialize(topic: String, bytes: Array[Byte]): A = {
    val a: A = mapper.readValue(bytes, A.getClass()) /******PROBLEM****/
    return a
  }
}

我反序列化的错误是ObjectMapper.readValue的第二个参数。我该怎么做才能让它返回一个通用类型A?

我的sbt:

name := "scalafunplay"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "org.apache.kafka" % "kafka_2.10" % "0.10.2.0",
  "com.fasterxml.jackson.core" % "jackson-databind" % "2.8.7",
  "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.7"
)

这是我的主要应用:

package scalafunplay

object Mistkafer {

  def main(args: Array[String]): Unit = {


    case class Asset (ruid: String)

    val test = new Asset("Dan The Man")

    val serializer = new MySerializer[Asset]()

    val sampleSerialized = serializer.serialize("test", test)
    val sampleUnserialized = serializer.deserialize("test", test)

    println("###### RESULT: " + sampleUnserialized)


  }

}

1 个答案:

答案 0 :(得分:0)

毕竟我决定不使用杰克逊。使用Java.IO字节数组和对象输入/输出流比较容易:

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
import java.util.{Map => jMap}
import scala.reflect.runtime.universe.TypeTag
import org.apache.kafka.common.serialization.{Deserializer, Serializer}

class ObjectSerializer[A : TypeTag]() extends Serializer[A] with Deserializer[A] {

  override def close() = {/*Do Nothing*/}

  override def configure(configs: jMap[String, _], isKey: Boolean) = {/*Do Nothing*/}

  override def serialize(topic: String, subject: A): Array[Byte] = {
    val byteArrInStr = new ByteArrayOutputStream()
    val objInpStr = new ObjectOutputStream(byteArrInStr)
    objInpStr.writeObject(subject)
    byteArrInStr.toByteArray()
  }

  override def deserialize(topic: String, bytes: Array[Byte]): A = {
    val byteArrInStr = new ByteArrayInputStream(bytes)
    val objInStr = new ObjectInputStream(byteArrInStr)
    objInStr.readObject().asInstanceOf[A]
  }
}