2017-09-28 89 views
2

我有JSON格式與一堆話轉錄在它解碼的Json與瑟茜當字段是不完全的

{ 
    "words": [{ 
      "duration": 123, 
      "name": "world" 
      "time": 234, 
      "speaker": null 
     }] 
} 

我已經使用瑟茜編碼/解碼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)))) 

當「單詞」沒有所有字段時,最好的解碼方法是什麼?

回答

2

由於特拉維斯布朗的Gitter指出:

「這會工作的,是仿製演員:」

import io.circe.generic.extras.Configuration 

implicit val config: Configuration = Configuration.default.withDefaults 

(加上第一個默認值,import io.circe.generic.extras.auto._