Skip to content

Commit

Permalink
Rename MoneyModule, use addModule in ReadMe, update License year
Browse files Browse the repository at this point in the history
  • Loading branch information
sri-adarsh-kumar committed Jan 23, 2025
1 parent f347a4c commit 90bd5ae
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mapper.registerModule(new JSONPModule()); // new (jakarta) json-P API
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JsonOrgModule())
.addModule(new JodaMoneyModule())
.addModule(new MoneyModule())
.addModule(new JavaxMoneyModule())
// ONE of these (not both):
.addModule(new JSR353Module()) // old (javax) json-p API
.addModule(new JSONPModule()) // new (jakarta) json-P API
Expand Down
67 changes: 35 additions & 32 deletions javax-money/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ actual version will be selected by a profile based on the current JDK version.
Register the module with your `ObjectMapper`:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule());
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule())
.build();
```

Alternatively, you can use the SPI capabilities:
Expand All @@ -81,8 +82,9 @@ and will, by default, serialize it as:
To serialize number as a JSON string, you have to configure the quoted decimal number value serializer:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule().withQuotedDecimalNumbers());
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule().withQuotedDecimalNumbers())
.build();
```

```json
Expand All @@ -98,16 +100,18 @@ A special feature for serializing monetary amounts is *formatting*, which is **d
have to either enable default formatting:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule().withDefaultFormatting());
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule().withDefaultFormatting())
.build();
```

... or pass in a `MonetaryAmountFormatFactory` implementation to the `MoneyModule`:
... or pass in a `MonetaryAmountFormatFactory` implementation to the `JavaxMoneyModule`:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
.withFormatting(new CustomMonetaryAmountFormatFactory()));
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule()
.withFormatting(new CustomMonetaryAmountFormatFactory()))
.build();
```

The default formatting delegates directly to `MonetaryFormats.getAmountFormat(Locale, String...)`.
Expand All @@ -122,9 +126,7 @@ The first example serializes a monetary amount using the `de_DE` locale:

```java
ObjectWriter writer = mapper.writer().with(Locale.GERMANY);
writer.

writeValueAsString(Money.of(29.95, "EUR"));
writer.writeValueAsString(Money.of(29.95, "EUR"));
```

```json
Expand All @@ -139,9 +141,7 @@ The following example uses `en_US`:

```java
ObjectWriter writer = mapper.writer().with(Locale.US);
writer.

writeValueAsString(Money.of(29.95, "USD"));
writer.writeValueAsString(Money.of(29.95, "USD"));
```

```json
Expand All @@ -158,45 +158,48 @@ More sophisticated formatting rules can be supported by implementing `MonetaryAm

This module will use `org.javamoney.moneta.Money` as an implementation for `javax.money.MonetaryAmount` by default when
deserializing money values. If you need a different implementation, you can pass a different `MonetaryAmountFactory`
to the `MoneyModule`:
to the `JavaxMoneyModule`:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
.withMonetaryAmount(new CustomMonetaryAmountFactory()));
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule()
.withMonetaryAmount(new CustomMonetaryAmountFactory()))
.build();
```

You can also pass in a method reference:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
.withMonetaryAmount(FastMoney::of));
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule()
.withMonetaryAmount(FastMoney::of))
.build();
```

*Jackson Datatype Money* comes with support for all `MonetaryAmount` implementations from Moneta, the reference
implementation of JavaMoney:

| `MonetaryAmount` Implementation | Factory |
|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| `org.javamoney.moneta.FastMoney` | [`new MoneyModule().withFastMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/FastMoneyFactory.java) |
| `org.javamoney.moneta.Money` | [`new MoneyModule().withMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/MoneyFactory.java) |
| `org.javamoney.moneta.RoundedMoney` | [`new MoneyModule().withRoundedMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/RoundedMoneyFactory.java) | |
| `MonetaryAmount` Implementation | Factory |
|-------------------------------------|----------------------------------------------------------------------------------------------------------------------------|
| `org.javamoney.moneta.FastMoney` | [`new JavaxMoneyModule().withFastMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/FastMoneyFactory.java) |
| `org.javamoney.moneta.Money` | [`new JavaxMoneyModule().withMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/MoneyFactory.java) |
| `org.javamoney.moneta.RoundedMoney` | [`new JavaxMoneyModule().withRoundedMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/RoundedMoneyFactory.java) | |

Module supports deserialization of amount number from JSON number as well as from JSON string without any special
configuration required.

### Custom Field Names

As you have seen in the previous examples the `MoneyModule` uses the field names `amount`, `currency` and `formatted`
As you have seen in the previous examples the `JavaxMoneyModule` uses the field names `amount`, `currency` and `formatted`
by default. Those names can be overridden if desired:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaxMoneyModule()
.withAmountFieldName("value")
.withCurrencyFieldName("unit")
.withFormattedFieldName("pretty"));
.withFormattedFieldName("pretty"))
.build();
```

## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@API(status = STABLE)
public final class MoneyModule extends Module {
public final class JavaxMoneyModule extends Module {

private final AmountWriter<?> writer;
private final FieldNames names;
Expand All @@ -30,18 +30,18 @@ public final class MoneyModule extends Module {
private final MonetaryAmountFactory<Money> moneyFactory;
private final MonetaryAmountFactory<RoundedMoney> roundedMoneyFactory;

public MoneyModule() {
public JavaxMoneyModule() {
this(new DecimalAmountWriter(), FieldNames.defaults(), MonetaryAmountFormatFactory.NONE,
Money::of, FastMoney::of, Money::of, RoundedMoney::of);
}

private MoneyModule(final AmountWriter<?> writer,
final FieldNames names,
final MonetaryAmountFormatFactory formatFactory,
final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
final MonetaryAmountFactory<FastMoney> fastMoneyFactory,
final MonetaryAmountFactory<Money> moneyFactory,
final MonetaryAmountFactory<RoundedMoney> roundedMoneyFactory) {
private JavaxMoneyModule(final AmountWriter<?> writer,
final FieldNames names,
final MonetaryAmountFormatFactory formatFactory,
final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
final MonetaryAmountFactory<FastMoney> fastMoneyFactory,
final MonetaryAmountFactory<Money> moneyFactory,
final MonetaryAmountFactory<RoundedMoney> roundedMoneyFactory) {

this.writer = writer;
this.names = names;
Expand All @@ -54,7 +54,7 @@ private MoneyModule(final AmountWriter<?> writer,

@Override
public String getModuleName() {
return MoneyModule.class.getSimpleName();
return JavaxMoneyModule.class.getSimpleName();
}

@Override
Expand All @@ -79,89 +79,89 @@ public void setupModule(final SetupContext context) {
context.addDeserializers(deserializers);
}

public MoneyModule withDecimalNumbers() {
public JavaxMoneyModule withDecimalNumbers() {
return withNumbers(new DecimalAmountWriter());
}

public MoneyModule withQuotedDecimalNumbers() {
public JavaxMoneyModule withQuotedDecimalNumbers() {
return withNumbers(new QuotedDecimalAmountWriter());
}

@API(status = EXPERIMENTAL)
public MoneyModule withNumbers(final AmountWriter<?> writer) {
return new MoneyModule(writer, names, formatFactory, amountFactory,
public JavaxMoneyModule withNumbers(final AmountWriter<?> writer) {
return new JavaxMoneyModule(writer, names, formatFactory, amountFactory,
fastMoneyFactory, moneyFactory, roundedMoneyFactory);
}

/**
* @see FastMoney
* @return new {@link MoneyModule} using {@link FastMoney}
* @return new {@link JavaxMoneyModule} using {@link FastMoney}
*/
public MoneyModule withFastMoney() {
public JavaxMoneyModule withFastMoney() {
return withMonetaryAmount(fastMoneyFactory);
}

/**
* @see Money
* @return new {@link MoneyModule} using {@link Money}
* @return new {@link JavaxMoneyModule} using {@link Money}
*/
public MoneyModule withMoney() {
public JavaxMoneyModule withMoney() {
return withMonetaryAmount(moneyFactory);
}

/**
* @see RoundedMoney
* @return new {@link MoneyModule} using {@link RoundedMoney}
* @return new {@link JavaxMoneyModule} using {@link RoundedMoney}
*/
public MoneyModule withRoundedMoney() {
public JavaxMoneyModule withRoundedMoney() {
return withMonetaryAmount(roundedMoneyFactory);
}

/**
* @see RoundedMoney
* @param rounding the rounding operator
* @return new {@link MoneyModule} using {@link RoundedMoney} with the given {@link MonetaryRounding}
* @return new {@link JavaxMoneyModule} using {@link RoundedMoney} with the given {@link MonetaryRounding}
*/
public MoneyModule withRoundedMoney(final MonetaryOperator rounding) {
public JavaxMoneyModule withRoundedMoney(final MonetaryOperator rounding) {
final MonetaryAmountFactory<RoundedMoney> factory = (amount, currency) ->
RoundedMoney.of(amount, currency, rounding);

return new MoneyModule(writer, names, formatFactory, factory,
return new JavaxMoneyModule(writer, names, formatFactory, factory,
fastMoneyFactory, moneyFactory, factory);
}

public MoneyModule withMonetaryAmount(final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory) {
return new MoneyModule(writer, names, formatFactory, amountFactory,
public JavaxMoneyModule withMonetaryAmount(final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory) {
return new JavaxMoneyModule(writer, names, formatFactory, amountFactory,
fastMoneyFactory, moneyFactory, roundedMoneyFactory);
}

public MoneyModule withoutFormatting() {
public JavaxMoneyModule withoutFormatting() {
return withFormatting(MonetaryAmountFormatFactory.NONE);
}

public MoneyModule withDefaultFormatting() {
public JavaxMoneyModule withDefaultFormatting() {
return withFormatting(MonetaryFormats::getAmountFormat);
}

public MoneyModule withFormatting(final MonetaryAmountFormatFactory formatFactory) {
return new MoneyModule(writer, names, formatFactory, amountFactory,
public JavaxMoneyModule withFormatting(final MonetaryAmountFormatFactory formatFactory) {
return new JavaxMoneyModule(writer, names, formatFactory, amountFactory,
fastMoneyFactory, moneyFactory, roundedMoneyFactory);
}

public MoneyModule withAmountFieldName(final String name) {
public JavaxMoneyModule withAmountFieldName(final String name) {
return withFieldNames(names.withAmount(name));
}

public MoneyModule withCurrencyFieldName(final String name) {
public JavaxMoneyModule withCurrencyFieldName(final String name) {
return withFieldNames(names.withCurrency(name));
}

public MoneyModule withFormattedFieldName(final String name) {
public JavaxMoneyModule withFormattedFieldName(final String name) {
return withFieldNames(names.withFormatted(name));
}

private MoneyModule withFieldNames(final FieldNames names) {
return new MoneyModule(writer, names, formatFactory, amountFactory,
private JavaxMoneyModule withFieldNames(final FieldNames names) {
return new JavaxMoneyModule(writer, names, formatFactory, amountFactory,
fastMoneyFactory, moneyFactory, roundedMoneyFactory);
}

Expand Down
2 changes: 1 addition & 1 deletion javax-money/src/main/resources/META-INF/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2016 Zalando SE
Copyright (c) 2025-2026 Zalando SE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.fasterxml.jackson.datatype.money.MoneyModule
com.fasterxml.jackson.datatype.money.JavaxMoneyModule
2 changes: 1 addition & 1 deletion javax-money/src/moditect/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
exports com.fasterxml.jackson.datatype.money;

provides com.fasterxml.jackson.databind.Module with
com.fasterxml.jackson.datatype.money.MoneyModule;
com.fasterxml.jackson.datatype.money.JavaxMoneyModule;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public final class CurrencyUnitDeserializerTest {

private final ObjectMapper unit = JsonMapper.builder().addModule(new MoneyModule()).build();
private final ObjectMapper unit = JsonMapper.builder().addModule(new JavaxMoneyModule()).build();

@Test
public void shouldDeserialize() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public final class CurrencyUnitSchemaSerializerTest {

private final ObjectMapper unit = new ObjectMapper().registerModule(new MoneyModule());
private final ObjectMapper unit = new ObjectMapper().registerModule(new JavaxMoneyModule());

@Test
public void shouldSerializeJsonSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public final class MonetaryAmountDeserializerTest<M extends MonetaryAmount> {
@SuppressWarnings("unused")
private Object[] data() {
return $($(Money.class, (Configurer) module -> module),
$(FastMoney.class, (Configurer) module -> new MoneyModule().withFastMoney()),
$(Money.class, (Configurer) module -> new MoneyModule().withMoney()),
$(RoundedMoney.class, (Configurer) module -> new MoneyModule().withRoundedMoney()),
$(FastMoney.class, (Configurer) module -> new JavaxMoneyModule().withFastMoney()),
$(Money.class, (Configurer) module -> new JavaxMoneyModule().withMoney()),
$(RoundedMoney.class, (Configurer) module -> new JavaxMoneyModule().withRoundedMoney()),
$(RoundedMoney.class, (Configurer) module -> module.withRoundedMoney(Monetary.getDefaultRounding())));
}

private interface Configurer {
MoneyModule configure(MoneyModule module);
JavaxMoneyModule configure(JavaxMoneyModule module);
}

private ObjectMapper unit(final Configurer configurer) {
Expand All @@ -50,8 +50,8 @@ private ObjectMapper unit(final Module module) {
return new ObjectMapper().registerModule(module);
}

private MoneyModule module(final Configurer configurer) {
return configurer.configure(new MoneyModule());
private JavaxMoneyModule module(final Configurer configurer) {
return configurer.configure(new JavaxMoneyModule());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private ObjectMapper unit(final Module module) {
return new ObjectMapper().registerModule(module);
}

private MoneyModule module() {
return new MoneyModule();
private JavaxMoneyModule module() {
return new JavaxMoneyModule();
}

}
Loading

0 comments on commit 90bd5ae

Please sign in to comment.