3
votes

I'm using the play framework, and have an abstract class:

 abstract class Base{...}

which has its own implicit JSON writer within the companion object

object Base {
   implicit val baseWrites: Writes[Base] = (...)(unlift(Base.unapply))
}

I subclass this abstract class:

case class SubClass{...}

which also has its own implicit JSON writer within its companion object

object SubClass {
   implicit val subClassWrites: Writes[SubClass] = (...)(unlift(SubClass.unapply))
}

When I try to serialize the subclass object using Json.toJson(SubClass), I get an error:

[error]  both value subClassWrites in object SubClass of type => play.api.libs.json.
Writes[models.SubClass]
[error]  and value baseWrites in object Base of type =>        
play.api.libs.json.Writes[models.Base]
[error]  match expected type play.api.libs.json.Writes[models.SubClass]
[error]  Ok(Json.toJson(SubClass.find(id)))

Is there any way to remove the ambiguity?

1

1 Answers

14
votes

You are getting a collision because Writes has a contravariant type parameter A:

trait Writes[-A] extends AnyRef

It means that Writes[Base] is subclass of Writes[SubClass] - you can use Writes[Base] where Writes[SubClass] is required.

The problem is here:

val base: Base = new SubClass(...)
val jsBase = Json.toJson(base)

So Writes[Base] should be able to serialize an instance of SubClass. You could use ADT in this case:

sealed trait Base
object Base {
  implicit val baseWrites: Writes[Base] = 
    new Writes[Base]{
      def writes(o: Base): JsValue = o match {
        case s: SubClass => SubClass.writes.writes(s)
        case s: SubClass2 => SubClass2.writes.writes(s)
      }
    }
}

case class SubClass(...) extends Base
object SubClass {
  val writes: Writes[SubClass] = (...)(unlift(SubClass.unapply))
}

case class SubClass2(...) extends Base
object SubClass2 {
  val writes: Writes[SubClass2] = (...)(unlift(SubClass2.unapply))
}

With sealed keyword you'll get a warning in case match is not exhaustive.