Trait

endpoints.documented.algebra

JsonSchemas

Related Doc: package algebra

Permalink

trait JsonSchemas extends AnyRef

An algebra interface for describing algebraic data types. Such descriptions can be interpreted to produce a JSON schema of the data type, a JSON encoder, a JSON decoder, etc.

A description contains the fields of a case class and their type, and the constructor names of a sealed trait.

For instance, consider the following record type:

case class User(name: String, age: Int)

Its description is the following:

object User {
  implicit val schema: JsonSchema[User] = (
    field[String]("name") zip
    field[Int]("age")
  ).invmap((User.apply _).tupled)(Function.unlift(User.unapply))
}

The description says that the record type has two fields, the first one has type String and is named “name”, and the second one has type Int and name “age”.

To describe sum types you have to explicitly “tag” each alternative:

sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(width: Double, height: Double) extends Shape

object Shape {
  implicit val schema: JsonSchema[Shape] = {
    val circleSchema = field[Double]("radius").invmap(Circle)(Function.unlift(Circle.unapply))
    val rectangleSchema = (
      field[Double]("width") zip
      field[Double]("height")
    ).invmap((Rectangle.apply _).tupled)(Function.unlift(Rectangle.unapply))
    (circleSchema.tagged("Circle") orElse rectangleSchema.tagged("Rectangle"))
      .invmap[Shape] {
        case Left(circle) => circle
        case Right(rect)  => rect
      } {
        c: Circle    => Left(c)
        r: Rectangle => Right(r)
      }
  }
}
Source
JsonSchemas.scala
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. JsonSchemas
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. abstract type JsonSchema[A]

    Permalink

    The JSON schema of a type A

  2. implicit final class JsonSchemaOps[A] extends AnyRef

    Permalink

    Convenient infix operations

  3. abstract type Record[A] <: JsonSchema[A]

    Permalink

    The JSON schema of a record type (case class) A

  4. implicit final class RecordOps[A] extends AnyRef

    Permalink

    Convenient infix operations

  5. abstract type Tagged[A] <: JsonSchema[A]

    Permalink

    A JSON schema containing the name of the type A.

    A JSON schema containing the name of the type A. Tagged schemas are useful to describe sum types (sealed traits).

  6. implicit final class TaggedOps[A] extends AnyRef

    Permalink

Abstract Value Members

  1. implicit abstract def arrayJsonSchema[C[X] <: Seq[X], A](implicit jsonSchema: JsonSchema[A], cbf: CanBuildFrom[_, A, C[A]]): JsonSchema[C[A]]

    Permalink

    A JSON schema for sequences

  2. implicit abstract def bigdecimalJsonSchema: JsonSchema[BigDecimal]

    Permalink

    A JSON schema for type BigDecimal

  3. implicit abstract def booleanJsonSchema: JsonSchema[Boolean]

    Permalink

    A JSON schema for type Boolean

  4. abstract def choiceTagged[A, B](taggedA: Tagged[A], taggedB: Tagged[B]): Tagged[Either[A, B]]

    Permalink

    The JSON schema of a coproduct made of the given alternative tagged records

  5. implicit abstract def doubleJsonSchema: JsonSchema[Double]

    Permalink

    A JSON schema for type Double

  6. abstract def field[A](name: String, documentation: Option[String] = None)(implicit tpe: JsonSchema[A]): Record[A]

    Permalink

    The JSON schema of a record with a single field name of type A

  7. implicit abstract def intJsonSchema: JsonSchema[Int]

    Permalink

    A JSON schema for type Int

  8. abstract def invmapJsonSchema[A, B](jsonSchema: JsonSchema[A], f: (A) ⇒ B, g: (B) ⇒ A): JsonSchema[B]

    Permalink

    Transforms the type of the JSON schema

  9. abstract def invmapRecord[A, B](record: Record[A], f: (A) ⇒ B, g: (B) ⇒ A): Record[B]

    Permalink

    Transforms the type of the JSON schema

  10. abstract def invmapTagged[A, B](taggedA: Tagged[A], f: (A) ⇒ B, g: (B) ⇒ A): Tagged[B]

    Permalink

    Transforms the type of the JSON schema

  11. implicit abstract def longJsonSchema: JsonSchema[Long]

    Permalink

    A JSON schema for type Long

  12. abstract def optField[A](name: String, documentation: Option[String] = None)(implicit tpe: JsonSchema[A]): Record[Option[A]]

    Permalink

    The JSON schema of a record with a single optional field name of type A

  13. implicit abstract def stringJsonSchema: JsonSchema[String]

    Permalink

    A JSON schema for type String

  14. abstract def taggedRecord[A](recordA: Record[A], tag: String): Tagged[A]

    Permalink

    Tags a schema for type A with the given tag name

  15. abstract def zipRecords[A, B](recordA: Record[A], recordB: Record[B]): Record[(A, B)]

    Permalink

    The JSON schema of a record merging the fields of the two given records

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  14. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  16. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped