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

Add ability to decode json in different ways and combine the results #893

Closed
wants to merge 2 commits into from

Conversation

pjoneswork
Copy link

@pjoneswork pjoneswork commented Mar 3, 2023

This is a different zip functionality. Instead of combining decoders with inside a tuple, this allows us to decode the same json twice.

Given the setup:

final case class Foo(a: Int)
final case class Bar(b: String)

implicit val fooDecoder: JsonDecoder[Foo] = DeriveJsonDecoder.gen[Foo]
implicit val barDecoder: JsonDecoder[Bar] = DeriveJsonDecoder.gen[Bar]

The old zip does

implicit val fooAndBarDecoder: JsonDecoder[(Foo, Bar)] = JsonDecoder[Foo].zip(JsonDecoder[Bar])
val json = """[{"a": 1}, {"b": "2"}]"""
val result: (Foo, Bar) = json.fromJson[(Foo, Bar)]

while newZip does

implicit val fooAndBarDecoder: JsonDecoder[(Foo, Bar)] = JsonDecoder[Foo].newZip(JsonDecoder[Bar])

val json = """{"a": 1, "b": "2"}"""
val result: (Foo, Bar) = json.fromJson[(Foo, Bar)]

Fixes #487

@pjoneswork pjoneswork requested a review from a team as a code owner March 3, 2023 20:02
* Returns a new codec that combines this codec and the specified codec into a single codec that
* decodes the input into a tuple of the values by the respective codecs.
*/
final def newZip[B](that: => JsonDecoder[B]): JsonDecoder[(A, B)] = new JsonDecoder[(A, B)] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good feature. But the naming is nod good ;). I think the right name is and.
@fsvehla maybe you have an opinion too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the name is meh. and is a good suggestion :)

@pjoneswork pjoneswork closed this Jan 16, 2024
@pjoneswork pjoneswork reopened this Jan 16, 2024
@987Nabil 987Nabil closed this Jan 26, 2025
@paulpdaniels paulpdaniels mentioned this pull request Jan 28, 2025
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 this pull request may close these issues.

Behavior of zip
3 participants