【发布时间】:2020-03-23 22:51:00
【问题描述】:
我正在使用 Scala 和 Circe。我有以下密封特征。
sealed trait Mode
case object Authentication extends Mode
case object Ocr extends Mode
当调用SessionModel.Authentication 时,此案例对象的输出如下:
"Authentication":{}
我需要将其转换为字符串,以便输出"authentication"
【问题讨论】:
-
您可以在添加the
circe-generic-extradependency 后使用here 之类的代码派生解码器和编码器。 -
另外,为了让名称保持小写,您可以使用自定义名称转换器:implicit val (modeDecoder, modeEncoder) = {implicit val config: Configuration = Configuration.default.withDefaults.copy(transformConstructorNames = _.toLowerCase); (deriveEnumerationDecoder[Mode],deriveEnumerationEncoder[Mode]) }