Skip to content

Commit

Permalink
Option.map: Pass None to new decoder when empty (zio#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsvehla authored Mar 13, 2021
1 parent 449499d commit 30c6b8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions zio-json/shared/src/main/scala/zio/json/decoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ trait JsonDecoder[A] extends JsonDecoderPlatformSpecific[A] { self =>
/**
* Returns a new codec whose decoded values will be mapped by the specified function.
*/
final def map[B](f: A => B): JsonDecoder[B] =
def map[B](f: A => B): JsonDecoder[B] =
new JsonDecoder[B] {

def unsafeDecode(trace: List[JsonError], in: RetractReader): B =
Expand Down Expand Up @@ -260,7 +260,7 @@ object JsonDecoder extends GeneratedTupleDecoders with DecoderLowPriority0 {
// If alternative behaviour is desired, e.g. pass null to the underlying, then
// use a newtype wrapper.
implicit def option[A](implicit A: JsonDecoder[A]): JsonDecoder[Option[A]] =
new JsonDecoder[Option[A]] {
new JsonDecoder[Option[A]] { self =>
private[this] val ull: Array[Char] = "ull".toCharArray

override def unsafeDecodeMissing(trace: List[JsonError]): Option[A] =
Expand All @@ -275,6 +275,17 @@ object JsonDecoder extends GeneratedTupleDecoders with DecoderLowPriority0 {
in.retract()
Some(A.unsafeDecode(trace, in))
}

// overridden here to pass `None` to the new Decoder instead of throwing
// when called from a derived decoder
override def map[B](f: Option[A] => B): JsonDecoder[B] =
new JsonDecoder[B] {
override def unsafeDecodeMissing(trace: List[JsonError]): B =
f(None)

def unsafeDecode(trace: List[JsonError], in: RetractReader): B =
f(self.unsafeDecode(trace, in))
}
}

// supports multiple representations for compatibility with other libraries,
Expand Down
24 changes: 24 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 @@ -72,6 +72,30 @@ object DecoderSpec extends DefaultRunnableSpec {
test("unicode") {
assert(""""€🐵🥰"""".fromJson[String])(isRight(equalTo("€🐵🥰")))
},
test("Option: .map on derived JsonDecoder with missing value") {
// More information about use case here https://github.com/zio/zio-json/issues/198
// User wants to derive an alternative encoding of optionality
sealed trait Assumed[+A]

object Assumed {
case object MissingAssumed extends Assumed[Nothing]
case class FoundAssumed[A](v: A) extends Assumed[A]

implicit def decoder[A](implicit decoding: JsonDecoder[A]): JsonDecoder[Assumed[A]] =
JsonDecoder.option[A].map {
case None => Assumed.MissingAssumed
case Some(v) => Assumed.FoundAssumed[A](v)
}
}

case class Example(a: Assumed[Boolean])
implicit val exampleDecoder: JsonDecoder[Example] = DeriveJsonDecoder.gen[Example]

assert("""{ "a": null }""".fromJson[Example])(isRight(equalTo(Example(Assumed.MissingAssumed)))) &&
assert("""{ "a": true }""".fromJson[Example])(isRight(equalTo(Example(Assumed.FoundAssumed(true))))) &&
assert("""{ "a": false }""".fromJson[Example])(isRight(equalTo(Example(Assumed.FoundAssumed(false))))) &&
assert("""{ }""".fromJson[Example])(isRight(equalTo(Example(Assumed.MissingAssumed))))
},
test("Seq") {
val jsonStr = """["5XL","2XL","XL"]"""
val expected = Seq("5XL", "2XL", "XL")
Expand Down

0 comments on commit 30c6b8a

Please sign in to comment.