Version Matrix


Build Status Download

A library for deriving a play-json formatter for a JSON-encoded tagged union.

Adding to your build

In your SBT build add:

resolvers += Resolver.bintrayRepo("hmrc", "releases")

libraryDependencies += "" %% "play-json-union-formatter" % "x.x.x"


In a given JSON payload, you may want to capture a field or array that contains a set of hetrogeneous types.

You might have created an object recognition system that recognises two types of object - a car and a pet:

  "matchType": "car",
  "fuelType": "petrol"
  "matchType": "pet",
  "species": "cat"

The natural way to represent this in scala is:

sealed trait Match

case class Pet(species: String) extends Match
case class Car(fuelType: String) extends Match

play-json-union-formatter provides a way to derive a play formatter (compatible with play 2.3.x) for the above.

import play.api.libs.json.Json

object Match { 
  implicit val formatPet = Json.format[Pet]
  implicit val formatCar = Json.format[Car]
  implicit val format = Union.from[Match](typeField = "matchType")
    .and[Pet](typeTag = "pet")
    .and[Car](typeTag = "car")


This code is open source software licensed under the Apache 2.0 License.