Scala Pickling 似乎不适用于 Point2D.Double

Scala Pickling doesn't seem to work with Point2D.Double

我正在开发一个 Scala 程序,该程序使用 Scala Pickling library 序列化和反序列化一个 Map 对象,该对象包含一个 String 和一个 Point2D.Double 对象java.awt.geom 包。

相关逻辑如下:

contents +=
      new Button("Save Config") {
        reactions += {
          case ButtonClicked(_) => {
            var m: Map[String, Point2D.Double] = Map()
            nodeFields.foreach(x => {
              m += (x._1 -> new Point2D.Double(x._2._1.text.toDouble, x._2._2.text.toDouble))
            })
            val pkl = m.pickle
            fc.showSaveDialog(null)
            val outputFile = fc.selectedFile
            val writer = new PrintWriter(outputFile)
            writer.write(pkl.value)
            writer.close()
            Dialog.showMessage(null, "Success!")
          }
        }
      }

如需查看更多,here's the commit with the offending logic

就目前而言,pkl.value 输出的 JSON 格式化字符串是一个有效的序列化 Map[String, Point2D.Double],只是 Point2D.Double 的值被删除了!

这是输出的一个片段:

{
  "$type": "scala.collection.mutable.Map[java.lang.String,java.awt.geom.Point2D.Double]",
  "elems": [
    {
    "$type": "scala.Tuple2[java.lang.String,java.awt.geom.Point2D.Double]",
    "_1": "BOTTOMLANE\r",
    "_2": {

    }
  },
    {
    "$type": "scala.Tuple2[java.lang.String,java.awt.geom.Point2D.Double]",
    "_1": "UPPERLANESECOND_0\r",
    "_2": {

    }
  },
    {
    "$type": "scala.Tuple2[java.lang.String,java.awt.geom.Point2D.Double]",
    "_1": "upperSecondTower_1",
    "_2": {

    }
  },
...
  ]
}

我该怎么做才能解决这个问题?

scala-pickling 不能直接 pickle/unpickle Point2D.Double 因为它没有 public 字段(x 和 y 值可以通过 getXgetY吸气剂)。

对于 Point2D.Double 可能的 Pickler / Unpickler 将是:

object Point2DPickler { 
  import scala.pickling._
  import scala.pickling.Defaults._
  import java.awt.geom.Point2D

  type DoublePoint = java.awt.geom.Point2D.Double
  implicit object Point2DDoublePickle extends Pickler[DoublePoint] with Unpickler[DoublePoint] {
    private val doubleUnpickler = implicitly[Unpickler[Double]]    

    override def tag = FastTypeTag[java.awt.geom.Point2D.Double]

    override def pickle(point: DoublePoint, builder: PBuilder) = {
      builder.beginEntry(point)
      builder.putField("x",
        b => b.hintTag(FastTypeTag.Double).beginEntry(point.getX).endEntry()
      )
      builder.putField("y",
        b => b.hintTag(FastTypeTag.Double).beginEntry(point.getY).endEntry()
      )
      builder.endEntry()
    }

    override def unpickle(tag: String, reader: PReader): DoublePoint = {
      val x = doubleUnpickler.unpickleEntry(reader.readField("x")).asInstanceOf[Double]
      val y = doubleUnpickler.unpickleEntry(reader.readField("y")).asInstanceOf[Double]
      new Point2D.Double(x, y)
    }
  }
}

可以用作:

import scala.pickling.Defaults._
import scala.pickling.json._
import java.awt.geom.Point2D

import Point2DPickler._

val dpoint = new Point2D.Double(1d, 2d)

scala> val json = dpoint.pickle
json: pickling.json.pickleFormat.PickleType =
JSONPickle({
  "$type": "java.awt.geom.Point2D.Double",
  "x": {
    "$type": "scala.Double",
    "value": 1.0
  },
  "y": {
    "$type": "scala.Double",
    "value": 2.0
  }
})

scala> val dpoint2 = json.value.unpickle[java.awt.geom.Point2D.Double]
dpoint2: java.awt.geom.Point2D.Double = Point2D.Double[1.0, 2.0]