Skip to content
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

Improvement: Allow selection of amount representation for individual Joda Money fields #18

Open
apankowski opened this issue Dec 12, 2021 · 3 comments
Labels
joda-money Issue related joda-money datatype module

Comments

@apankowski
Copy link
Contributor

Intro

For Joda Money module, we have the possibility to select the amount representation on module level. Amount representation varies from API to API. In projects that involve integration with many APIs it would be beneficial to allow such selection on field-level. This ticket collects possible approaches.

Appoaches

To achieve field-level selection we could:

  1. Leverage @JsonFormat's shape (see comment #1 and comment #2):
@JsonFormat(shape = NUMBER_INT)
Money grandTotal;

Characteristics:

  • This has the benefit of leveraging existing annotation related to formatting without the need to introduce a new one.
  • We will still have to document the behavior of @JsonFormat on Money (de)serializers as it won't be obvious what shapes are supported and what effect they have.
  • It might be a bit confusing to some, as the annotation doesn't influence the field itself but rather a particular nested property.
  • It doesn't help with potential further extensions. As an example, the naming of the properties varies from API to API - I've integrated systems with services using currencyCode instead of currency and value instead of amount as the names of the individual properties carrying the monetary amount. If we wanted to allow customization of the names in the future, most likely we wouldn't do that via @JsonFormat annotation but rather a dedicated one.
  1. Introduce dedicated annotation (here: @JsonMoney) allowing selection of amount representation (see comment):
@JsonMoney(amountRepresentation = MINOR_CURRENCY_UNIT)
Money grandTotal;

Characteristics:

  • We have to introduce a new annotation, mention it in README and document in JavaDocs.
  • We are open for further extension (argument with custom property names above).
  1. Do a mix of the two - extend @JsonFormat to allow passing (de)serializer-specific annotations (here: via new with attribute):
@JsonFormat(with = @JsonMoney(amountRepresentation = MINOR_CURRENCY_UNIT))
Money grandTotal;

Characteristics:

  • We configure the format the way users might be used to do it.
  • We are open for further extension (argument with custom property names above).
  • We have to introduce a new annotation, mention it in README and document in JavaDocs.
  • We have to modify @JsonFormat just for the sake of passing custom annotations.

Implementation

Regardless of the approach, most likely Contextual(De)Serializer would be used to build the (de)serializer.

Discussion

Do you see some more characteristics of these approaches?
Do you see a clear winner? (I'm slightly leaning towards approach 2)

Of course we don't have to pull the trigger now - might as well keep this open until we have more insights or convictions. Just wanted to write down my thoughts.

@cowtowncoder
Copy link
Member

I think this was fixed via #17 (and release notes for 2.14.0 refer to it). Closing.

@apankowski
Copy link
Contributor Author

hey @cowtowncoder 🙂

I believe the functionality described in this enhancement (as it's not really an issue) was not part of #17. In the course of #17 only global selection of the amount representation was done, which applies to (de)serialization of all Money fields. This enhancement pertains to the possibility of overriding the global amount representation for any particular Money field. My goal was to discuss possible approaches in order to select the best one and implement it in the future.

@cowtowncoder
Copy link
Member

Ah. Ok, will reopen.

@cowtowncoder cowtowncoder reopened this Jan 10, 2022
@cowtowncoder cowtowncoder added the joda-money Issue related joda-money datatype module label Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
joda-money Issue related joda-money datatype module
Projects
None yet
Development

No branches or pull requests

2 participants