Permalink
Browse files

introduced play-json-extensions for formatting Suit and Rank

  • Loading branch information...
1 parent f4e527c commit 49c538ad87d1626d2f4702bf2ed3305635f006cd @wlk committed Mar 25, 2016
Showing with 8 additions and 18 deletions.
  1. +3 −16 app/models/JsonMarshalling.scala
  2. +4 −2 app/models/v1/cardsCaseObjects.scala
  3. +1 −0 build.sbt
@@ -1,28 +1,15 @@
package models
import com.github.tototoshi.play.json.JsonNaming
+import ai.x.play.json.Jsonx
import models.v1._
import play.api.libs.json._
trait JsonMarshalling {
- implicit val suitFormat = new Format[Suit] {
- def writes(s: Suit) = JsString(s.toString)
+ implicit val suitFormat = Jsonx.formatInline[Suit]
- def reads(json: JsValue) = json match {
- case JsNull => JsError()
- case _ => JsSuccess(Suit(json.as[String]))
- }
- }
-
- implicit val rankFormat = new Format[Rank] {
- def writes(r: Rank) = JsString(r.toString)
-
- def reads(json: JsValue) = json match {
- case JsNull => JsError()
- case _ => JsSuccess(Rank(json.as[String]))
- }
- }
+ implicit val rankFormat = Jsonx.formatInline[Rank]
implicit val cardFormat = Json.format[Card]
@@ -8,7 +8,8 @@ object Suit {
def apply(key: String) = fromStringMap(key)
}
-sealed abstract class Suit(val value: String) {
+// should be abstract, workaround for play-json-extensions
+sealed class Suit(val value: String) {
override def toString = value
}
@@ -29,7 +30,8 @@ object Rank {
def apply(key: String) = fromStringMap(key)
}
-sealed abstract class Rank(val value: String) {
+// should be abstract, workaround for play-json-extensions
+sealed class Rank(val value: String) {
override def toString = value
}
View
@@ -16,6 +16,7 @@ libraryDependencies ++= Seq(
cache,
ws,
"com.github.tototoshi" %% "play-json-naming" % "1.1.0",
+ "ai.x" %% "play-json-extensions" % "0.8.0",
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0-RC1" % Test
)

0 comments on commit 49c538a

Please sign in to comment.