Last active
August 26, 2022 00:39
-
-
Save salanki/95f792e3c7b9b236e0f4 to your computer and use it in GitHub Desktop.
Coproduct Encoder for Argonaut
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 shapeless.{Poly1, Coproduct} | |
import shapeless.ops.coproduct.Folder | |
import argonaut.{_} | |
import argonaut.Argonaut._ | |
/** | |
* Generic encoder for Coproducts, will only resolve if all types in a Coproduct has an EncodeJson in scope | |
*/ | |
object CoproductToArgonautFolder extends Poly1 { | |
implicit def caseAnyJsonable[A: EncodeJson] = at[A](_.asJson) | |
} | |
implicit def coproductEncode[A <: Coproduct, Out <: Json](implicit fldr: Folder.Aux[CoproductToArgonautFolder.type, A, Out]): EncodeJson[A] = | |
EncodeJson(_.fold(CoproductToArgonautFolder)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment