Scala 枚举循环序列化

Scala 枚举循环序列化,scala,circe,enumeratum,Scala,Circe,Enumeratum,我有一个简单的case类,如下所示: case class ColumnMetadata(name: String, displayName: String, description: Option[String], attributeType: AttributeType) sealed trait AttributeType exten

我有一个简单的case类,如下所示:

case class ColumnMetadata(name: String,
                          displayName: String,
                          description: Option[String],
                          attributeType: AttributeType)

sealed trait AttributeType extends EnumEntry

case object AttributeType extends Enum[AttributeType] with CirceEnum[AttributeType] {

 val values: immutable.IndexedSeq[AttributeType] = findValues

  case object Number extends AttributeType
  case object Text extends AttributeType
  case object Percentage extends AttributeType
}
和半自动编码器:

package object circe {

  implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames


  implicit val columnMetaDecoder: Decoder[ColumnMetadata] = deriveConfiguredDecoder
  implicit val columnMetaEncoder: Encoder[ColumnMetadata] = deriveConfiguredEncoder

  implicit val columnTypeDecoder: Decoder[AttributeType] = deriveConfiguredDecoder
  implicit val columnTypeEncoder: Encoder[AttributeType] = deriveConfiguredEncoder
}
但是,当我进行序列化测试时:

  ColumnMetadata("column1", "Column 1", Some("column1"), 
    AttributeType.withName("Text")).asJson
我得到:

  {
    "name" : "column1",
    "display_name" : "Column 1",
    "description" : "column1",
    "attribute_type" : {
      "Text": {}
    }
  }
当我想要的时候:

  {
    "name" : "column1",
    "display_name" : "Column 1",
    "description" : "column1",
    "attribute_type" : "Text"
  } 
当我使用自动派生时,它可以工作,但我想使用半自动派生,这样我就可以使用诸如WithNakeAssemMemberNames之类的功能。

这是您的错误:


  implicit val columnTypeDecoder: Decoder[AttributeType] = deriveConfiguredDecoder
  implicit val columnTypeEncoder: Encoder[AttributeType] = deriveConfiguredEncoder
这将派生新的编解码器,将
AttributeType
视为任何其他密封特征,因此它将使用判别值(半自动始终忽略其派生类型的现有编解码器!)

因此,您将派生一个新的
属性类型
编解码器,将它们放在使用它们的范围内,从而使这些新实现比来自同伴对象的实现具有更高的优先级。我总是赢

如果您不派生编解码器(因为
CirceEnum
trait已经提供了现有的实现),那么它将按照您的预期工作

此外,不要这样做:

  implicit val columnMetaDecoder: Decoder[ColumnMetadata] = deriveConfiguredDecoder
  implicit val columnMetaEncoder: Encoder[ColumnMetadata] = deriveConfiguredEncoder
您可以这样做:

// make sure that Configuration is in scope by e.g. importing it
// or putting it in package object in the same package as case class
@ConfiguredJsonCodec
case class ColumnMetadata(name: String,
                          displayName: String,
                          description: Option[String],
                          attributeType: AttributeType)
这将节省您创建编解码器包和手动导入它们的工作,无论您在哪里需要它们。例如

// imports

package object models_package {

  private[models_package] implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames
}
package models_package

// imports

@ConfiguredJsonCodec
case class ColumnMetadata(name: String,
                          displayName: String,
                          description: Option[String],
                          attributeType: AttributeType)

sealed trait AttributeType extends EnumEntry
object AttributeType extends Enum[AttributeType] with CirceEnum[AttributeType] {

 val values: immutable.IndexedSeq[AttributeType] = findValues

  case object Number extends AttributeType
  case object Text extends AttributeType
  case object Percentage extends AttributeType
}