Skip to content

Commit

Permalink
GH-43 Updates README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderYastrebov committed May 8, 2017
1 parent 76d2af4 commit 1e4db2e
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ JSON while reflecting our API preferences, as shown in the following example. We

```json
{
"amount": 29.95,
"amount": 29.95,
"currency": "EUR"
}
```
Expand Down Expand Up @@ -75,8 +75,26 @@ For serialization this module currently supports the following data types:
|-----------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|----------------------------------------|
| [`java.util.Currency`](https://docs.oracle.com/javase/8/docs/api/java/util/Currency.html) | [ISO-4217](http://en.wikipedia.org/wiki/ISO_4217) | `EUR` |
| [`javax.money.CurrencyUnit`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/CurrencyUnit.java) | [ISO-4217](http://en.wikipedia.org/wiki/ISO_4217) | `EUR` |
| [`javax.money.NumberValue`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/NumberValue.java) | | `99.95` |
| [`javax.money.MonetaryAmount`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmount.java) | | `{"amount": 99.95, "currency": "EUR"}` |


By default amount's number value is serialized as a JSON number.
To serialize number as a JSON string, you have to configure string number value serializer:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
.withNumberValueSerializer(new StringNumberValueSerializer()));
```

```json
{
"amount": "99.95",
"currency": "EUR"
}
```

### Formatting

A special feature for serializing monetary amounts is *formatting*, which is **disabled by default**. To enable it, you
Expand All @@ -103,7 +121,7 @@ writer.writeValueAsString(Money.of(29.95, "EUR"));

```json
{
"amount": 29.95,
"amount": 29.95,
"currency": "EUR",
"formatted": "29,95 EUR"
}
Expand All @@ -118,13 +136,13 @@ writer.writeValueAsString(Money.of(29.95, "USD"));

```json
{
"amount": 29.95,
"amount": 29.95,
"currency": "USD",
"formatted": "USD29.95"
}
```

More sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly.
More sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly.

### Deserialization

Expand All @@ -146,26 +164,28 @@ ObjectMapper mapper = new ObjectMapper()
```

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

| `MonetaryAmount` Implementation | Factory |
|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `org.javamoney.moneta.FastMoney` | [`org.zalando.jackson.datatype.money.FastMoneyFactory`](src/main/java/org/zalando/jackson/datatype/money/FastMoneyFactory.java) |
| `org.javamoney.moneta.Money` | [`org.zalando.jackson.datatype.money.MoneyFactory`](src/main/java/org/zalando/jackson/datatype/money/MoneyFactory.java) |
| `org.javamoney.moneta.RoundedMoney` | [`org.zalando.jackson.datatype.money.RoundedMoneyFactory`](src/main/java/org/zalando/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`
by default. Those names can be overridden if desired:

```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MoneyModule()
.withFieldNames(FieldNames.valueOf("value", "unit", "pretty")));
```

Overriding only one of them can be achieved by using:
Overriding only one of them can be achieved by using:

```java
FieldNames.defaults().withCurrency("unit")
Expand All @@ -191,7 +211,7 @@ If you have questions, concerns, bug reports, etc, please file an issue in this

## Getting involved

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.
Please note that we aim to keep this project straightforward and focused. We are not looking to add lots of features;
we just want it to keep doing what it does, as well and as powerfully as possible. For more details check the
To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.
Please note that we aim to keep this project straightforward and focused. We are not looking to add lots of features;
we just want it to keep doing what it does, as well and as powerfully as possible. For more details check the
[contribution guidelines](CONTRIBUTING.md).

0 comments on commit 1e4db2e

Please sign in to comment.