Skip to content

scalacommunitybuild/spray-json-shapeless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spray-json-shapeless

Automatically derive [spray-json][spray-json] JsonFormats, powered by [shapeless][shapeless] (no macros were written during the creation of this library).

For documentation, read the scaladocs and for examples read the test cases.

Please read the documentation - especially the caveats - and examples before raising a ticket.

TL;DR

// always check maven central for the lastest release
libraryDependencies += "com.github.fommil" %% "spray-json-shapeless" % "1.3.0"
import spray.json._
import fommil.sjs.FamilyFormats._

object domain {
  sealed trait SimpleTrait
  case class Foo(s: String) extends SimpleTrait
  case class Bar() extends SimpleTrait
  case object Baz extends SimpleTrait
  case class Faz(o: Option[String]) extends SimpleTrait
}
object use {
  import domain._

  Foo("foo").toJson                // """{"s":"foo"}"""
  Faz(Some("meh")).toJson          // """{"o":"meh"}"""
  Faz(None).toJson                 // """{}"""
  (Foo("foo"): SimpleTrait).toJson // """{"type":"Foo","s":"foo"}"""
  (Bar(): SimpleTrait).toJson      // """{"type":"Bar"}"""
  (Baz: SimpleTrait).toJson        // """{"type":"Baz"}"""
  (Faz(None): SimpleTrait).toJson  // """{"type":"Faz"}"""
}