1
我用scala的pickle序列化玩了一下,所以我编写了一个Netty Handler,但是当我尝试使用scalac编译文件时,由于类型不匹配而导致错误,我无法解释我斯卡拉酱菜类型不匹配
import java.util
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.{MessageToMessageDecoder, MessageToMessageEncoder}
import scala.pickling._
import scala.pickling.binary._
import scala.pickling.json._
class PickleEncoder(format: PickleFormat = scala.pickling.binary.pickleFormat) extends MessageToMessageEncoder[AnyRef] {
implicit val pickleFormat = format
override def encode(implicit ctx: ChannelHandlerContext, msg: AnyRef, out: util.List[AnyRef]): Unit = {
val pickled: Pickle = msg.pickle
val bytes = pickled.value match {
case bytes: Array[Byte] => bytes
case json: String => json.getBytes("UTF-8")
case _ => throw PicklingException("Unable to detect pickle format")
}
out.add(bytes.toByteBuf)
}
}
class PickleDecoder(format: PickleFormat = scala.pickling.binary.pickleFormat) extends MessageToMessageDecoder[ByteBuf] {
implicit val pickleFormat = format
override def decode(ctx: ChannelHandlerContext, msg: ByteBuf, out: util.List[AnyRef]) = {
val bytes: Array[Byte] = msg.toBytes
val data = format match {
case scala.pickling.binary.pickleFormat => bytes.unpickle[AnyRef]
case scala.pickling.json.pickleFormat => new String(bytes, "UTF-8").unpickle[AnyRef]
case _ => throw PicklingException("Unable to detect pickle format")
}
out.add(data)
}
}
编译错误:
Error:(30, 31) type mismatch; found : scala.pickling.Pickle
required: PickleEncoder.this.pickleFormat.PickleType
val pickled: Pickle = msg.pickle
任何想法的问题是什么? ^