This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.libs.json._ | |
import scala.reflect.ClassTag | |
trait AdtFormat[A <: Product with Serializable] { | |
type ReadByTypeFunc[C <: A] = PartialFunction[String, JsResult[C]] | |
type ReadsFunc[B <: A] = JsValue => JsResult[B] | |
type WriteFunc = PartialFunction[A, JsValue] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.libs.functional.syntax._ | |
import play.api.libs.json.{Format, Json} | |
object SingleFieldFormat { | |
def format[A, B](f: A => B, g: B => A)(implicit fa: Format[A]) = fa.inmap(f, g) | |
// expanded: | |
// syntax.toInvariantFunctorOps(fa)(Format.invariantFunctorFormat).inmap(f, g) | |
// inmap's underlying: | |
// Format[B](fa.map(f), Writes[B](b => fa.writes(g(b)))) | |
} |