Giter Site home page Giter Site logo

scala-play-json-derived's Introduction

Scala CI Release

play-json derivation

Provide derivation of Reads and Writes of play-json types, using magnolia

Install

  libraryDependencies += "com.github.fxhibon" %% "play-json-derived-play-2X" % "<latest-release>" 

How to use

  • for a complete examples, see the tests

In a few words, given:

sealed trait Tree[T]

case class Branch[T](left: Tree[T], right: Tree[T]) extends Tree[T]

case class Leaf[T](value: T) extends Tree[T]

one can derive for zero cost an instance of Reads or Writes for Tree[Int], assuming there is a Reads[Int] in the scope

import play.api.libs.json._
import com.github.fxhibon.json.derived.ReadsDerivation._
import com.github.fxhibon.json.derived.WritesDerivation._

val treeReads: Reads[Tree[Int]] = deriveReads[Tree[Int]]
val treeWrites: Writes[Tree[Int]] = deriveWrites[Tree[Int]]

val leaf: Tree[Int] = Json.parse("""{"type": "Leaf", "value": 123}""").as[Tree[Int]](treeReads)
// Leaf(123)

val json = treeWrites.writes(leaf)
// {"type": "Leaf", "value": 123}

The default behaviors to read/write type name and payload can be overloaded by providing implicit instances for respectively TypeNameReads, TypeNameWrites and PayloadPath

import play.api.libs.json._
import com.github.fxhibon.json.derived.ReadsDerivation._
import com.github.fxhibon.json.derived.WritesDerivation._

implicit val payloadPath: PayloadPath = PayloadPath(JsPath \ "data")
implicit val typeNameWrites: TypeNameWrites = TypeNameWrites((JsPath \ "custom_type_name").write[String])
implicit val typeNameReads: TypeNameReads = TypeNameReads((JsPath \ "custom_type_name").read[String])

val treeReads: Reads[Tree[Int]] = deriveReads[Tree[Int]]
val treeWrites: Writes[Tree[Int]] = deriveWrites[Tree[Int]]

val leaf: Tree[Int] = Json.parse("""{"custom_type_name": "Leaf", "data": {"value": 123}}""").as[Tree[Int]](treeReads)
// Leaf(123)

val json = treeWrites.writes(leaf)
// {"custom_type_name": "Leaf", "data": {"value": 123}}

I made sure that errors generated by failing Reads or Writes are close enough to what play-json macro's would have generated, so switching from play-json macro to this library should be seamless.

Features

  • derive Reads
  • derive Writes
  • support customization of type name Reads / Writes
  • support customization of payload Reads / Writes

Resources

scala-play-json-derived's People

Contributors

fxhibon avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.