Open nryanov opened 3 years ago
Hi! I had the same problem of this and ended up doing this:
val scriptCheckEncoder = DeriveJsonEncoder.Gen[ScriptCheck]
// ... repeat for all your types
implicit val checkEncoder: JsonEncoder[Check] = DeriveJsonEncoder.gen[Check].contramap(¨
{
case c:ScriptCheck => c
// Repeat for all your types
}
)
This seems silly, but what this is essentially doing is telling the type system to look for your implicitly defined case class encoders and use those instead of the ADT encoder.
Hello!
I have a domain model (for consul api) and want to encode/decode types like
Check
andServiceCheck
and their subtypes . The issue is in model encoding/decoding results. Classes :and encoders/decoders:
When i encode instance of any subtype of
Check
orServiceCheck
i get a json object like this:The api does not recognise field
ScriptCheck
and fails with an error. If i try to encode ScriptCheck explicitly using encoder forScriptCheck
type then json will not have type discriminator.I tried to use annotations with empty values
@jsonHint("")
and@jsonDiscriminator("")
, as i thought that it will remove it because in macros there is a code like this:but got an error:
Also, i tried to construct encoder using
eitherWith
, but in result i get a json with additional fieldLeft
orRight
. In the meantime, decoder can be constructed usingorElse
and it will not require type discriminator.Is it possible to completely remove type discriminator?