-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61b7634
commit 108af08
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
zio-json-interop-newtype/shared/src/main/scala/zio/json/interop/newtype/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package zio.json.interop | ||
|
||
import io.estatico.newtype.Coercible | ||
import io.estatico.newtype.ops._ | ||
import zio.json.{ JsonDecoder, JsonEncoder, JsonFieldDecoder, JsonFieldEncoder } | ||
|
||
package object newtype { | ||
implicit def coercibleDecoder[A: Coercible[B, *], B: JsonDecoder]: JsonDecoder[A] = | ||
JsonDecoder[B].map(_.coerce[A]) | ||
|
||
implicit def coercibleEncoder[A: Coercible[*, B], B: JsonEncoder]: JsonEncoder[A] = | ||
JsonEncoder[B].contramap(_.coerce[B]) | ||
|
||
implicit def coercibleKeyDecoder[A: Coercible[B, *], B: JsonFieldDecoder]: JsonFieldDecoder[A] = | ||
JsonFieldDecoder[B].map(_.coerce[A]) | ||
|
||
implicit def coercibleKeyEncoder[A: Coercible[*, B], B: JsonFieldEncoder]: JsonFieldEncoder[A] = | ||
JsonFieldEncoder[B].contramap(_.coerce[B]) | ||
} |
22 changes: 22 additions & 0 deletions
22
zio-json-interop-newtype/shared/src/test/scala/zio/json/interop/newtype/NewtypeSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package zio.json.interop.newtype | ||
|
||
import io.estatico.newtype.macros.newtype | ||
import zio.json.{ DecoderOps, DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder } | ||
import zio.test.Assertion.{ equalTo, isRight } | ||
import zio.test.{ Spec, ZIOSpecDefault, assert, assertTrue } | ||
|
||
object NewtypeSpec extends ZIOSpecDefault { | ||
override def spec: Spec[Environment, Any] = suite("newtype")( | ||
test("newtype") { | ||
assert("""{"name":"fommil"}""".fromJson[Person])(isRight(equalTo(Person(Name("fommil"))))) && | ||
assertTrue(Person(Name("fommil")).toJson == """{"name":"fommil"}""") | ||
} | ||
) | ||
|
||
@newtype case class Name(value: String) | ||
case class Person(name: Name) | ||
object Person { | ||
implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person] | ||
implicit val decoder: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person] | ||
} | ||
} |