Декодирование ZIO-JSON со значениями по умолчанию в случае, если класс не работает для Scala 3.2.2, но успешен в 2.13.8
У меня есть короткая тестовая программа, которая не может декодировать строку json, поскольку в строке json отсутствует идентификатор personId.
Однако у меня есть значение по умолчанию, установленное в классе case для personId на основе документации zio.
https://zio.dev/zio-json/decoding#automatic-derivation-and-case-class-default-field-values
Что я здесь делаю не так?
import zio.*
import zio.json.*
import zio.schema.{DeriveSchema, Schema}
final case class Person(personId: Long = 1, name: String, age: Int)
object Person {
implicit val decoder: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person]
}
object MyApp extends ZIOAppDefault {
override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- Console.printLine("Decoding a JSON string representing a person...")
jsonString =
"""
{
"name": "John",
"age": 30
}
""".stripMargin
result <- ZIO.fromEither(jsonString.fromJson[Person])
_ <- Console.printLine(s"Decoded Person: $result")
} yield ExitCode.success
}
Выход :
Decoding a JSON string representing a person...
timestamp=2023-11-02T21:11:01.198368Z level=ERROR thread=#zio-fiber-1 message="" cause="Exception in thread "zio-fiber-4" java.lang.String: .personId(missing)
at <empty>.MyApp.run(MyApp2.scala:25)
at <empty>.MyApp.run(MyApp2.scala:27)"
Process finished with exit code 1
Версии:
val zioVersion = "2.0.13"
val zioJsonVersion = "0.5.0"
scala version 3.2.2