Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of zip #487

Open
soujiro32167 opened this issue Nov 18, 2021 · 2 comments · May be fixed by #893
Open

Behavior of zip #487

soujiro32167 opened this issue Nov 18, 2021 · 2 comments · May be fixed by #893

Comments

@soujiro32167
Copy link

After using Circe, I expected zip to behave like product:

import zio.json._

case class A(a: Int)
object A {
    implicit val codec: JsonCodec[A] = DeriveJsonCodec.gen
}

case class B(b: Int)
object B {
    implicit val codec: JsonCodec[B] = DeriveJsonCodec.gen
}

implicit val product: JsonDecoder[(A, B)] = JsonDecoder[A] <*> JsonDecoder[B]

"""
{"a":1,"b":2}
""".fromJson[(A, B)]

However, the implementation expects an array. This makes sense as a default, but how can I define the behavior above?

@soujiro32167
Copy link
Author

related: #516

@pjoneswork
Copy link

I've hit this too. I think we need an implementation of zipWith that looks like:

    def newZipWith[B, C](other: JsonDecoder[B])(f: (A, B) => C): JsonDecoder[C] =
      new JsonDecoder[C] {
        def unsafeDecode(trace: List[JsonError], in: RetractReader): C = {
          // Can't actually do this in my code because it's private
          // And can't make my own RetractReader because it's a sealed trait
          val in2 = new zio.json.internal.WithRecordingReader(in) 
          val a = self.unsafeDecode(trace, in2)
          in2.rewind()
          val b = other.unsafeDecode(trace, in2)
          f(a, b)
        }
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants