FasterXML / jackson-datatypes-misc

Collection of common Jackson datatype modules not part of other multi-project repos
Apache License 2.0
22 stars 19 forks source link

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

Open apankowski opened 2 years ago

apankowski commented 2 years ago

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:

  1. Introduce dedicated annotation (here: @JsonMoney) allowing selection of amount representation (see comment):
@JsonMoney(amountRepresentation = MINOR_CURRENCY_UNIT)
Money grandTotal;

Characteristics:

  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:

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 commented 2 years ago

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

apankowski commented 2 years ago

hey @cowtowncoder :slightly_smiling_face:

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 commented 2 years ago

Ah. Ok, will reopen.