【问题标题】:Scala, spray-json: universal enumeration json formattingScala,spray-json:通用枚举 json 格式
【发布时间】:2018-03-18 15:22:36
【问题描述】:

我有这样的模型:两个枚举和一个案例类,其中包含这些枚举类型的两个字段:

// see later, why objects are implicit
implicit object Fruits extends Enumeration {
  val Apple = Value("apple")
  val Orange = Value("orange")
}

implicit object Vegetables extends Enumeration {
  val Potato = Value("potato")
  val Cucumber = Value("cucumber")
  val Tomato = Value("tomato")
}

type Fruit = Fruits.Value
type Vegetable = Vegetables.Value

case class Pair(fruit: Fruit, vegetable: Vegetable)

我想使用 spray-json 向/从 Pairs 解析/生成 JSON。我不想为水果和蔬菜单独声明JsonFormats。所以,我想做这样的事情:

import spray.json._
import spray.json.DefaultJsonProtocol._

// enum is implicit here, that's why we needed implicit objects
implicit def enumFormat[A <: Enumeration](implicit enum: A): RootJsonFormat[enum.Value] =
  new RootJsonFormat[enum.Value] {
    def read(value: JsValue): enum.Value = value match {
      case JsString(s) =>
        enum.withName(s)
      case x =>
        deserializationError("Expected JsString, but got " + x)
    }

    def write(obj: enum.Value) = JsString(obj.toString)
  }

// compilation error: couldn't find implicits for JF[Fruit] and JF[Vegetable]
implicit val pairFormat = jsonFormat2(Pair)

// expected value:
// spray.json.JsValue = {"fruit":"apple","vegetable":"potato"}
// but actually doesn't even compile
Pair(Fruits.Apple, Vegetables.Potato).toJson

遗憾的是,enumFormat 不会为 jsonFormat2 生成隐式值。如果我在 pairFormat 之前手动为水果和蔬菜格式编写两个隐式声明,那么 json marshalling 就可以了:

implicit val fruitFormat: RootJsonFormat[Fruit] = enumFormat(Fruits)
implicit val vegetableFormat: RootJsonFormat[Vegetable] = enumFormat(Vegetables)

implicit val pairFormat = jsonFormat2(Pair)

// {"fruit":"apple","vegetable":"potato"}, as expected
Pair(Fruits.Apple, Vegetables.Potato).toJson

那么,两个问题:

  1. 如何摆脱这些fruitFormatvegetableFormat 声明?

  2. 理想情况下,最好不要将枚举对象设为隐式,同时保持enumFormat 函数的通用性。有没有办法做到这一点?也许,使用scala.reflect 包或类似的东西。

【问题讨论】:

    标签: json scala enums spray-json


    【解决方案1】:

    您只需将enum.Value 替换为A#Value

    查看spray-json #200,您可以找到定义明确的隐式enumFormat 的示例,稍作修改以利用隐式enu 检索:

    implicit def enumFormat[T <: Enumeration](implicit enu: T): RootJsonFormat[T#Value] =
      new RootJsonFormat[T#Value] {
        def write(obj: T#Value): JsValue = JsString(obj.toString)
        def read(json: JsValue): T#Value = {
          json match {
            case JsString(txt) => enu.withName(txt)
            case somethingElse => throw DeserializationException(s"Expected a value from enum $enu instead of $somethingElse")
          }
        }
    }
    

    【讨论】:

      【解决方案2】:

      你不能,Scala 不允许隐式链接,因为它会导致组合爆炸,使编译器太慢。

      https://docs.scala-lang.org/tutorials/FAQ/chaining-implicits.html

      Scala 不允许发生两次这样的隐式转换,因此不能使用隐式 A 到 B 和另一个隐式 B 到 C 从 A 到 C。

      您必须为要使用的每个 T 显式生成一个 JsonFormat[T]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 2016-03-09
        • 2017-06-03
        • 1970-01-01
        相关资源
        最近更新 更多