play-json-traits


License

License

Categories

Categories

JSON Data
GroupId

GroupId

io.leonard
ArtifactId

ArtifactId

play-json-traits_2.11
Last Version

Last Version

1.4.5
Release Date

Release Date

Type

Type

jar
Description

Description

play-json-traits
play-json-traits
Project URL

Project URL

https://github.com/leonardehrenfried/play-json-traits
Project Organization

Project Organization

io.leonard
Source Code Management

Source Code Management

https://github.com/leonardehrenfried/play-json-traits

Download play-json-traits_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/io.leonard/play-json-traits_2.11/ -->
<dependency>
    <groupId>io.leonard</groupId>
    <artifactId>play-json-traits_2.11</artifactId>
    <version>1.4.5</version>
</dependency>
// https://jarcasting.com/artifacts/io.leonard/play-json-traits_2.11/
implementation 'io.leonard:play-json-traits_2.11:1.4.5'
// https://jarcasting.com/artifacts/io.leonard/play-json-traits_2.11/
implementation ("io.leonard:play-json-traits_2.11:1.4.5")
'io.leonard:play-json-traits_2.11:jar:1.4.5'
<dependency org="io.leonard" name="play-json-traits_2.11" rev="1.4.5">
  <artifact name="play-json-traits_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.leonard', module='play-json-traits_2.11', version='1.4.5')
)
libraryDependencies += "io.leonard" % "play-json-traits_2.11" % "1.4.5"
[io.leonard/play-json-traits_2.11 "1.4.5"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.11
com.typesafe.play : play-json_2.11 jar 2.6.3

provided (1)

Group / Artifact Type Version
org.joda : joda-convert jar 1.8.2

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.4

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.4.5
1.4.4
1.4.3
1.4.2
1.4.0
1.3.6
1.3.5
1.3.4
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.1.0
1.0.1
1.0.0