Skip to content

A support library for integrating JSON API specification with Scala and Spray JSON or Play! JSON

License

Notifications You must be signed in to change notification settings

wlk/scala-jsonapi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

Build Status Coverage Status codecov.io Join the chat at https://gitter.im/zalando/scala-jsonapi

A Scala library for producing JSON output based on JSON API specification.

Current status

The library supports read and write for both Play-JSON and Spray-JSON.

Library is very much Work-In-Progress and expect API to change.

Currently library supports Scala version 2.11 and 2.10.

Setup

In order to use current version scala-jsonapi you have to add library dependency assuming that you have sonatype resolvers set up.

libraryDependencies += "org.zalando" %% "scala-jsonapi" % "0.3.4"

Usage

The rich JSON API model is available by following import:

import org.zalando.jsonapi.model._

The library provides serialization and deserialization of Jsonapi root objects to JSON using either Spray-JSON or Play-JSON. Please note that you need to explicitly add a dependency to either spray-json or play-json to your project.

Spray-JSON

import org.zalando.jsonapi.json.sprayjson.SprayJsonJsonapiProtocol._
import spray.json._

// Serialization
val rootObject: RootObject = ???
rootObject.toJson

// Deserialization
val json: JsValue = ???
json.convertTo[RootObject]

Play-JSON

import org.zalando.jsonapi.json.playjson.PlayJsonJsonapiProtocol._
import play.api.libs.json._

// Serialization
val rootObject: RootObject = ???
Json.toJson(rootObject)

// Deserialization
val json: JsValue = ???
Json.fromJson[RootObject](json)

Creating JSON API root object

The library provides type class JsonapiRootEntityWriter in order to enable you to create JSON API representation for your resources. Following code snippet demonstrate the usage of that:

import org.zalando.jsonapi
import jsonapi.Jsonapi

case class Person(name: String)

implicit val personJsonapiWriter = new JsonapiRootObjectWriter[Person] {
  override def toJsonapi(person: Person) = {
    ???
  }
}

val personRootObject: RootObject = Jsonapi.asJsonapi(Person("Boris M."))

For complete usage see the specs example.

Publishing and Releasing

Publishing and releasing is made with help of sbt-sonatype plugin

About

A support library for integrating JSON API specification with Scala and Spray JSON or Play! JSON

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%