I am trying to derive the following string into a proper ADT type:
res6: String = {"raw":"Hello","status":{"MsgSuccess":{}}}
and using the circe library.
The ADT type looks as the following:
sealed trait MsgDoc {
}
final case class MsgPreFailure(raw: String, reasons: Chain[String]) extends MsgDoc
final case class MsgProceed(raw: String, status: MsgStatus) extends MsgDoc
and the MsgStatus
type:
sealed trait MsgStatus {
}
case object MsgSuccess extends MsgStatus
final case class MsgFailure(reasons: Chain[String]) extends MsgStatus
final case class MsgUnknown(reason: String) extends MsgStatus
and the way, I've tried to drive:
object MsgDocDerivation {
import shapeless.{Coproduct, Generic}
implicit def encodeAdtNoDiscr[A, Repr <: Coproduct](implicit
gen: Generic.Aux[A, Repr],
encodeRepr: Encoder[Repr]
): Encoder[A] = encodeRepr.contramap(gen.to)
implicit def decodeAdtNoDiscr[A, Repr <: Coproduct](implicit
gen: Generic.Aux[A, Repr],
decodeRepr: Decoder[Repr]
): Decoder[A] = decodeRepr.map(gen.from)
}
and the execution:
object Main extends App {
val json = MsgProceed("Hello", MsgSuccess).asJson
println(json)
val adt = decode[MsgDoc](json.noSpaces)
println(adt)
}
as the result I've got:
{
"raw" : "Hello",
"status" : {
"MsgSuccess" : {
}
}
}
Left(DecodingFailure(CNil, List()))
As you can see, it does not decode
properly.
The source code can be find https://gitlab.com/playscala/adtjson.