How to Use Circe for Decoding JSON Lists/Arrays in Scala

别说谁变了你拦得住时间么 提交于 2019-12-07 06:46:20

问题


I have the code snippet

cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]

Where Clip is a simple case class of strings and numbers. The incoming Json should contain a json object "playlist" with an array of "items" where each item is a clip. So the json should look like

{
  "playlist": {
      "name": "Sample Playlist",
      "items": [
        {
          "clipId":"xyz", 
          "name":"abc"
        },
        {
          "clipId":"pqr", 
          "name":"def"
        } 
      ]
   }
}

With the code snippet above, I'm getting the compile error:

 Error:(147, 81) could not find implicit value for parameter d:     
 io.circe.Decoder[List[com.packagename.model.Clip]]
      cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]

What am I doing wrong? How do you setup decoding for a list/array of simple items using circe?


回答1:


For the sake of completeness, instead of navigating into the JSON value and then decoding the clips, you could create a custom decoder that includes the navigation in its processing:

import io.circe.Decoder, io.circe.generic.auto._

case class Clip(clipId: String, name: String)

val decodeClipsParam = Decoder[List[Clip]].prepare(
  _.downField("params").downField("playlist").downField("items")
)

And then if you've got this:

val json = """{ "params": {
  "playlist": {
      "name": "Sample Playlist",
      "items": [
        {
          "clipId":"xyz", 
          "name":"abc"
        },
        {
          "clipId":"pqr", 
          "name":"def"
        } 
      ]
   }
}}"""

You can use the decoder like this:

scala> io.circe.parser.decode(json)(decodeClipsParam)
res3: Either[io.circe.Error,List[Clip]] = Right(List(Clip(xyz,abc), Clip(pqr,def)))

I'd probably go a step further and use a custom case class:

import io.circe.generic.auto._
import io.circe.generic.semiauto.deriveDecoder

case class Clip(clipId: String, name: String)
case class PlaylistParam(name: String, items: List[Clip])

object PlaylistParam {
  implicit val decodePlaylistParam: Decoder[PlaylistParam] =
    deriveDecoder[PlaylistParam].prepare(
      _.downField("params").downField("playlist")
    )
}

Now you can just write this:

scala> io.circe.parser.decode[PlaylistParam](json).foreach(println)
PlaylistParam(Sample Playlist,List(Clip(xyz,abc), Clip(pqr,def)))

How you want to split up the navigation and decoding is mostly a matter of taste, though.




回答2:


Thanks for the help. I was able to figure it out after stepping away for awhile and coming back with fresh eyes.

I think I was going wrong by using the downArray function.

My solution was to do the following:

override def main(args: Array[String]): Unit = {
    import ClipCodec.decodeClip

    val json = parse(Source.fromFile("playlist.json").mkString).right.get
    val clips = json.hcursor.downField("params").downField("playlist")
                   .downField("items").as[Seq[Clip]]

  }



回答3:


Circe is looking for an implicitly declared decoder for List[Clip] and cannot find it.

I suspect that you have not defined a decoder either manually or (semi)automatically. You can do both by following the official docs https://circe.github.io/circe/codec.html.

Unfortunately I cannot provide more detail than this because the question is rather vague. I will update my answer when more details are given.



来源:https://stackoverflow.com/questions/42288948/how-to-use-circe-for-decoding-json-lists-arrays-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!