Created
March 23, 2015 22:59
-
-
Save anonymous/55e59636a730d74ebcad to your computer and use it in GitHub Desktop.
case Object to json
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
package TicTacToe | |
import TicTacToe.Player._ | |
import spray.json._ | |
/** | |
* Created by ferdy on 3/23/15. | |
*/ | |
sealed trait Player | |
object Player { | |
case object X extends Player | |
case object O extends Player | |
case object None extends Player | |
} | |
object PlayerJsonProtocol extends DefaultJsonProtocol { | |
implicit object PlayerJsonFormat extends JsonFormat[Player] { | |
override def read(json: JsValue): Player = json match { | |
case (JsString("X")) => X | |
case (JsString("O")) => O | |
case (JsString("None")) => None | |
case _ => throw new DeserializationException("Not a player") | |
} | |
override def write(player: Player): JsString = player match { | |
case (X) => JsString("X") | |
case (O) => JsString("O") | |
case (None) => JsString("None") | |
} | |
} | |
} | |
object Main extends App { | |
println(X.toJson) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment