I can not comment on Json4s since I have never used it, but I know that this is not a problem in play-json . You would do something like:
import play.api.libs.json._
sealed trait MyEventBase
case object MyEvent extends MyEventBase
implicit val myEventBaseFormat: Format[MyEventBase] = Format(Reads.StringReads.collect(ValidationError("must be the string `MyEvent`") {
case "MyEvent" => MyEvent
}, Writes.pure("MyEvent"))
, StringReads , , collect, . , , Reads. singleton case object. , , deserialize MyEventBase, MyEvent, .
MyEventBase, , , Writes - , Reads , , JSON , type, . - JSON Extensions, Format sealed trait.