Skip to content

Commit

Permalink
Fix toJsonAST implementation for product types + More efficient `to…
Browse files Browse the repository at this point in the history
…JsonAST` implementation for product types and collections + Update magnolia to 1.3.16 (#1344)

* Fix implementation of `toJsonAST` for product types

* Update magnolia to 1.3.16

* More efficient `toJsonAST` for collections
  • Loading branch information
plokhotnyuk authored Feb 26, 2025
1 parent 0dd96c4 commit 1e27996
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 61 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ lazy val zioJson = crossProject(JSPlatform, JVMPlatform, NativePlatform)
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) =>
Seq(
"com.softwaremill.magnolia1_3" %%% "magnolia" % "1.3.15"
"com.softwaremill.magnolia1_3" %%% "magnolia" % "1.3.16"
)
case _ =>
Seq(
Expand Down
14 changes: 9 additions & 5 deletions zio-json/shared/src/main/scala-2.x/zio/json/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -551,22 +551,26 @@ object DeriveJsonEncoder {
}

override final def toJsonAST(a: A): Either[String, Json] = {
val buf = Array.newBuilder[(String, Json)]
val fields = this.fields
var idx = 0
var buf = new Array[(String, Json)](fields.length)
var i, idx = 0
while (idx < fields.length) {
val field = fields(idx)
idx += 1
val p = field.p.dereference(a)
if (field.skip(p)) ()
else {
field.encoder.toJsonAST(p) match {
case Right(value) => buf += ((field.name, value))
case _ =>
case Right(value) =>
buf(i) = (field.name, value)
i += 1
case left =>
return left
}
}
}
new Right(Json.Obj(Chunk.fromArray(buf.result())))
if (i != buf.length) buf = java.util.Arrays.copyOf(buf, i)
new Right(Json.Obj(Chunk.fromArray(buf)))
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions zio-json/shared/src/main/scala-3/zio/json/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -571,22 +571,26 @@ sealed class JsonEncoderDerivation(config: JsonCodecConfiguration) extends Deriv
}

override final def toJsonAST(a: A): Either[String, Json] = {
val buf = Array.newBuilder[(String, Json)]
val fields = this.fields
var idx = 0
var buf = new Array[(String, Json)](fields.length)
var i, idx = 0
while (idx < fields.length) {
val field = fields(idx)
idx += 1
val p = field.p.deref(a)
if (field.skip(p)) ()
else {
field.encoder.toJsonAST(p) match {
case Right(value) => buf += ((field.name, value))
case _ =>
case Right(value) =>
buf(i) = (field.name, value)
i += 1
case left =>
return left
}
}
}
new Right(Json.Obj(Chunk.fromArray(buf.result())))
if (i != buf.length) buf = java.util.Arrays.copyOf(buf, i)
new Right(Json.Obj(Chunk.fromArray(buf)))
}
}
}
Expand Down
Loading

0 comments on commit 1e27996

Please sign in to comment.