Skip to content

Commit

Permalink
🤦
Browse files Browse the repository at this point in the history
  • Loading branch information
fommil committed Sep 25, 2020
1 parent 555717e commit 6e04229
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions zio-json/shared/src/main/scala/zio/json/codecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ object JsonCodec {
* Derives a `JsonCodec[A]` from an encoder and a decoder.
*/
implicit def apply[A](implicit encoder0: JsonEncoder[A], decoder0: JsonDecoder[A]): JsonCodec[A] =
new JsonCodec[A] {
def encoder: JsonEncoder[A] = encoder0

def decoder: JsonDecoder[A] = decoder0

def unsafeDecode(trace: List[JsonError], in: RetractReader): A =
decoder0.unsafeDecode(trace, in)

def unsafeEncode(a: A, indent: Option[Int], out: Write): Unit =
encoder0.unsafeEncode(a, indent, out)
(encoder0, decoder0) match {
case (e: JsonCodec[_], d: JsonCodec[_]) =>
// protects against cycles in implicit resolution, unfortunately the
// instantiation of decoder0 could have been wasteful.
e
case other =>
new JsonCodec[A] {
def encoder: JsonEncoder[A] = encoder0

def decoder: JsonDecoder[A] = decoder0

def unsafeDecode(trace: List[JsonError], in: RetractReader): A =
decoder0.unsafeDecode(trace, in)

def unsafeEncode(a: A, indent: Option[Int], out: Write): Unit =
encoder0.unsafeEncode(a, indent, out)
}
}
}

0 comments on commit 6e04229

Please sign in to comment.