Closed ajaychandran closed 1 year ago
Versions
Scastie
import zio.schema.* import zio.schema.codec.JsonCodec @annotation.discriminatorName("type") sealed trait Command derives Schema object Command: case class Buy(credits: Int) extends Command case object Cash extends Command val decoder = JsonCodec.jsonDecoder(summon[Schema[Command]]) val encoder = JsonCodec.jsonEncoder(summon[Schema[Command]]) var encoding = encoder.encodeJson(Command.Buy(100)) println(encoding) println(decoder.decodeJson(encoding)) encoding = encoder.encodeJson(Command.Cash) println(encoding) println(decoder.decodeJson(encoding))
Output:
{"type":"Buy","credits":100} Right(Buy(100)) {"type":"Cash"} Left(.type.Cash(expected '{' got '}'))
Versions
Scastie
Output: