Skip to content
The quickest way to add a JSON format to your Play project's case classes
Branch: master
Clone or download
Pull request Compare This branch is 43 commits ahead of kifi:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildkite
project
src
.dockerignore
.gitignore
CHANGELOG.md
LICENSE
README.md
build.sbt
version.sbt

README.md

The @json scala macro annotation is the quickest way to add a JSON format to your Play project's case classes.

Project forked from Martin Raison's Kifi macros

How it works

Just add @json in front of your case class definition:

import com.github.vitalsoftware.macros._

@json case class Person(name: String, age: Int)

You can now serialize/deserialize your objects using Play's convenience methods:

import play.api.libs.json._
val person = Person("Victor Hugo", 46)
val json = Json.toJson(person)
Json.fromJson[Person](json)

For Play-Json 2.6+, you can use @jsonDefaults as an equivalent to Json.using[Json.WithDefaults].format[T]. This creates a format where the Reads[T] will pull in default values from non-Option case classes. These must be static values (e.g. timestamp = DateTime.now is not a good default, as DateTime.now is a def).

@jsonDefaults case class Person(name: String, age: Int = 7)
Json.fromJson("{\"name\": \"Victor Hugo\"}")
> Person("Victor Hugo", 7)

Installation

If you're using Play with SBT, you should add both the package and the "Macros Paradise" compiler plugin:

libraryDependencies += "com.github.vital-software" %% "json-annotation" % "0.6.0"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

If you're not using Play, you will also need to add play-json to your dependencies:

resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.7"

This library was tested with both Scala 2.11 and 2.12.

You can’t perform that action at this time.