Skip to content

Commit

Permalink
encoder/decoder/codec for java.util.Currency (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsprint authored Aug 21, 2023
1 parent c7e474f commit 481ab26
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions zio-json/shared/src/main/scala/zio/json/JsonCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@ private[json] trait CodecLowPriority3 { this: JsonCodec.type =>
implicit val zoneOffset: JsonCodec[ZoneOffset] = JsonCodec(JsonEncoder.zoneOffset, JsonDecoder.zoneOffset)

implicit val uuid: JsonCodec[java.util.UUID] = JsonCodec(JsonEncoder.uuid, JsonDecoder.uuid)

implicit val currency: JsonCodec[java.util.Currency] = JsonCodec(JsonEncoder.currency, JsonDecoder.currency)
}
9 changes: 9 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 @@ -688,6 +688,15 @@ private[json] trait DecoderLowPriority3 extends DecoderLowPriority4 {
case iae: IllegalArgumentException => Left(s"Invalid UUID: ${iae.getMessage}")
}
}

implicit val currency: JsonDecoder[java.util.Currency] =
mapStringOrFail { str =>
try {
Right(java.util.Currency.getInstance(str))
} catch {
case iae: IllegalArgumentException => Left(s"Invalid Currency: ${iae.getMessage}")
}
}
}

private[json] trait DecoderLowPriority4 {
Expand Down
2 changes: 2 additions & 0 deletions zio-json/shared/src/main/scala/zio/json/JsonEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ private[json] trait EncoderLowPriority3 extends EncoderLowPriority4 {
implicit val zoneOffset: JsonEncoder[ZoneOffset] = stringify(serializers.toString)

implicit val uuid: JsonEncoder[UUID] = stringify(_.toString)

implicit val currency: JsonEncoder[java.util.Currency] = stringify(_.toString)
}

private[json] trait EncoderLowPriority4 {
Expand Down
5 changes: 5 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import zio._
import zio.json._
import zio.json.ast.Json
import zio.test.Assertion._
import zio.test.TestAspect.jvmOnly
import zio.test._

import scala.collection.immutable
Expand Down Expand Up @@ -41,6 +42,10 @@ object CodecSpec extends ZIOSpecDefault {
isLeft(equalTo("(expected a 128 bit BigInteger)"))
) && assert(exampleBDString.fromJson[BigDecimal])(isRight(equalTo(BigDecimal(exampleBDString))))
},
test("java.util.Currency") {
val exampleValue = "\"USD\""
assert(exampleValue.fromJson[java.util.Currency])(isRight(equalTo(java.util.Currency.getInstance("USD"))))
} @@ jvmOnly,
test("eithers") {
val bernies = List("""{"a":1}""", """{"left":1}""", """{"Left":1}""")
val trumps = List("""{"b":2}""", """{"right":2}""", """{"Right":2}""")
Expand Down
5 changes: 5 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 @@ -4,6 +4,7 @@ import zio._
import zio.json._
import zio.json.ast.Json
import zio.test.Assertion._
import zio.test.TestAspect.jvmOnly
import zio.test.{ TestAspect, _ }

import java.time.{ Duration, OffsetDateTime, ZonedDateTime }
Expand Down Expand Up @@ -264,6 +265,10 @@ object DecoderSpec extends ZIOSpecDefault {
assert(bad6.fromJson[UUID])(isLeft(containsString("Invalid UUID: 64d7c38d-2afd-X-9832-4e70afe4b0f8"))) &&
assert(bad7.fromJson[UUID])(isLeft(containsString("Invalid UUID: 0-0-0-0-00000000000000000")))
},
test("java.util.Currency") {
assert(""""USD"""".fromJson[java.util.Currency])(isRight(equalTo(java.util.Currency.getInstance("USD")))) &&
assert(""""LLL"""".fromJson[java.util.Currency])(isLeft)
} @@ jvmOnly,
test("java.time.Duration") {
val ok1 = """"PT1H2M3S""""
val ok2 = """"PT-0.5S"""" // see https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8054978
Expand Down
4 changes: 4 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/EncoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testzio.json
import zio.json._
import zio.json.ast.Json
import zio.test.Assertion._
import zio.test.TestAspect.jvmOnly
import zio.test._
import zio.{ Chunk, NonEmptyChunk }

Expand Down Expand Up @@ -265,6 +266,9 @@ object EncoderSpec extends ZIOSpecDefault {
equalTo(""""e142f1aa-6e9e-4352-adfe-7e6eb9814ccd"""")
)
},
test("java.util.Currency") {
assert(java.util.Currency.getInstance("USD").toJson)(equalTo(""""USD""""))
} @@ jvmOnly,
test("parameterless products") {
import exampleproducts._

Expand Down

0 comments on commit 481ab26

Please sign in to comment.