如何使用 circe 将密封的特征案例对象转换为字符串

How to convert sealed trait case objects to string using circe

我正在使用 Scala 和 Circe。我有以下密封特性。

  sealed trait Mode
  case object Authentication extends Mode
  case object Ocr extends Mode

调用 SessionModel.Authentication 时此案例对象的输出如下:

"Authentication":{}

我需要将其转换为字符串,以便输出 "authentication"

正如 Andriy Plokhotnyuk 上面提到的,您可以使用 circe-generic-extras:

import io.circe.Codec
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveEnumerationCodec

sealed trait Mode
case object Authentication extends Mode
case object Ocr extends Mode

object Mode {
  private implicit val config: Configuration =
    Configuration.default.copy(transformConstructorNames = _.toLowerCase)

  implicit val modeCodec: Codec[Mode] = deriveEnumerationCodec[Mode]
}

然后:

scala> import io.circe.syntax._
import io.circe.syntax._

scala> (Authentication: Mode).asJson
res1: io.circe.Json = "authentication"

scala> io.circe.Decoder[Mode].decodeJson(res1)
res2: io.circe.Decoder.Result[Mode] = Right(Authentication)

(请注意,Codec 是 0.12 中的新增内容——对于早期版本,您必须像 Andriy 的评论中那样写出这两个实例。)

除非你有很多这样的东西要维护,否则我个人认为手工写出实例通常比使用 circe-generic-extras 更好,在这种情况下它甚至不会更冗长:

import io.circe.{Decoder, Encoder}

sealed trait Mode
case object Authentication extends Mode
case object Ocr extends Mode

object Mode {
  implicit val decodeMode: Decoder[Mode] = Decoder[String].emap {
    case "authentication" => Right(Authentication)
    case "ocr"            => Right(Ocr)
    case other            => Left(s"Invalid mode: $other")
  }

  implicit val encodeMode: Encoder[Mode] = Encoder[String].contramap {
    case Authentication => "authentication"
    case Ocr            => "ocr"
  }
}

它的工作原理与 deriveEnumerationCodec 版本完全相同,但除了 circe-core 之外不需要任何东西,没有那么神奇,编译速度更快等等。通用派生对于简单的情况可能很好 类 使用简单的映射,但我认为人们经常尝试将其扩展到涵盖所有情况,而手动编写实例不会造成太大负担,甚至可能更清晰。