Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spray.json.JsArray to sequence of case classes #259

Closed
lehtone1 opened this issue May 14, 2018 · 4 comments
Closed

spray.json.JsArray to sequence of case classes #259

lehtone1 opened this issue May 14, 2018 · 4 comments

Comments

@lehtone1
Copy link

How do I convert a spray.json.JsArray to sequence of case classes?

e.g. I have a variable

movieArray which is of typespray.json.JsArray and I would like to turn it into a sequence of Movie case classes case class Movie(id: Int, title: String)

I tried already movieArray.convertTo[Seq[Movie]] and movieArray.map(_.convertTo[Movie]), but neither seemed to work.

@ktoso
Copy link
Member

ktoso commented May 14, 2018

Can you define does "not work" mean here? Pasting the compile or runtime errors you encountered would make helping you easier.

This works:

    "works" in {
      case class Movie(id: Int, title: String)

      import spray.json._
      import DefaultJsonProtocol._ // if you don't supply your own Protocol (see below)
      implicit val movieFormat = jsonFormat2(Movie)

      val x = """
        |[
        |  { "id": 12, "title": "that movie" }
        |]
      """.stripMargin.parseJson

      info("it + " + x.convertTo[Seq[Movie]])
    }

@lehtone1
Copy link
Author

"not work" -meant that I got the following errors

Cannot find JsonReader or JsonFormat type class for Seq[Movie]
value map is not a member of spray.json.JsValue

Followed your instructions, and now I got an error which says...

Cannot automatically determine case class field names and order for 'Movie', please use the 'jsonFormat' overload with explicit field name specification

Any suggestions for next step?

@lehtone1
Copy link
Author

Apparently I had a problem in my class. Your solution is correct. Thanks

@ktoso
Copy link
Member

ktoso commented May 14, 2018

Happy hakking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants