vendredi 8 mai 2015

Scala key/value case class to Json

Given the following case class:

case class ValueItem(key: String, value: String)

and the following json formatter:

implicit val valueItemFormat: Format[ValueItem] = (
        (__ \ "key").format[String] and
        (__ \ "value").format[String])(ValueItem.apply, unlift(ValueItem.unapply))

a json representation of a ValueItem instance like

ValueItem("fieldname", "fieldValue")

is

{ "key" : "fieldName" , "value" : "fieldValue" }

I'm wondering how to get a json in a flat key/value serialization like

{ "fieldName" : "fieldValue" }

Aucun commentaire:

Enregistrer un commentaire