Skip to content

Instantly share code, notes, and snippets.

Created March 23, 2015 22:59
Show Gist options
  • Save anonymous/55e59636a730d74ebcad to your computer and use it in GitHub Desktop.
Save anonymous/55e59636a730d74ebcad to your computer and use it in GitHub Desktop.
case Object to json
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