I have a problem with adding a field to Json object in Play Framework using Scala:
I have a case class containing data. For example:
case class ClassA(a:Int,b:Int)
and I am able to create a Json object using Json Writes:
val classAObject = ClassA(1,2)
implicit val classAWrites= Json.writes[ClassA]
val jsonObject = Json.toJson(classAObject)
and the Json would look like:
{ a:1, b:2 }
Let's suppose I would like to add an additional 'c' field to the Json object. Result:
{ a:1, b:2, c:3 }
How do I do that without creating a new case class or creating my Json object myself using Json.obj? I am looking for something like:
jsonObject.merge({c:3})
Any help appreciated!
Writes.writes
returnsJsValue
, so I don't understand how you can get rid of upcasting (not in the REPL). – Foliose