I have the 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 the Play Json format, so I think I should have something like this (I don't want to do this in a 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 the compiler complains and says:
No implicit format for Tweet available.
He also says that I cannot use Edited.type because it should be applied and not use functions. What should I do?
Edit1:
I can recall 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 a problem, the compiler complains that it needs JsonResult not Edited.type
json scala playframework scala-macros
Omid
source share