Skip to content

Instantly share code, notes, and snippets.

@tixxit
Created November 7, 2014 15:35
Show Gist options
  • Save tixxit/380af8de0f2471b368df to your computer and use it in GitHub Desktop.
Save tixxit/380af8de0f2471b368df to your computer and use it in GitHub Desktop.
package example
import argonaut._
import Argonaut._
sealed trait WidgetType
object WidgetType {
case object Sprocket extends WidgetType
case object Cog extends WidgetType
case object Doowazzle extends WidgetType
}
object json {
implicit val WidgetTypeCodec: CodecJson[WidgetType] = {
import WidgetType._
CodecJson({
case Sprocket => "sprocket".asJson
case Cog => "cog".asJson
case Doowazzle => "doowazzle".asJson
},
{ j =>
j.as[String].flatMap {
case "sprocket" => DecodeResult.ok(Sprocket)
case "cog" => DecodeResult.ok(Cog)
case "doowazzle" => DecodeResult.ok(Doowazzle)
case _ => DecodeResult.fail("example.WidgetType", j.history)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment