lift-json を使用して json 配列を scala vector に逆シリアル化するにはどうすればよいですか?
例えば:
case class Foo(bar: Vector[Bar])
trait Bar {
def value: Int
}
case class Bar1(value: Int) extends Bar
case class Bar2(value: Int) extends Bar
import net.liftweb.json.{ShortTypeHints, Serialization, DefaultFormats}
implicit val formats = new DefaultFormats {
override val typeHintFieldName = "type"
override val typeHints = ShortTypeHints(List(classOf[Foo],classOf[Bar1],classOf[Bar2]))
}
println(Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))))
結果は次のとおりです。
{
"type":"Foo",
"bar":[{
"type":"Bar1",
"value":1
},{
"type":"Bar2",
"value":5
},{
"type":"Bar1",
"value":1
}]
}
良い。しかし、この文字列を逆シリアル化しようとすると
println(Serialization.read[Foo](Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1))))))
私は例外を取得します:
net.liftweb.json.MappingException: 解析された JSON 値がクラス コンストラクターと一致しません args=List(Bar1(1), Bar2(5), Bar1(1)) arg types=scala.collection.immutable.$colon$colon コンストラクター=public test.Foo(scala.collection.immutable.Vector)
クラスFooで定義されたベクトル型ではなく、scalaリストに関連付けられたjson配列を意味します。net.liftweb.json.Serializer を拡張してカスタムシリアライザーを作成し、それをフォーマット値に含める方法があることを私は知っています。しかし、Vector に格納されているオブジェクトのタイプを復元するにはどうすればよいですか。私はこのように逆シリアル化の結果を取得したい:
Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))