我有一个json格式的成绩单,里面有很多单词
{
"words": [{
"duration": 123,
"name": "world"
"time": 234,
"speaker": null
}]
}
我一直在使用Circe来编码/解码Json。在这种特殊情况下:
import io.circe.generic.auto._
import io.circe.parser._
val decoded = decode[Transcript](transcriptJson)
我的ADT看起来像:
case class Word(
duration: Double,
name: String,
time: Float,
para: String,
speaker: Option[String],
key: Option[String] = None,
strike: Option[String] = None,
highlight: Option[String] = None
)
case class Transcript(words: List[Word])
有时单词会出现“罢工”或“突出显示”等键,但很可能没有。如果没有,我收到以下错误消息。
Left(DecodingFailure([A]List[A], List(DownField(highlight), MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, MoveRight, DownArray, DownField(words))))
当“单词”没有所有字段时,正确解码它的最佳方法是什么?
答案 0 :(得分:3)
特拉维斯·布朗在吉特指出:
“这将与泛型附加功能一样:”
import io.circe.generic.extras.Configuration
implicit val config: Configuration = Configuration.default.withDefaults
(加上para和import io.circe.generic.extras.auto._
的默认值)