Scala Gson无法序列化选项[Blob]

时间:2017-01-24 16:24:51

标签: scala gson blob

我设法扩展Gson以处理Scala的OptionBlob类型,但我在合并Option[java.sql.Blob]时遇到问题。

我的测试说

  

[info] - 应该序列化一个选项 - 确定

     

[info] - 应序列化Blob - 确定

     

[info] - 应序列化选项[Blob] * FAILED *

     

[info] {" x":{" buf":[97,115,100,102]," len":4," origLen": 4}}不等于{" x":" asdf"}

你能告诉我这个{"buf": [...]}对象是什么,也可能是为什么Gson会这样做?我的序列化器(下面)肯定告诉他从它做一个String,但是这个对象甚至没有引用。

FWIW,尝试序列化具有真实Blob的Option [Blob]字段的现实对象(数据库行),我甚至得到StackOverflow错误。

以下是我编写序列化程序的方法:

class OptionSerializer extends JsonSerializer[Option[Any]] {
  def serialize(src: Option[Any], typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
    src match {
      case None => JsonNull.INSTANCE
      case Some(v) => context.serialize(v)
    }
  }
}

class BlobSerializer extends JsonSerializer[java.sql.Blob] {
  override def serialize(src: java.sql.Blob, typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
    val s: String = utils.blob2String(v)
    context.serialize(s)
  }
}

class BlobOptionSerializer extends JsonSerializer[Option[java.sql.Blob]] {
  override def serialize(src: Option[java.sql.Blob], typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
    src match {
      case None => JsonNull.INSTANCE
      case Some(v: java.sql.Blob) =>
        val s: String = utils.blob2String(v)
        context.serialize(s)
    }
  }
}

private val builder = new GsonBuilder()
builder.registerTypeAdapter(classOf[java.sql.Blob], new BlobSerializer)
builder.registerTypeAdapter(classOf[Option[java.sql.Blob]], new BlobOptionSerializer)
builder.registerTypeAdapter(classOf[Option[Any]], new OptionSerializer)
val gson = builder.create()

产生上述结果的测试:

case class B(x: Option[Int], y: String)
case class C(x: java.sql.Blob)
case class D(x: Option[java.sql.Blob])

"serialize options" in {
  val b = B(Some(1), "asdf")
  gson.toJson(b) must be ("{\"x\":1,\"y\":\"asdf\"}")
}

"serialize a Blob" in {
  val c = C(utils.string2Blob("asdf"))
  gson.toJson(c) must be ("{\"x\":\"asdf\"}")
}

"serialize an Option[Blob]" in {
  val d = D(Some(utils.string2Blob("asdf")))
  gson.toJson(d) must be ("{\"x\":\"asdf\"}")
}

我和Blob的转换方法,以防万一:

def string2Blob(s: String) = {
  new SerialBlob(s.getBytes)
}

def blob2String(blob: java.sql.Blob): String = {
  if (blob.length == 0)
    return ""  // Array[Byte]()
  val b: Array[Byte] = blob.getBytes(1, blob.length.toInt)
  blob.free
  new String(b)
}

1 个答案:

答案 0 :(得分:0)

在Option [Any]的序列化器中找到解决方案,特殊包装Blob类型:

class OptionSerializer extends JsonSerializer[Option[Any]] {
  def serialize(src: Option[Any], typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
    src match {
      case None => JsonNull.INSTANCE
      case Some(b: java.sql.Blob) => context.serialize(utils.Sql.blob2String(b))
      case Some(v) => context.serialize(v)
    }
  }
}