Search code examples
jsonplay-json

Play Framework JSON Conversion


I'm planning to use the Play JSON library to Serialize and De-Serialize objects to and from Json. I have a case class like this:

case class MyCaseClass(string: String, intVal: Int)

I have a service that returns me a JSON String which effectively is a Seq[MyCaseClass]. I can put this String into a JsValue, but how could I convert this JsValue into MyCaseClass?

def reads(json: JsValue): JsResult[Seq[MyCaseClass]] = {
  ???
}

Any suggestions?


Solution

  • Have you tried simply defining reads as Json.format[MyCaseClass]? If the names of the properties match the names in the json you are reading, that should work.