vendredi 11 septembre 2015

Using scala play-json 2.4.x, how do I extract the name of a json object into a different object?

Given this json:

{
  "credentials": {
    "b79a2ba2-lolo-lolo-lolo-lololololol": {
      "description": "meaningful description",
      "displayName": "git (meaningful description)",
      "fullName": "credential-store/_/b79a2ba2-lolo-lolo-lolo-lololololol",
      "typeName": "SSH Username with private key"
    }
  },
  "description": "Credentials that should be available irrespective of domain specification to requirements matching.",
  "displayName": "Global credentials (unrestricted)",
  "fullDisplayName": "Credentials » Global credentials (unrestricted)",
  "fullName": "credential-store/_",
  "global": true,
  "urlName": "_"
}

and this scala destination class:

case class JenkinsCredentials(uuid: String, description: String)

How can I create a Reads[JenkinsCredentials] to extract that first object name uuid b79a2ba2-lolo-lolo-lolo-lololololol along with the description?

Following the documentation it'd be something along the lines of this:

implicit val credsReader: Reads[JenkinsCredentials] = (
  (JsPath).read[String] and
    (JsPath \ "description").read[String]
  )(JenkinsCredentials.apply _)

Used with (Json.parse(content) \\ "credentials").validate[Seq[JenkinsCredentials]

But the documentation doesn't discuss anything about extracting the names of the objects as a field used somewhere else...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire