play JSON Support
This module provides refined types Writes/Reads instances for play JSON.
Dependency
SBT:
libraryDependencies += "io.github.iltotore" %% "iron-play-json" % "version"
Mill:
ivy"io.github.iltotore::iron-play-json:version"
Following examples' dependencies
SBT:
libraryDependencies += "org.playframework" %% "play-json" % "3.0.5"
Mill:
ivy"org.playframework::play-json::3.0.5"
Writes/Reads instances
import play.api.libs.json.{Reads, Writes, Json}
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.playJson.given
type Username = DescribedAs[Alphanumeric, "Username should be alphanumeric"]
type Age = DescribedAs[Positive, "Age should be positive"]
case class User(name: String :| Username, age: Int :| Age)
//Encoding
Json.stringify(Json.writes[User].writes(User("Iltotore", 8))) //{"name":"Iltotore", "age":18}
//Decoding
Json.fromJson[User](Json.parse("""{"name":"Iltotore","age":18}"""))(Json.reads[User]) //JsSuccess(User(Iltotore,18),)
In this article