Skip to content

Commit

Permalink
Make the Decoder for Currency case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
jcazevedo committed Apr 26, 2023
1 parent 7e076ad commit 89f59f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ trait ExtraMiscJsonProtocol {
Decoder[Json].emapPrettyTry(json => Try(ConfigFactory.parseString(json.toString)))

implicit def currencyDecoder(implicit moneyContext: MoneyContext): Decoder[Currency] =
Decoder[String].emapPrettyTry(Currency(_))
Decoder[String].emapPrettyTry(s => Currency(s.toUpperCase))
implicit val currencyEncoder: Encoder[Currency] = Encoder[String].contramap(_.toString)

/** Encodes a map as an array of key-value objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ class ExtraJsonProtocolSpec extends Specification {

"provide an Encoder and Decoder for Currency" in {
implicit val moneyContext: MoneyContext = MoneyContext(EUR, defaultCurrencySet, Nil)
val currency: Currency = USD
val currencyString = """"USD""""
val usd: Currency = USD

currency.asJson.noSpaces mustEqual currencyString
decode[Currency](currencyString) must beRight(currency)
decode[Currency]("EU") must beLeft
usd.asJson.noSpaces mustEqual "\"USD\""
decode[Currency]("\"USD\"") must beRight(usd)
decode[Currency]("\"usd\"") must beRight(usd)
decode[Currency]("\"EU\"") must beLeft
}

"provide an Encoder and Decoder for a Map as an array of json objects" in {
Expand Down

0 comments on commit 89f59f9

Please sign in to comment.