Using abstract classes when creating Json using the Play JSON library

I am trying to come up with a good approach to creating my json for some api calls that I want to make from my Play application to another service.

I am having a problem creating an abstract class that I want to include in case classes. I have a common Request object and want to include Parameter objects in it, Login is an example of a Parameters object, but the contents can change.

I tried to create a companion object by working with a tag instead of an abstract class and tried to implement Writer hand objects instead of using the start method, however I cannot get the type system to do what I want, and the documentation does not describe what I'm trying to do.

package sandbox

import play.api.libs.json._
import play.api.libs.functional.syntax._

object JsonTest {
    abstract class Parameters

    case class Request(
        interface: String,
        method: String,
        parameters: Parameters
    )

    case class Login(username: String, password: String) extends Parameters

    implicit val loginWrites = Json.writes[Login]
    implicit val requestWrites = Json.writes[Request]

    val login = Login("user1", "pass1")
    val request = Request("interface1", "method1", login)
    val requestJS = Json.toJson(request)
}

, , - ?

+4

All Articles