|
| 1 | +# Jackson Datatype Javax-Money |
| 2 | + |
| 3 | +*Jackson Datatype Javax-Money* is a [Jackson](https://github.com/FasterXML/jackson) module to support JSON serialization and deserialization of [JSR 354](https://github.com/JavaMoney/jsr354-api) data types. |
| 4 | +It fills a niche, in that it integrates [MonetaryAmount](https://javamoney.github.io/apidocs/javax/money/MonetaryAmount.html) and Jackson so that they work seamlessly together, without requiring additional |
| 5 | +developer effort. In doing so, it aims to perform a small but repetitive task — once and for all. |
| 6 | + |
| 7 | +This library reflects an opinionated API [representation of monetary amounts in JSON](MONEY.md) |
| 8 | + |
| 9 | +With this library, it is possible to represent monetary amounts in JSON as follows: |
| 10 | + |
| 11 | +```json |
| 12 | +{ |
| 13 | + "amount": 29.95, |
| 14 | + "currency": "EUR" |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- enables you to express monetary amounts in JSON |
| 21 | +- can be used in a REST APIs |
| 22 | +- customized field names |
| 23 | +- localization of formatted monetary amounts |
| 24 | +- allows you to implement RESTful API endpoints that format monetary amounts based on the Accept-Language header |
| 25 | +- is unique and flexible |
| 26 | + |
| 27 | +## Dependencies |
| 28 | + |
| 29 | +- Java 8 or higher |
| 30 | +- Any build tool using Maven Central, or direct download |
| 31 | +- Jackson |
| 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-javax-money</artifactId> |
| 42 | + <version>${jackson-datatype-money.version}</version> |
| 43 | +</dependency> |
| 44 | +``` |
| 45 | + |
| 46 | +For ultimate flexibility, this module is compatible with any implementation of JSR 354 MonetaryAmount |
| 47 | + |
| 48 | +## Configuration |
| 49 | + |
| 50 | +Register the module with your `ObjectMapper`: |
| 51 | + |
| 52 | +```java |
| 53 | +ObjectMapper mapper = JsonMapper.builder() |
| 54 | + .addModule(new JavaxMoneyModule()) |
| 55 | + .build(); |
| 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 = JsonMapper.builder() |
| 83 | + .addModule(new JavaxMoneyModule().withQuotedDecimalNumbers()) |
| 84 | + .build(); |
| 85 | +``` |
| 86 | + |
| 87 | +```json |
| 88 | +{ |
| 89 | + "amount": "99.95", |
| 90 | + "currency": "EUR" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Formatting |
| 95 | + |
| 96 | +A special feature for serializing monetary amounts is *formatting*, which is **disabled by default**. To enable it, you |
| 97 | +have to either enable default formatting: |
| 98 | + |
| 99 | +```java |
| 100 | +ObjectMapper mapper = JsonMapper.builder() |
| 101 | + .addModule(new JavaxMoneyModule().withDefaultFormatting()) |
| 102 | + .build(); |
| 103 | +``` |
| 104 | + |
| 105 | +... or pass in a `MonetaryAmountFormatFactory` implementation to the `JavaxMoneyModule`: |
| 106 | + |
| 107 | +```java |
| 108 | +ObjectMapper mapper = JsonMapper.builder() |
| 109 | + .addModule(new JavaxMoneyModule() |
| 110 | + .withFormatting(new CustomMonetaryAmountFormatFactory())) |
| 111 | + .build(); |
| 112 | +``` |
| 113 | + |
| 114 | +The default formatting delegates directly to `MonetaryFormats.getAmountFormat(Locale, String...)`. |
| 115 | + |
| 116 | +Formatting only affects the serialization and can be customized based on the *current* locale, as defined by the |
| 117 | +[ |
| 118 | +`SerializationConfig`](https://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/SerializationConfig.html#with\(java.util.Locale\)). |
| 119 | +This allows to implement RESTful API endpoints |
| 120 | +that format monetary amounts based on the `Accept-Language` header. |
| 121 | + |
| 122 | +The first example serializes a monetary amount using the `de_DE` locale: |
| 123 | + |
| 124 | +```java |
| 125 | +ObjectWriter writer = mapper.writer().with(Locale.GERMANY); |
| 126 | +writer.writeValueAsString(Money.of(29.95, "EUR")); |
| 127 | +``` |
| 128 | + |
| 129 | +```json |
| 130 | +{ |
| 131 | + "amount": 29.95, |
| 132 | + "currency": "EUR", |
| 133 | + "formatted": "29,95 EUR" |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +The following example uses `en_US`: |
| 138 | + |
| 139 | +```java |
| 140 | +ObjectWriter writer = mapper.writer().with(Locale.US); |
| 141 | +writer.writeValueAsString(Money.of(29.95, "USD")); |
| 142 | +``` |
| 143 | + |
| 144 | +```json |
| 145 | +{ |
| 146 | + "amount": 29.95, |
| 147 | + "currency": "USD", |
| 148 | + "formatted": "USD29.95" |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +More sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly. |
| 153 | + |
| 154 | +### Deserialization |
| 155 | + |
| 156 | +This module will not have a default deserialization feature. |
| 157 | +At the same time, if the [Moneta](https://javamoney.github.io/ri.html) library is found in the class path, the module will use `org.javamoney.moneta.Money` as an implementation for `javax.money.MonetaryAmount` by default when deserializing monetary amounts. |
| 158 | + |
| 159 | +Alternatively, in order to deserialize money values, one has to configure the module to use a specific implementation of `javax.money.MonetaryAmount`. |
| 160 | +This can be done by passing the required `MonetaryAmountFactory` to the `JavaxMoneyModule`: |
| 161 | + |
| 162 | +```java |
| 163 | +ObjectMapper mapper = JsonMapper.builder() |
| 164 | + .addModule(new JavaxMoneyModule() |
| 165 | + .withMonetaryAmountFactory(new CustomMonetaryAmountFactory())) |
| 166 | + .build(); |
| 167 | +``` |
| 168 | + |
| 169 | +You can also pass in a method reference: |
| 170 | + |
| 171 | +```java |
| 172 | +ObjectMapper mapper = JsonMapper.builder() |
| 173 | + .addModule(new JavaxMoneyModule() |
| 174 | + .withMonetaryAmountFactory(FastMoney::of)) |
| 175 | + .build(); |
| 176 | +``` |
| 177 | + |
| 178 | +Please note that, for Moneta implementations like Money, FastMoney and RoundedMoney, the sibling module `jackson-datatype-moneta` can also be used. |
| 179 | +Refer to [javax-money-moneta](../javax-money-moneta/README.md) for more information. |
| 180 | + |
| 181 | +### Custom Field Names |
| 182 | + |
| 183 | +As you have seen in the previous examples the `JavaxMoneyModule` uses the field names `amount`, `currency` and `formatted` |
| 184 | +by default. Those names can be overridden if desired: |
| 185 | + |
| 186 | +```java |
| 187 | +ObjectMapper mapper = JsonMapper.builder() |
| 188 | + .addModule(new JavaxMoneyModule() |
| 189 | + .withAmountFieldName("value") |
| 190 | + .withCurrencyFieldName("unit") |
| 191 | + .withFormattedFieldName("pretty")) |
| 192 | + .build(); |
| 193 | +``` |
| 194 | + |
| 195 | +## Usage |
| 196 | + |
| 197 | +After registering and configuring the module you're now free to directly use `MonetaryAmount` in your data types: |
| 198 | + |
| 199 | +```java |
| 200 | +import javax.money.MonetaryAmount; |
| 201 | + |
| 202 | +public class Product { |
| 203 | + private String sku; |
| 204 | + private MonetaryAmount price; |
| 205 | + ... |
| 206 | +} |
| 207 | +``` |
0 commit comments