Skip to content

Instantly share code, notes, and snippets.

@benkolera
Last active December 29, 2015 23:39
Show Gist options
  • Save benkolera/7744061 to your computer and use it in GitHub Desktop.
Save benkolera/7744061 to your computer and use it in GitHub Desktop.
Ahh, so I was a noob after all.
import scalaz._
import argonaut._,Argonaut._
object JsonDb {
sealed trait SumType
case class Option1( foo:String ) extends SumType
case class Option2( bar:Int ) extends SumType
private implicit val option1Codec = casecodec1( Option1.apply , Option1.unapply _ )("foo")
private implicit val option2Codec = casecodec1( Option2.apply , Option2.unapply _ )("bar")
implicit def sumTypeCodec = CodecJson[SumType](
(sumType) => Json.obj( sumType match {
case o1:Option1 => "option1" := o1.asJson
case o2:Option2 => "option2" := o2.asJson
} ) ,
c => ( (c --\ "option1").as[Option1] ||| (c --\ "option2").as[Option2] )
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment