Search code examples
jsonscalaplayframeworkplay-json

Play JSON Reads: read value which can be presented in multiple types


I has such JSON for ship:

{
  "name" : "Watership Elizbeth",
  "sea": "white",
  "size": 100500,
  "residents" : [ {
    "type" : "men",
    "age" : 4,
    "sex": 0
  }, {
    "type" : "cat",
    "legs" :4,
    "color" : "black",
    "leg1" :{
      "color" : "black"
    },
    "leg2" :{
      "color" : "white"
    },
    "leg3" :{
      "color" : "brown"
    },
    "leg4" :{
      "color" : "black"
    }
  },{
    "type" : "bird",
    "size" : "XXL",
    "name": "ostrich"
  }, {"type": "water",...}, {"type": "mice",...}, {...}]
}

As you see in JsArray residents i has residents which is different:men, cat, bird, ...water,... All that residents of ship has own simple case classes in Scala code:

case class Men (type: String, age: Int, sex: Int)
case class Leg (color: String)
case class Cat (type: String, legs: Int, color: String, leg1: Leg, leg2: Leg, leg3: Leg, leg4: Leg)
case class Bird (...)

Also for each simple case class i write readers in their companion objects:

object Men {
implicit val reads: Reads[Ship] = (
    (JsPath \ "type").read[String] and
      (JsPath \ "age").read[Int] and
      (JsPath \ "sex").read[Int]
    )(Ship.apply _)
}
// and so on for another objects...

The main problem that i do not find how to create case class for Ship and its reads:

case class Ship (name: String, sea: String, size: Int, residents: Seq[???])

object Ship {
implicit val reads: Reads[Ship] = (
    (JsPath \ "name").read[String] and
      (JsPath \ "sea").read[String] and
      (JsPath \ "size").read[Int] and
      (JsPath \ "residents").read[Seq[???]]
    )(Ship.apply _)
}

As you see residents is array which elements can be different types: Men, Cat, Bird.. I want instead ??? put in reads of Ship all objects of my types or some like `AnyVal, but it is not work:

// for case classes
case class Ship (name: String, sea: String, size: Int, residents: Seq[Man,Cat,Bird,...])
case class Ship (name: String, sea: String, size: Int, residents: Seq[AnyVal])

// for case reads
(JsPath \ "residents").read[Seq[Man,Cat,Bird,...]]
(JsPath \ "residents").read[Seq[AnyVal]]

How to write case class Ship and its reads which can read in its key multiple types?


Solution

  • The easiest way would be to extend all case classes from a sealed trait. That way play-json can generate a Reads automatically:

    sealed trait Resident
    
    case class Men (age: Int, sex: Int) extends Resident
    case class Leg (color: String) extends Resident
    case class Cat (legs: Int, color: String, leg1: Leg, leg2: Leg, leg3: Leg, leg4: Leg) extends Resident
    case class Bird (...) extends Resident
    
    object Resident {
      private val cfg = JsonConfiguration(
          discriminator = "type",
    
          typeNaming = JsonNaming { fullName =>
            fullName
              .replaceFirst(".*[.]", "")
              .toLowerCase
          }
        )
    
      implicit val reads = Json.configured(cfg).reads[Resident]
    }
    
    case class Ship (name: String, sea: String, size: Int, residents: Seq[Resident])
    
    object Ship {
      implicit val reads: Reads[Ship] = Json.reads[Ship]
    }
    

    Here is a complete example.