Skip to content

Commit 953f7ed

Browse files
authored
Fix traces of decoding error for tuples (zio#1225)
1 parent 75ec28b commit 953f7ed

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

build.sbt

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,18 @@ lazy val zioJson = crossProject(JSPlatform, JVMPlatform, NativePlatform)
138138
val tparams = (1 to i).map(p => s"A$p").mkString(", ")
139139
val implicits = (1 to i).map(p => s"A$p: JsonDecoder[A$p]").mkString(", ")
140140
val work = (1 to i)
141-
.map(p => s"val a$p = A$p.unsafeDecode(trace :+ traces($p), in)")
141+
.map(p => s"val a$p = A$p.unsafeDecode(traces(${p - 1}) :: trace, in)")
142142
.mkString("\n Lexer.char(trace, in, ',')\n ")
143143
val returns = (1 to i).map(p => s"a$p").mkString(", ")
144144

145145
s"""implicit def tuple$i[$tparams](implicit $implicits): JsonDecoder[Tuple$i[$tparams]] =
146146
| new JsonDecoder[Tuple$i[$tparams]] {
147-
| val traces: Array[JsonError] = (0 to $i).map(JsonError.ArrayAccess(_)).toArray
147+
| private[this] val traces: Array[JsonError] = (0 to ${i - 1}).map(JsonError.ArrayAccess(_)).toArray
148148
| def unsafeDecode(trace: List[JsonError], in: RetractReader): Tuple$i[$tparams] = {
149149
| Lexer.char(trace, in, '[')
150150
| $work
151151
| Lexer.char(trace, in, ']')
152-
| Tuple$i($returns)
152+
| new Tuple$i($returns)
153153
| }
154154
| }""".stripMargin
155155
}

zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ object DecoderSpec extends ZIOSpecDefault {
5454
forall(isRight(isRight(equalTo(2))))
5555
)
5656
},
57+
test("tuples") {
58+
assert("""["a",3]""".fromJson[(String, Int)])(isRight(equalTo(("a", 3))))
59+
assert("""["a","b"]""".fromJson[(String, Int)])(isLeft(equalTo("[1](expected a number, got 'b')")))
60+
assert("""[[0.1,0.2],[0.3,0.4],[-0.3,-]]""".fromJson[Seq[(Double, Double)]])(
61+
isLeft(equalTo("[2][1](expected a Double)"))
62+
)
63+
},
5764
test("parameterless products") {
5865
import exampleproducts._
5966

0 commit comments

Comments
 (0)