项目作者: driver-oss

项目描述 :
Easy JSON formats for any case classes, for Scala, ScalaJS and Scala Native
高级语言: Scala
项目地址: git://github.com/driver-oss/spray-json-derivation.git
创建时间: 2018-02-13T23:11:04Z
项目社区:https://github.com/driver-oss/spray-json-derivation

开源协议:Apache License 2.0

下载


Build Status
Latest version
Download

Spray JSON Format Derivation

This library provides automatic
spray-json RootJsonFormats
for any case class and children of sealed traits.

It is available (and makes spray-json available) for Scala, ScalaJS and Scala Native.

It uses the Magnolia (source
code
) typeclass derivation
library to implicitly generate JSON formats for any product
type. Magnolia integrates with spray seamlessly, to such a point that
the contents of
DerivedFormats.scala provide
almost all functionality.

Getting Started

Spray JSON Format Derivation is published to maven central as a Scala
library (for Scala version 2.12 and 2.11, including support for ScalaJS
and Scala Native). Include it in sbt by adding the
following snippet to your build.sbt:

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

Basic Usage

Define some case classes and mix DerivedFormats into your JSON
protocol stack. Formats can now be summoned with the jsonFormat[A]
helper method. E.g.

  1. import spray.json._
  2. // case classes
  3. case class A(x: Int)
  4. case class B(a: A, str: String)
  5. // sealed traits
  6. sealed trait X
  7. case class Y(x: Int) extends X
  8. case class Z(y: Y, str: String) extends X
  9. object MyProtocol extends DefaultJsonProtocol with DerivedFormats {
  10. implicit val bFormat: RootJsonFormat[B] = jsonFormat[B] // [1]
  11. implicit val xFormat: RootJsonFormat[X] = jsonFormat[X]
  12. }
  13. // [1]: Note that no format was specified for A, although B contains an A.
  14. // In general, formats of children are not required to be defined
  15. // explicitly if the child is itself a case class. However they can still
  16. // be overriden if desired.
  17. object Main extends App {
  18. import MyProtocol._
  19. println(B(A(42), "hello world").toJson.prettyPrint)
  20. // {
  21. // "a": {
  22. // "x": 42
  23. // },
  24. // "str": "hello world"
  25. // }
  26. println(Seq[X](Z(Y(42), "foo"), Y(2)).toJson.prettyPrint)
  27. // [{
  28. // "@type": "Z",
  29. // "y": {
  30. // "x": 42
  31. // },
  32. // "str": "foo"
  33. // }, {
  34. // "type": "Y",
  35. // "x": 2
  36. // }]
  37. }

Error Handling

In case a format cannot be derived, e.g. if a child is not a case
class and does not have a format available, Magnolia will report a
detailed stack trace about which format could not be found. This is
most useful for deeply nested structures, where Scala’s conventional
implicit lookup would simply fail without giving a detailed error
message. For example:

  1. import spray.json._
  2. class X() // note that X is not a case class, hence a format cannot be derived
  3. case class Y(x: X)
  4. case class Z(y: Y, w: Int)
  5. object MyProtocol extends DefaultJsonProtocol with DerivedFormats {
  6. implicit val fmt: RootJsonFormat[Z] = jsonFormat[Z]
  7. }
  8. // magnolia: could not find JsonFormat.Typeclass for type ProductTypeFormatTests.this.X
  9. // in parameter 'x' of product type ProductTypeFormatTests.this.Y
  10. // in parameter 'y' of product type ProductTypeFormatTests.this.Z
  11. // implicit val fmt: RootJsonFormat[Z] = jsonFormat[Z]

Implicit Derived Formats

It is also possible to summon derived formats implicitly by mixing in
ImplicitDerivedFormatsinstead of DerivedFormats. This makes it
possible to use derived formats without explicitly creating them
first:

  1. import spray.json._
  2. object Main extends App with DefaultJsonProtocol with ImplicitDerivedFormats {
  3. case class A(x: Int)
  4. case class B(a: A, str: String)
  5. println(B(A(42), "hello world").toJson.prettyPrint)
  6. }

Although this variant of using the library provides the most power and
requires the least amount of boilerplate, it does come with a couple
of drawbacks to consider:

  1. Error handling falls back to the standard implicit lookup
    mechanism. Magnolia does no longer show a stack trace if a child
    format cannot be found.

  2. Macros that enable format derivation are expanded at every call
    site. In other words, the compiler will try to create JSON formats
    at every point where an implicit RootJsonFormat is required for a
    case class. This can increase compile times and binary size if
    formats are required in many locations.

Documentation

Check out the main file
DerivedFormats.scala and the
test suite 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

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