Spray JSON Format Derivation

This library provides automatic spray-jsonRootJsonFormats 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:

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:

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:

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:

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.

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

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.