4

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)))

4

2 に答える 2

3

私はListLiftの中心性に悩まされることが多く、過去にも同様のことをする必要があることに気づきました。以下は私が使用したアプローチであり、あなたの例に少し適応しています。

trait Bar { def value: Int }
case class Bar1(value: Int) extends Bar
case class Bar2(value: Int) extends Bar
case class Foo(bar: Vector[Bar])

import net.liftweb.json._

implicit val formats = new DefaultFormats { outer =>
  override val typeHintFieldName = "type"
  override val typeHints =
    ShortTypeHints(classOf[Bar1] :: classOf[Bar2] :: Nil) +
    new ShortTypeHints(classOf[Foo] :: Nil) {
      val FooName = this.hintFor(classOf[Foo])
      override def deserialize = {
        case (FooName, foo) => foo \ "bar" match {
          case JArray(bars) => Foo(
            bars.map(_.extract[Bar](outer, manifest[Bar]))(collection.breakOut)
          )
          case _ => throw new RuntimeException("Not really a Foo.")
        }
      }
    }
}

ちょっと醜いです、そしておそらく少しきれいにすることができます、しかしそれは働きます。

于 2012-06-28T18:49:43.723 に答える
1

暗黙的な変換を追加できます。

implicit def listToVect(list:List[Bar]):Vector[Bar] = list.map(identity)(breakOut)

その後、Serialization.read[Foo]期待どおりに動作します。

于 2012-06-28T15:06:16.050 に答える