zio-json icon indicating copy to clipboard operation
zio-json copied to clipboard

JsonDecoder.{zip, zipRight, ...} don't preserve their laziness

Open felher opened this issue 2 years ago • 0 comments

As discussed on discord:

Even though zip, zipLeft, zipRight and zipWith on JsonDecoder all take their second decoder by name, they internally delegate to JsonDecoder.tuple2. This renders them effectively non-lazy.

Here is an example of code which one might expect to work but which throws a stack overflow error:

import zio.json.*

final case class Tree(value: Int, children: List[Tree])
object Tree:
  implicit def decoder: JsonDecoder[Tree] =
    JsonDecoder[Int].zipWith(JsonDecoder.list(decoder))(Tree.apply)

@main()
def run = 
  println("[1, [[2, []]]]".fromJson[Tree])

felher avatar Mar 09 '22 20:03 felher