|
| 1 | +# Jackson Datatype Money |
| 2 | + |
| 3 | +*Jackson Datatype Money* is a [Jackson](https://github.com/codehaus/jackson) module to support JSON serialization and |
| 4 | +deserialization of [JavaMoney](https://github.com/JavaMoney/jsr354-api) data types. It fills a niche, in that it |
| 5 | +integrates JavaMoney and Jackson so that they work seamlessly together, without requiring additional |
| 6 | +developer effort. In doing so, it aims to perform a small but repetitive task — once and for all. |
| 7 | + |
| 8 | +With this library, it is possible to represent monetary amounts in JSON as follows: |
| 9 | + |
| 10 | +```json |
| 11 | +{ |
| 12 | + "amount": 29.95, |
| 13 | + "currency": "EUR" |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +- enables you to express monetary amounts in JSON |
| 20 | +- can be used in a REST APIs |
| 21 | +- customized field names |
| 22 | +- localization of formatted monetary amounts |
| 23 | +- allows you to implement RESTful API endpoints that format monetary amounts based on the Accept-Language header |
| 24 | +- is unique and flexible |
| 25 | + |
| 26 | +## Dependencies |
| 27 | + |
| 28 | +- Java 8 or higher |
| 29 | +- Any build tool using Maven Central, or direct download |
| 30 | +- Jackson |
| 31 | +- JavaMoney |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +Add the following dependency to your project: |
| 36 | + |
| 37 | +```xml |
| 38 | + |
| 39 | +<dependency> |
| 40 | + <groupId>com.fasterxml.jackson.datatype</groupId> |
| 41 | + <artifactId>jackson-datatype-money</artifactId> |
| 42 | + <version>${jackson-datatype-money.version}</version> |
| 43 | +</dependency> |
| 44 | +``` |
| 45 | + |
| 46 | +For ultimate flexibility, this module is compatible with the official version as well as the backport of JavaMoney. The |
| 47 | +actual version will be selected by a profile based on the current JDK version. |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +Register the module with your `ObjectMapper`: |
| 52 | + |
| 53 | +```java |
| 54 | +ObjectMapper mapper = new ObjectMapper() |
| 55 | + .registerModule(new MoneyModule()); |
| 56 | +``` |
| 57 | + |
| 58 | +Alternatively, you can use the SPI capabilities: |
| 59 | + |
| 60 | +```java |
| 61 | +ObjectMapper mapper = new ObjectMapper() |
| 62 | + .findAndRegisterModules(); |
| 63 | +``` |
| 64 | + |
| 65 | +### Serialization |
| 66 | + |
| 67 | +For serialization this module currently supports |
| 68 | +[ |
| 69 | +`javax.money.MonetaryAmount`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmount.java) |
| 70 | +and will, by default, serialize it as: |
| 71 | + |
| 72 | +```json |
| 73 | +{ |
| 74 | + "amount": 99.95, |
| 75 | + "currency": "EUR" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +To serialize number as a JSON string, you have to configure the quoted decimal number value serializer: |
| 80 | + |
| 81 | +```java |
| 82 | +ObjectMapper mapper = new ObjectMapper() |
| 83 | + .registerModule(new MoneyModule().withQuotedDecimalNumbers()); |
| 84 | +``` |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "amount": "99.95", |
| 89 | + "currency": "EUR" |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Formatting |
| 94 | + |
| 95 | +A special feature for serializing monetary amounts is *formatting*, which is **disabled by default**. To enable it, you |
| 96 | +have to either enable default formatting: |
| 97 | + |
| 98 | +```java |
| 99 | +ObjectMapper mapper = new ObjectMapper() |
| 100 | + .registerModule(new MoneyModule().withDefaultFormatting()); |
| 101 | +``` |
| 102 | + |
| 103 | +... or pass in a `MonetaryAmountFormatFactory` implementation to the `MoneyModule`: |
| 104 | + |
| 105 | +```java |
| 106 | +ObjectMapper mapper = new ObjectMapper() |
| 107 | + .registerModule(new MoneyModule() |
| 108 | + .withFormatting(new CustomMonetaryAmountFormatFactory())); |
| 109 | +``` |
| 110 | + |
| 111 | +The default formatting delegates directly to `MonetaryFormats.getAmountFormat(Locale, String...)`. |
| 112 | + |
| 113 | +Formatting only affects the serialization and can be customized based on the *current* locale, as defined by the |
| 114 | +[ |
| 115 | +`SerializationConfig`](https://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/SerializationConfig.html#with\(java.util.Locale\)). |
| 116 | +This allows to implement RESTful API endpoints |
| 117 | +that format monetary amounts based on the `Accept-Language` header. |
| 118 | + |
| 119 | +The first example serializes a monetary amount using the `de_DE` locale: |
| 120 | + |
| 121 | +```java |
| 122 | +ObjectWriter writer = mapper.writer().with(Locale.GERMANY); |
| 123 | +writer. |
| 124 | + |
| 125 | +writeValueAsString(Money.of(29.95, "EUR")); |
| 126 | +``` |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "amount": 29.95, |
| 131 | + "currency": "EUR", |
| 132 | + "formatted": "29,95 EUR" |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +The following example uses `en_US`: |
| 137 | + |
| 138 | +```java |
| 139 | +ObjectWriter writer = mapper.writer().with(Locale.US); |
| 140 | +writer. |
| 141 | + |
| 142 | +writeValueAsString(Money.of(29.95, "USD")); |
| 143 | +``` |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "amount": 29.95, |
| 148 | + "currency": "USD", |
| 149 | + "formatted": "USD29.95" |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +More sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly. |
| 154 | + |
| 155 | +### Deserialization |
| 156 | + |
| 157 | +This module will use `org.javamoney.moneta.Money` as an implementation for `javax.money.MonetaryAmount` by default when |
| 158 | +deserializing money values. If you need a different implementation, you can pass a different `MonetaryAmountFactory` |
| 159 | +to the `MoneyModule`: |
| 160 | + |
| 161 | +```java |
| 162 | +ObjectMapper mapper = new ObjectMapper() |
| 163 | + .registerModule(new MoneyModule() |
| 164 | + .withMonetaryAmount(new CustomMonetaryAmountFactory())); |
| 165 | +``` |
| 166 | + |
| 167 | +You can also pass in a method reference: |
| 168 | + |
| 169 | +```java |
| 170 | +ObjectMapper mapper = new ObjectMapper() |
| 171 | + .registerModule(new MoneyModule() |
| 172 | + .withMonetaryAmount(FastMoney::of)); |
| 173 | +``` |
| 174 | + |
| 175 | +*Jackson Datatype Money* comes with support for all `MonetaryAmount` implementations from Moneta, the reference |
| 176 | +implementation of JavaMoney: |
| 177 | + |
| 178 | +| `MonetaryAmount` Implementation | Factory | |
| 179 | +|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------| |
| 180 | +| `org.javamoney.moneta.FastMoney` | [`new MoneyModule().withFastMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/FastMoneyFactory.java) | |
| 181 | +| `org.javamoney.moneta.Money` | [`new MoneyModule().withMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/MoneyFactory.java) | |
| 182 | +| `org.javamoney.moneta.RoundedMoney` | [`new MoneyModule().withRoundedMoney()`](src/main/java/com/fasterxml/jackson/datatype/money/RoundedMoneyFactory.java) | | |
| 183 | + |
| 184 | +Module supports deserialization of amount number from JSON number as well as from JSON string without any special |
| 185 | +configuration required. |
| 186 | + |
| 187 | +### Custom Field Names |
| 188 | + |
| 189 | +As you have seen in the previous examples the `MoneyModule` uses the field names `amount`, `currency` and `formatted` |
| 190 | +by default. Those names can be overridden if desired: |
| 191 | + |
| 192 | +```java |
| 193 | +ObjectMapper mapper = new ObjectMapper() |
| 194 | + .registerModule(new MoneyModule() |
| 195 | + .withAmountFieldName("value") |
| 196 | + .withCurrencyFieldName("unit") |
| 197 | + .withFormattedFieldName("pretty")); |
| 198 | +``` |
| 199 | + |
| 200 | +## Usage |
| 201 | + |
| 202 | +After registering and configuring the module you're now free to directly use `MonetaryAmount` in your data types: |
| 203 | + |
| 204 | +```java |
| 205 | +import javax.money.MonetaryAmount; |
| 206 | + |
| 207 | +public class Product { |
| 208 | + private String sku; |
| 209 | + private MonetaryAmount price; |
| 210 | + ... |
| 211 | +} |
| 212 | +``` |
0 commit comments