-
Notifications
You must be signed in to change notification settings - Fork 23
Add configurable amount representations for Joda Money module #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cowtowncoder
merged 11 commits into
FasterXML:2.14
from
apankowski:feature/add-joda-money-amount-representations
Nov 30, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
364740a
Add possibility to select amount representation for Joda Money (de)se…
apankowski 865bb15
Add tests for different amount representations for Joda Money (de)ser…
apankowski f7b5149
Add description of configuration of amount representation for Joda Mo…
apankowski f5eb862
Rename vague Representer classes to Converter along with interface me…
apankowski d789f73
Use default field serialization in Joda Money serializer
apankowski 63a41f8
Add example usage of amount representation to README
apankowski 1bb263d
Remove generics from amount converter interface
apankowski fc18e84
Reduce visibility of amount representation-related classes from publi…
apankowski d1ebbaf
Add public default constructors for Money (de)serializers for 2.x bac…
apankowski ab9d78d
Add JavaDoc to classes related to representation of Money amounts
apankowski d3ae41a
Add references to corresponding amount converter classes to JavaDocs …
apankowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
joda-money/src/main/java/com/fasterxml/jackson/datatype/jodamoney/AmountConverter.java
This file contains hidden or 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,18 @@ | ||
| package com.fasterxml.jackson.datatype.jodamoney; | ||
|
|
||
| import org.joda.money.CurrencyUnit; | ||
| import org.joda.money.Money; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * Common interface for amount conversion strategies used by {@link Money} (de)serializer. | ||
| * Allows conversion of {@code Money} to implementation-specific representation of its amount, | ||
| * and back to {@code Money}. | ||
| */ | ||
| interface AmountConverter { | ||
|
|
||
| Object fromMoney(Money money); | ||
|
|
||
| Money toMoney(CurrencyUnit currencyUnit, BigDecimal amount); | ||
| } |
36 changes: 36 additions & 0 deletions
36
joda-money/src/main/java/com/fasterxml/jackson/datatype/jodamoney/AmountRepresentation.java
This file contains hidden or 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,36 @@ | ||
| package com.fasterxml.jackson.datatype.jodamoney; | ||
|
|
||
| /** | ||
| * Enumeration of available strategies used by {@link MoneySerializer} and {@link MoneyDeserializer} | ||
| * to represent amounts of {@link org.joda.money.Money Money}. | ||
| */ | ||
| public enum AmountRepresentation { | ||
|
|
||
| /** | ||
| * Decimal number representation, where amount is (de)serialized as decimal number equal | ||
| * to {@link org.joda.money.Money Money}'s amount, e.g. {@code 12.34} for | ||
| * {@code Money.parse("EUR 12.34")}. | ||
| * | ||
| * @see DecimalNumberAmountConverter | ||
| */ | ||
| DECIMAL_NUMBER, | ||
apankowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Decimal string representation, where amount is (de)serialized as string containing decimal | ||
| * number equal to {@link org.joda.money.Money Money}'s amount, e.g. {@code "12.34"} for | ||
| * {@code Money.parse("EUR 12.34")}. | ||
| * | ||
| * @see DecimalStringAmountConverter | ||
| */ | ||
| DECIMAL_STRING, | ||
|
|
||
| /** | ||
| * Minor currency unit representation, where amount is (de)serialized as long integer equal | ||
| * to {@link org.joda.money.Money Money}'s amount expressed in minor currency unit, e.g. | ||
| * {@code 1234} for {@code Money.parse("EUR 12.34")}, {@code 12345} for | ||
| * {@code Money.parse("KWD 12.345")} or {@code 12} for {@code Money.parse("JPY 12")}. | ||
| * | ||
| * @see MinorCurrencyUnitAmountConverter | ||
| */ | ||
| MINOR_CURRENCY_UNIT, | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
.../src/main/java/com/fasterxml/jackson/datatype/jodamoney/DecimalNumberAmountConverter.java
This file contains hidden or 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,37 @@ | ||
| package com.fasterxml.jackson.datatype.jodamoney; | ||
|
|
||
| import org.joda.money.CurrencyUnit; | ||
| import org.joda.money.Money; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
|
|
||
| /** | ||
| * An {@link AmountConverter} converting {@link Money} to its amount represented as | ||
| * {@link BigDecimal decimal number} (such as {@code 12.34} for {@code Money.parse("USD 12.34")}), | ||
| * and back to {@code Money} from this representation. | ||
| */ | ||
| final class DecimalNumberAmountConverter implements AmountConverter { | ||
|
|
||
| private static final DecimalNumberAmountConverter INSTANCE = new DecimalNumberAmountConverter(); | ||
|
|
||
| static DecimalNumberAmountConverter getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| private DecimalNumberAmountConverter() { | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal fromMoney(final Money money) { | ||
| final BigDecimal decimal = money.getAmount(); | ||
| final int decimalPlaces = money.getCurrencyUnit().getDecimalPlaces(); | ||
| final int scale = Math.max(decimal.scale(), decimalPlaces); | ||
| return decimal.setScale(scale, RoundingMode.UNNECESSARY); | ||
| } | ||
|
|
||
| @Override | ||
| public Money toMoney(final CurrencyUnit currencyUnit, final BigDecimal amount) { | ||
| return Money.of(currencyUnit, amount); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/com/fasterxml/jackson/datatype/jodamoney/DecimalStringAmountConverter.java
This file contains hidden or 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,33 @@ | ||
| package com.fasterxml.jackson.datatype.jodamoney; | ||
|
|
||
| import org.joda.money.CurrencyUnit; | ||
| import org.joda.money.Money; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * An {@link AmountConverter} converting {@link Money} to its amount represented as decimal string | ||
| * (such as {@code "12.34"} for {@code Money.parse("USD 12.34")}), and back to {@code Money} from | ||
| * this representation. | ||
| */ | ||
| final class DecimalStringAmountConverter implements AmountConverter { | ||
|
|
||
| private static final DecimalStringAmountConverter INSTANCE = new DecimalStringAmountConverter(); | ||
|
|
||
| static DecimalStringAmountConverter getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| private DecimalStringAmountConverter() { | ||
| } | ||
|
|
||
| @Override | ||
| public String fromMoney(final Money money) { | ||
| return DecimalNumberAmountConverter.getInstance().fromMoney(money).toPlainString(); | ||
| } | ||
|
|
||
| @Override | ||
| public Money toMoney(final CurrencyUnit currencyUnit, final BigDecimal amount) { | ||
| return Money.of(currencyUnit, amount); | ||
| } | ||
| } |
This file contains hidden or 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
34 changes: 34 additions & 0 deletions
34
.../main/java/com/fasterxml/jackson/datatype/jodamoney/MinorCurrencyUnitAmountConverter.java
This file contains hidden or 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,34 @@ | ||
| package com.fasterxml.jackson.datatype.jodamoney; | ||
|
|
||
| import org.joda.money.CurrencyUnit; | ||
| import org.joda.money.Money; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
|
|
||
| /** | ||
| * An {@link AmountConverter} converting {@link Money} to its amount represented in | ||
| * {@link Money#getAmountMinorLong() minor units as a long} (such as {@code 1234} for | ||
| * {@code Money.parse("USD 12.34")}), and back to {@code Money} from this representation. | ||
| */ | ||
| final class MinorCurrencyUnitAmountConverter implements AmountConverter { | ||
|
|
||
| private static final MinorCurrencyUnitAmountConverter INSTANCE = new MinorCurrencyUnitAmountConverter(); | ||
|
|
||
| static MinorCurrencyUnitAmountConverter getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| private MinorCurrencyUnitAmountConverter() { | ||
| } | ||
|
|
||
| @Override | ||
| public Long fromMoney(final Money money) { | ||
| return money.getAmountMinorLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public Money toMoney(final CurrencyUnit currencyUnit, final BigDecimal amount) { | ||
| return Money.of(currencyUnit, amount.movePointLeft(currencyUnit.getDecimalPlaces()), RoundingMode.UNNECESSARY); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.