0
votes

I have a type Wf which is

case class Wf(id: Option[WfId], socId: SocId, year: Int, employment: Float) extends WithId[WfId]

I have the following implicit function defining an argonaut decoder

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))
  }
}

here is the json I am trying to decode

{
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
}
]
}

if I do

json.decodeOption[Wf]

I get the expected some(Wf)

if I do

json.decodeOption[List[Wf]]

I get Nil. If I add breakpoints to the implicit decoder it enters the for comprehension when i just ask for Wf. It dosn't even seem to enter the for comprehension when I ask for the List[Wf]

if I do

json.decodeValidation[List[Wf]]

I get

Failure([A]List[A]: [])

What am I doing wrong?

2
Better to look at the details of failure (hidden when using decodeOption)cchantep
thanks, thats listed at the endMark

2 Answers

1
votes

Your JSON starts with {, not [, so it's not an array which could be parsed as List[T].

0
votes

Rather than writing the DecodeJson instances manually by hand using the cursors you'd be much better off using methods like casecodec2 and friends.GeneratedDecodeJsons