Skip to content
No description, website, or topics provided.
Scala
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
project
src
.gitignore
LICENSE
README.md
build.sbt
repository.yaml

README.md

play-json-union-formatter

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 += "uk.gov.hmrc" %% "play-json-union-formatter" % "x.x.x"

If your microservice is using Play 2.5, use version 1.7.0 or older. Versions > 1.7.0 only support Play 2.6

Usage

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
import uk.gov.hmrc.play.json.Union

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")
    .format
}

License

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

You can’t perform that action at this time.