I have following configuration:
sealed trait Status
case object Edited extends Status
case object NotEdited extends Status
case class Tweet(content:String, status:Status)
I want to use Play Json format, so I guess I have to have something like this(I don't want to do it in companion object):
trait JsonImpl{
implicit val TweetFormat = Json.format[Tweet]
implicit val statusFormat = Json.format[Status]
implicit val StatusFormat = Json.format[Edited.type]
implicit val NotEditedFormat = Json.format[NotEdited.type]
}
but compiler complains and says:
No implicit format for Tweet available.
Also it says I cannot use Edited.type
because it needs apply and unapply functions. What should I do?
Edit1:
I can think of something like this:
implicit object StatusFormat extends Format[Status] {
def reads(json: JsValue) =
(json \ "type").get.as[String] match {
case "" => Edited
case _ => UnEdited
}
def writes(stat: Status) = JsObject(Seq(
stat match {
case Edited => "type" -> JsString("Edited")
case NotEdited => "type" -> JsString("UnEdited")
}
))
}
but the read
part has problem, the compiler complains that it needs JsonResult not Edited.type
For doing that I should define an implicit object like this: