Scala Pickling似乎不适用于Point2D.Double



我正在编写一个Scala程序,该程序使用Scala Pickling库来序列化和反序列化Map对象,该对象包含来自java.awt.geom包的StringPoint2D.Double对象。

相关逻辑如下:

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!")
          }
        }
      }

如果您需要查看更多信息,请查看包含违规逻辑的提交

目前,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": "BOTTOMLANEr",
    "_2": {
    }
  },
    {
    "$type": "scala.Tuple2[java.lang.String,java.awt.geom.Point2D.Double]",
    "_1": "UPPERLANESECOND_0r",
    "_2": {
    }
  },
    {
    "$type": "scala.Tuple2[java.lang.String,java.awt.geom.Point2D.Double]",
    "_1": "upperSecondTower_1",
    "_2": {
    }
  },
...
  ]
}

我能做些什么来解决这个问题?

scala-pickling不能直接pickle/unpickle Point2D.Double,因为它没有公共字段(x和y值可以通过getXgetY getter访问)。

Point2D.DoublePickler/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]

相关内容

  • 没有找到相关文章