我正在尝试解码这段 Json:
{
"id" : "e07cff6a-bbf7-4bc9-b2ec-ff2ea8e46288",
"paper" : {
"title" : "Example Title",
"authors" : [
"1bf5e911-8878-4e06-ba8e-8159aadb052c"
]
}
}
但是,当它到达 Sets 部分时,它会失败。错误消息没有帮助。
DecodingFailure([A]Set[A], List())
这是我的代码:
implicit val paperIdDecoder: Decoder[PaperId] = Decoder.decodeString.emap[PaperId] { str ⇒
Either.catchNonFatal(PaperId(str)).leftMap(_.getMessage)
}
implicit val paperAuthorDecoder: Decoder[PaperAuthor] = Decoder.decodeString.emap[PaperAuthor] { str ⇒
Either.catchNonFatal(PaperAuthor(str)).leftMap(_.getMessage)
}
implicit val paperDecoder: Decoder[Paper] = {
for {
title <- Decoder.decodeString
authors <- Decoder.decodeSet[PaperAuthor]
} yield Paper(title, authors)
}
implicit val paperViewDecoder: Decoder[PublishedPaperView] = for {
id <- Decoder[PaperId]
paper <- Decoder[Paper]
} yield PublishedPaperView(id, paper)
以下是使用的案例类:
case class PublishedPaperView(id: PaperId, paper: Paper)
case class PaperId(value: String)
case class Paper(title: String, authors: Set[PaperAuthor])
case class PaperAuthor(value: String)