我有一个类型 Wf 是
case class Wf(id: Option[WfId], socId: SocId, year: Int, employment: Float) extends WithId[WfId]
我有以下定义 argonaut 解码器的隐式函数
implicit def WfDecodeJson: DecodeJson[Wf] = {
DB.withSession {
implicit session: Session =>
DecodeJson(c => for {
id <- (c --\ "id").as[Option[WfId]]
socCode <- (c --\ "soc").as[Int]
year <- c.--\("predictedEmployment").\\.--\("year").as[Int]
employment <- c.--\("predictedEmployment").\\.--\("employment").as[Float]
} yield Wf(None,
SocSchema.socsTable.filter(_.code === 2216).first.id.get,
year,
employment))
}
}
这是我要解码的 json
{
soc: 2216,
predictedEmployment: [
{
year: 2014,
employment: 19916.416015625
},
{
year: 2015,
employment: 20252.84375
},
{
year: 2016,
employment: 20345.037109375
},
{
year: 2017,
employment: 20640.29296875
},
{
year: 2018,
employment: 21200.6328125
},
{
year: 2019,
employment: 21564.833984375
},
{
year: 2020,
employment: 21896.298828125
},
{
year: 2021,
employment: 22376.546875
},
{
year: 2022,
employment: 22867.8828125
}
]
}
如果我做
json.decodeOption[Wf]
我得到了预期的一些(Wf)
如果我做
json.decodeOption[List[Wf]]
我得到零。如果我向隐式解码器添加断点,它会在我只要求 Wf 时进入 for 理解。当我要求 List[Wf] 时,它甚至似乎都没有进入理解
如果我做
json.decodeValidation[List[Wf]]
我明白了
Failure([A]List[A]: [])
我究竟做错了什么?