Last active
December 29, 2015 23:39
-
-
Save benkolera/7744061 to your computer and use it in GitHub Desktop.
Ahh, so I was a noob after all.
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
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