From 144f2f929626bc88b0f9b11a9bd4fc72ca47048e Mon Sep 17 00:00:00 2001 From: Andriy Plokhotnyuk Date: Mon, 3 Feb 2025 17:35:52 +0100 Subject: [PATCH] More efficient decoding of case classes --- zio-json/shared/src/main/scala-2.x/zio/json/macros.scala | 1 + zio-json/shared/src/main/scala-3/zio/json/macros.scala | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/zio-json/shared/src/main/scala-2.x/zio/json/macros.scala b/zio-json/shared/src/main/scala-2.x/zio/json/macros.scala index 9c2dd8a50..05c831cdc 100644 --- a/zio-json/shared/src/main/scala-2.x/zio/json/macros.scala +++ b/zio-json/shared/src/main/scala-2.x/zio/json/macros.scala @@ -8,6 +8,7 @@ import zio.json.internal.{ FieldEncoder, Lexer, RetractReader, StringMatrix, Wri import scala.annotation._ import scala.language.experimental.macros +import scala.reflect.ClassTag /** * If used on a case class field, determines the name of the JSON field. Defaults to the case class field name. diff --git a/zio-json/shared/src/main/scala-3/zio/json/macros.scala b/zio-json/shared/src/main/scala-3/zio/json/macros.scala index dd619e6da..b9aac0d27 100644 --- a/zio-json/shared/src/main/scala-3/zio/json/macros.scala +++ b/zio-json/shared/src/main/scala-3/zio/json/macros.scala @@ -12,6 +12,7 @@ import zio.json.ast.Json import zio.json.internal.{ FieldEncoder, Lexer, RetractReader, StringMatrix, Write } import scala.annotation._ +import scala.collection.Factory import scala.collection.mutable import scala.language.experimental.macros @@ -516,6 +517,9 @@ sealed class JsonDecoderDerivation(config: JsonCodecConfiguration) extends Deriv private final class ArraySeq(p: Array[Any]) extends IndexedSeq[Any] { def apply(i: Int): Any = p(i) def length: Int = p.length + override def to[A](factory: Factory[Any, A]): A = + if (factory.isInstanceOf[Factory[Any, Array[Any]]]) p.asInstanceOf[A] + else super.to(factory) } } @@ -540,6 +544,9 @@ object DeriveJsonDecoder extends JsonDecoderDerivation(JsonCodecConfiguration.de private final class ArraySeq(p: Array[Any]) extends IndexedSeq[Any] { def apply(i: Int): Any = p(i) def length: Int = p.length + override def to[A](factory: Factory[Any, A]): A = + if (factory.isInstanceOf[Factory[Any, Array[Any]]]) p.asInstanceOf[A] + else super.to(factory) } }