zio-json icon indicating copy to clipboard operation
zio-json copied to clipboard

Combine encoding as an array of values and JsonDiscriminator

Open rouazana opened this issue 4 years ago • 1 comments

Hello,

JMAP is encoding some data, called Invocation in the following way:

["nameOfTheMethod", arguments, "clientId"]

It's an array of 3 elements, the first and the third are string, while the second is an object which type depends on the first.

So for example

"""["Mailbox/get", {}, "0"]""".fromJson[Invocation]

should return:

Either[String, MailboxGet] = Right(MailboxGet("Mailbox/get", MailboxGetArguments(...), "0"))

while

"""["Mailbox/query", {}, "0"]""".fromJson[Invocation]

should return:

Either[String, MailboxQuery] = Right(MailboxQuery("Mailbox/query", MailboxQueryArguments(...), "0"))

given:

sealed trait Invocation
case class MailboxGet(methodName: String, args: MailboxGetArguments, clientId: String) extends Invocation
case class MailboxQuery(methodName: String, args: MailboxQueryArguments, clientId: String) extends Invocation

Is there a way to handle this case with zio-json?

rouazana avatar Nov 05 '20 10:11 rouazana

@rouazana This is a very specific need. It needs to be handled manually.

guizmaii avatar Oct 31 '23 08:10 guizmaii