Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

The quickest way to add a JSON format to your Play project's case classes

License

Notifications You must be signed in to change notification settings

vital-software/json-annotation

 
 

Repository files navigation

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.3" // Latest release
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"

The library is currently published for Scala 2.11 - 2.13

About

The quickest way to add a JSON format to your Play project's case classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 98.3%
  • Dockerfile 1.7%