json - 使用lift-json进行向量反序列化

标签 json scala lift lift-json

如何使用lift-json将json数组反序列化为scala向量?

例如:

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

我得到一个异常(exception):

net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor args=List(Bar1(1), Bar2(5), Bar1(1)) arg types=scala.collection.immutable.$colon$colon constructor=public test.Foo(scala.collection.immutable.Vector)



这意味着与 scala 列表关联的 json 数组,而不是类 Foo 中定义的向量类型。我知道有一种方法可以通过扩展 net.liftweb.json.Serializer 并将其包含到格式值中来创建自定义序列化程序。但是如何恢复存储在 Vector 中的对象类型。我想得到这样的反序列化结果:

Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))

最佳答案

我经常被 List 惹恼-以 Lift 为中心,并且发现自己过去需要做类似的事情。以下是我使用的方法,针对您的示例进行了一些调整:

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

有点丑,可能可以清理一下,但它有效。

关于json - 使用lift-json进行向量反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244046/

相关文章:

json - 如果解码器失败使用其他解码器

scala - 拆分 Monix Observable

scala - 为什么要解除触发方法?

json - 从 JSON 中提取嵌套实体和值

database - Lift 映射器中的外键约束

javascript - 使用 laravel 从 json 中删除 &quot

java - JSONObject ["featured_image"] 不是字符串

c# - 将 SQL Server json 属性与代码优先 EF 一起使用

java - Scala - Java 互操作 : can Scala emit enums in bytecode for Java to consume?

scala - 合法代码在 Scalding 中无法编译