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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions zio-json/shared/src/main/scala/zio/json/JsonDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ trait JsonDecoder[A] extends JsonDecoderPlatformSpecific[A] {
final def zipWith[B, C](that: => JsonDecoder[B])(f: (A, B) => C): JsonDecoder[C] =
self.zip(that).map(f.tupled)

/**
* 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 :)

def unsafeDecode(trace: List[JsonError], in: RetractReader): (A, B) = {
val in2 = new zio.json.internal.WithRecordingReader(in, 64)
val a = self.unsafeDecode(trace, in2)
in2.rewind()
val b = that.unsafeDecode(trace, in2)
(a, b)
}
}

/**
* NewZips two codecs, but discards the output on the right hand side.
*/
final def newZipLeft[B](that: => JsonDecoder[B]): JsonDecoder[A] = self.newZipWith(that)((a, _) => a)

/**
* NewZips two codecs, but discards the output on the left hand side.
*/
final def newZipRight[B](that: => JsonDecoder[B]): JsonDecoder[B] = self.newZipWith(that)((_, b) => b)

/**
* NewZips two codecs into one, transforming the outputs of zip codecs by the specified function.
*/
final def newZipWith[B, C](that: => JsonDecoder[B])(f: (A, B) => C): JsonDecoder[C] =
self.newZip(that).map(f.tupled)


def unsafeDecodeMissing(trace: List[JsonError]): A =
throw JsonDecoder.UnsafeJson(JsonError.Message("missing") :: trace)

Expand Down
11 changes: 11 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ object DecoderSpec extends ZIOSpecDefault {
)
)
)
},
test("newZip") {
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]
implicit val fooAndBarDecoder: JsonDecoder[(Foo, Bar)] = JsonDecoder[Foo].newZip(JsonDecoder[Bar])

val json = """{"a": 1, "b": "2"}"""
assert(json.fromJson[(Foo, Bar)])(isRight(equalTo((Foo(1), Bar("2")))))
}
),
suite("fromJsonAST")(
Expand Down
Loading