Coder Social home page Coder Social logo

json-annotation's Introduction

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.3"
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.0-M3"

This library was tested with both Scala 2.10 and 2.11.

json-annotation's People

Contributors

apatzer avatar martinraison avatar andrewconner avatar stkem avatar

Watchers

James Cloos avatar Artur Bańkowski avatar Michal Nowak avatar Paweł Jagiełłowicz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.