score:0

One way, which is pretty straightforward, is to use case classes along with Play Json inceptions

import play.api.libs.json.Json

case class Relationship(relationType: String, value: String)

object Relationship {

  implicit val RelationshipFormatter = Json.format[Relationship]

}

case class Person(personId: String, name: String, age: Int, birthdate: String, relationships: Seq[Relationship]) {

  def withRelationships(relationship: Seq[Relationship]) = copy(relationships = relationships ++ relationship)

}

object Person {

  implicit val PersonFormatter = Json.format[Person]

}

Now you can convert a json value to Person by using the following code, provided that jsValue is a json value of type JsValue (which in play controllers you can get by request.body.asJson):

Json.fromJson[Person](jsValue)

In Play controllers, you can

For converting a Person to json you can use the following code provided that person is a value of type Person in your context:

Json.toJson(person)

The only remaining thing is your Slick schemas which is pretty straight forward.

One option is to use a simple schema for Person, without relations and one schema for Relation with a foreign key to Person table. Then you need to find all relations associated with a specific Person, namely person and then append them to that person by calling the withRelationships method which gives you a new Person which you can serve as json:

val person = ....      // find person
val relations = ....   // find relationships associated with this person
val json = Json.toJson(person.withRelationships(relations))

Related Query

More Query from same tag