GithubHelp home page GithubHelp logo

duketon / json-annotation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kifi/json-annotation

0.0 2.0 0.0 212 KB

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

License: MIT License

Scala 100.00%

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.

#How it works Just add @json in front of your case class definition:

import com.kifi.macros.json

@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)

If the case class contains 2 fields or more, Play's JSON macro inception is used. If the case class has only one field (i.e. the class is just a wrapper around another type), then the JSON format is the format of the field itself:

> @json case class City(name: String)
> val city = City("San Francisco")
> Json.toJson(city)
"San Francisco"

This is often more convenient than Play's default format {"name": "San Francisco"}.

If you would rather stick to Play's default format even for single field case classes, you can use @jsonstrict instead of @json.

#Installation

If you're using Play (version 2.1 or higher) with SBT, you should add the following settings to your build:

libraryDependencies += "com.kifi" %% "json-annotation" % "0.2"

addCompilerPlugin("org.scalamacros" % "paradise" % "2.0.1" 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.2.1"

This library was tested with both Scala 2.10 and 2.11.

json-annotation's People

Contributors

andrewconner avatar martinraison avatar stkem avatar

Watchers

 avatar  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.