aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 3c15698520a32e0212133a87f0e3de2eb8977788 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[![Build Status](https://travis-ci.org/drivergroup/spray-json-derivation.svg?branch=master)](https://travis-ci.org/drivergroup/spray-json-derivation)
[![Latest version](https://index.scala-lang.org/drivergroup/spray-json-derivation/latest.svg)](https://index.scala-lang.org/drivergroup/spray-json-derivation)
[![Download](https://img.shields.io/maven-central/v/xyz.driver/spray-json-derivation_2.12.svg)](http://search.maven.org/#search|ga|1|xyz.driver%20spray-json-derivation-)

# Spray JSON Format Derivation

This library provides automatic spray JsonFormats for any `case class`
and children of `sealed trait`s.

It uses the [Magnolia](http://magnolia.work/) ([source
code](https://github.com/propensive/magnolia)) typeclass derivation library
to implicitly generate JSON formats for any product type. Magnolia
integrates with spray so seamlessly that it is almost not worth the
effort to publish this project as a full fledged repository; a single
gist with the contents of
[DerivedFormats.scala](src/main/scala/DerivedFormats.scala) would
demonstrate almost all functionality.

## Getting Started

Spray JSON Format Derivation is published to maven central as a Scala
library. Include it in sbt by adding the following snippet to your
build.sbt:

```scala
libraryDependencies += "xyz.driver" %% "spray-json-derivation" % "<latest version>"
```

Define some case classes and mix `DerivedFormats` into your JSON
protocol stack. That's it.

```scala
import spray.json._
import xyz.driver.json.DerivedFormats

object Main extends App with DefaultJsonProtocol with DerivedFormats {
  
  // Simple case classes

  case class A(x: Int)
  case class B(a: A, str: String)

  println(B(A(42), "hello world").toJson.prettyPrint)
  // {
  //   "a": {
  //     "x": 42
  //   },
  //   "str": "hello world"
  // }


  // Sealed traits

  sealed trait X
  case class Y(x: Int) extends X
  case class Z(y: Y, str: String) extends X

  println(Seq[X](Z(Y(42), "foo"), Y(2)).toJson.prettyPrint)
  // [{
  //   "type": "Z",
  //   "y": {
  //     "x": 42
  //   },
  //   "str": "foo"
  // }, {
  //   "type": "Y",
  //   "x": 2
  // }]

}
```

## Documentation
Check out the main file
[DerivedFormats.scala](src/main/scala/DerivedFormats.scala) and the
[test suite](src/test/scala/ProductTypeFormats.scala) for a complete
overview of the project.

## Development
This project uses sbt. It is set up to auto-release when a tag is
pushed to the main repository.

## Copying
Copyright 2018 Driver Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.