Skip to content

Instantly share code, notes, and snippets.

@benkolera
Created December 2, 2013 03:29
Show Gist options
  • Save benkolera/7744557 to your computer and use it in GitHub Desktop.
Save benkolera/7744557 to your computer and use it in GitHub Desktop.
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")
def sumTypeCodecHelper2[A <: X,B <: X ,X](
nameA:String , nameB:String
)( implicit
dA:DecodeJson[A], eA:EncodeJson[A], dB:DecodeJson[B], eB:EncodeJson[B]
) = CodecJson[X](
(sumType) => Json.obj( sumType match {
case o1:A => nameA := o1
case o2:B => nameB := o2
} ) ,
c => ( (c --\ nameA).as[A] ||| (c --\ nameB).as[B] )
)
implicit def sumTypeCodec = sumTypeCodecHelper2[Option1,Option2,SumType]("option1","option2")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment