Skip to content

Instantly share code, notes, and snippets.

@dabd
Created March 29, 2016 21:20
Show Gist options
  • Save dabd/432687c972e939bba68002bf9bdbd24a to your computer and use it in GitHub Desktop.
Save dabd/432687c972e939bba68002bf9bdbd24a to your computer and use it in GitHub Desktop.
import play.api.libs.json.{Format, JsResult, JsValue, Json}
import shapeless.tag.@@
trait Money
case class Amount(price: String @@ Money)
private val amountWrites = Json.writes[Amount]
private val amountReads = Json.reads[Amount]
implicit val format: Format[Amount] = new Format[Amount] {
override def writes(amount: Amount): JsValue = Json.toJson(amount)(amountWrites)
override def reads(json: JsValue): JsResult[Amount] = Json.fromJson[Amount](json)(amountReads)
}
implicit val formatMoney: Format[String @@ Money] = new Format[@@[String, Money]] {
override def writes(o: @@[String, Money]): JsValue = Json.toJson(o)(Json.writes[String @@ Money])
override def reads(json: JsValue): JsResult[@@[String, Money]] = Json.fromJson[String @@ Money](json)(Json.reads[String @@ Money])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment