play-json-traits

Build Status Latest version

This package allows you read and write traits (not just sealed ones) using play-json. It does this by adding a discriminator property to the JSON which can be used to identify the precise implementation when parsing the JSON back to Scala.

Installation

The library is deployed to Maven Central.

Add the following to your build.sbt:

libraryDependencies += "io.leonard" %% "play-json-traits" % "$version"

Example usage

import io.leonard.TraitFormat.{ traitFormat, caseObjectFormat }
import play.api.libs.json.Json.format
import play.api.libs.json._

sealed trait Animal
case class Dog(s: String) extends Animal
case class Cat(s: String) extends Animal
case object Nessy extends Animal

val doggy = Dog("woof!")
val kitty = Cat("Meow!")

val animalFormat = traitFormat[Animal] << format[Dog] << format[Cat] << caseObjectFormat(Nessy)

val doggyJson = animalFormat.writes(doggy).toString

// returns {"s":"woof!","type":"Dog"}

val animal1: Animal = animalFormat.reads(Json.parse(doggyJson)).get
animal1 == doggy

animalFormat.writes(kitty).toString() == """{"s":"Meow!","type":"Cat"}"""

More examples can be found in the spec.

Customisation

By default the discriminator property is called type. However, this can be customised by doing the following:

val animalFormat = traitFormat[Animal]("animalType") << format[Dog] << format[Cat]
animalFormat.writes(doggy).toString() == """{"s":"woof!","animalType":"Dog"}"""

Similar projects