scala circe编码器/解码器,用于带有case类的抽象类

时间:2018-05-12 14:09:38

标签: scala circe

我想将一组FieldMapping类保存为json字符串 -

abstract class Field {
  def clazz: Class[_]

  def name: String
}

case class StringField(name: String) extends Field {
  override def clazz: Class[_] = classOf[String]
}

case class DateField(name: String) extends Field {
  override def clazz: Class[_] = classOf[Date]
}

...等 - 这里的完整代码: https://github.com/alexeyOnGitHub/scala-typesafe/blob/master/src/main/scala/com/example/model/Field.scala

Circe code:

import com.example.model.{DateField, Field, FieldMapping, StringField}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}

object CirceBoilerplateForConfigs {
  implicit val fieldDecoder: Decoder[StringField] = deriveDecoder[StringField]
  implicit val fieldEncoder: Encoder[StringField] = deriveEncoder[StringField]

  implicit val dateDecoder: Decoder[DateField] = deriveDecoder[DateField]
  implicit val dateEncoder: Encoder[DateField] = deriveEncoder[DateField]

  implicit val fooDecoder: Decoder[FieldMapping] = deriveDecoder[FieldMapping]
  implicit val fooEncoder: Encoder[FieldMapping] = deriveEncoder[FieldMapping]
}
  

错误:(14,65)找不到类型的延迟隐式值   io.circe.generic.decoding.DerivedDecoder [com.example.model.FieldMapping]   隐式val fooDecoder:解码器[FieldMapping] =   deriveDecoder [FieldMapping]错误:(14,65)

     

方法deriveDecoder没有足够的参数:(隐式解码:   shapeless.Lazy [io.circe.generic.decoding.DerivedDecoder [com.example.model.FieldMapping]])io.circe.Decoder [com.example.model.FieldMapping]。   未指定的值参数解码。隐式val fooDecoder:   解码器[FieldMapping] = deriveDecoder [FieldMapping]错误:(15,65)

     

找不到类型的Lazy隐式值   io.circe.generic.encoding.DerivedObjectEncoder [com.example.model.FieldMapping]   隐式val fooEncoder:编码器[FieldMapping] =   deriveEncoder [FieldMapping]错误:(15,65)

     

没有足够的论据   方法deriveEncoder :(隐式编码:   shapeless.Lazy [io.circe.generic.encoding.DerivedObjectEncoder [com.example.model.FieldMapping]])io.circe.ObjectEncoder [com.example.model.FieldMapping]。   未指定的值参数编码。隐式val fooEncoder:   编码器[FieldMapping] = deriveEncoder [FieldMapping]

1 个答案:

答案 0 :(得分:2)

public Form1() { InitializeComponent(); pictureBox1.Left = (this.ClientSize.Width - pictureBox1.Width) / 2; pictureBox1.Top = (this.ClientSize.Height - pictureBox1.Height) / 2; } 应该是一个密封的特征(具有抽象类或没有密封的特性,这将不起作用)。

以下代码编译:

Field