3

I have a trait that has many implementations. When in json format you can distinguish easly between them by kind property. Is there a some simple way to have json4s pick correct implementation based on that property? I would prefer to avoid writing huge custom serializer to handle each case individually.

I was hoping something like this would work:

class ExampleTest extends WordSpecLike with Matchers {

  implicit val formats: Formats = DefaultFormats + new CarSerializer

  val teslaJson =
    """
      |{
      | "name": "Tesla",
      | "kind": "electric",
      | "batteryCapacity": 250.0
      |}
    """.stripMargin
  val bmwJson =
    """
      |{
      | "name": "BMW",
      | "kind": "petrol",
      | "tankCapacity": 70.0
      |}
    """.stripMargin

  val tesla = ElectricCar(
    "Tesla", "electric", 250.0
  )

  val bmw = PetrolCar(
    "BMW", "petrol", 70.0
  )

  "JSON4s" should {
    "extract electric car" in {
      parse(teslaJson).extract[ElectricCar] should be(tesla)
    }
    "extract petrol car" in {
      parse(bmwJson).extract[PetrolCar] should be(bmw)
    }

    "extract electric car with CarSerializer" in {
      parse(teslaJson).extract[Car] should be(teslaJson)
    }
  }

}

class CarSerializer extends CustomSerializer[Car](_ => ( {
  case car: JObject =>
    car \ "kind" match {
      case JString(v) if v == "electric" => car.extract[ElectricCar]
      case JString(v) if v == "petrol" => car.extract[PetrolCar]
    }
}, {
  case car: Car => Extraction.decompose(car)
}))

sealed trait Car {
  def name: String

  def kind: String
}

case class ElectricCar(
  name: String,
  kind: String,
  batteryCapacity: Double
) extends Car

case class PetrolCar(
  name: String,
  kind: String,
  tankCapacity: Double
) extends Car

Unfortunately it does not work as extract and decompose functions require implicit formats to be present but the serializer itself is meant to be part of the formats.

Gustek
  • 3,680
  • 2
  • 22
  • 36
  • is there a reason you are creating a serializer instead of using polymorphic lists (https://github.com/json4s/json4s#serializing-polymorphic-lists)? – Assaf Mendelson Jul 18 '18 at 13:32
  • thats sounds like it may be we I need. I will try it in coming days. – Gustek Jul 18 '18 at 17:25
  • Did you find a solution to this mate? – Ali Nov 02 '19 at 14:28
  • I am not actively working on that project anymore, but just had a quick look at the code there and seems like I did manage to get it working with the TypeHints from Assaf link. – Gustek Nov 04 '19 at 22:39

0 Answers0