playframework/Play20 — pull request #809 by mandubian (2013-03-18 17:35:08+00:00)
Title: Serializing selective attributes of a case class to a nested JSON object discards all but last selected attribute...

If you want to "unflatten" a case class to a JSON object by putting certain attributes within their own nested object, only the last attribute will be written. For example:

case class TestCase(id: String, attr1: String, attr2: String)

*should* serialize to:

{
"id": "my-id",
"data": {
"attr1": "hello",
"attr2": "world"
}
}

The obvious way to do this would be with a Writes (or Format) that looks like:

implicit val testCaseWrites: Writes[TestCase] = (
(__ \ "id").write[String] and
(__ \ "data" \ "attr1").write[String] and
(__ \ "data" \ "attr2").write[String]
)(unlift(TestCase.unapply))

However this does not work "correctly", since only "attr2" will end up in the serialized JSON:

{
"id" : "id-1",
"data" : {
"attr2":"world"
}
}

Note that doing an equivalent `Reads[TestCase]` object works correctly, so I think this is definitely unexpected behaviour.
@gissues:{"order":97.43589743589743,"status":"backlog"}