-
Notifications
You must be signed in to change notification settings - Fork 23
Move jackson-datatype-money module from zalando #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
abaf36d
Migrating Commit 7fd4c1e from zalando/jackson-datatype-money
sri-adarsh-kumar 8d99b6d
Respond to review comments
sri-adarsh-kumar 487cbf9
Move Money.md in from source repo
sri-adarsh-kumar f347a4c
Use hand-crafted moditect date and change ObjectMapper in test
sri-adarsh-kumar 90bd5ae
Rename MoneyModule, use addModule in ReadMe, update License year
sri-adarsh-kumar 5fa5d6a
Remove strict Moneta dependency, Update docs
sri-adarsh-kumar bc9ab3a
Add WIP Moneta Module
sri-adarsh-kumar 4df053d
Add WIP Moneta Module
sri-adarsh-kumar 01ef594
Merge branch '2.19' into 2.19
cowtowncoder 3d34f47
Add WIP Moneta Module
sri-adarsh-kumar 4a6a7fc
Merge branch '2.19' of https://github.com/sri-adarsh-kumar/jackson-da…
sri-adarsh-kumar cf26624
Review docs
sri-adarsh-kumar a8c7b2e
Update parent README
sri-adarsh-kumar 6948585
Use SPI for MonetaryAmountFactory
sri-adarsh-kumar ed52822
Deserialize MonetaryAmount with Amount Factory
sri-adarsh-kumar 7e75b5f
Migrate Javax Money and Moneta modules to Junit 5
sri-adarsh-kumar 90a4850
Support for Moneta types moved from Javax to Moneta Module
sri-adarsh-kumar 045a019
Remove Amount Factory from MonetaModule
sri-adarsh-kumar 8cded1d
Remove CurrencyUnit deserializing from MonetaModule
sri-adarsh-kumar ac80e2a
Merge branch '2.19' into 2.19
cowtowncoder a1978c2
Fix typo in Moneta Readme
sri-adarsh-kumar 9fb4b7e
Bit of refactoring
cowtowncoder 1455f75
Fixing things wrt refactoring
cowtowncoder cdcc25a
Merge branch '2.19' into 2.19
cowtowncoder 1945ac3
minor reording of README
cowtowncoder b6ca468
Merge branch '2.19' of github.com:sri-adarsh-kumar/jackson-datatypes-…
cowtowncoder a47a085
Merge branch '2.19' into 2.19
cowtowncoder 7b8cfb3
Minor to fix to `module-info.java`s
cowtowncoder 4c2fd28
Minor fixes to READMEs
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| # Jackson Datatype Money | ||
|
|
||
| *Jackson Datatype Moneta* is a [Jackson](https://github.com/codehaus/jackson) module to support JSON serialization and | ||
| deserialization of [JavaMoney](https://github.com/JavaMoney/jsr354-api) data types with special support for JavaMoney's | ||
| Moneta datatypes. | ||
|
|
||
| With this library, it is possible to represent monetary amounts in JSON as follows: | ||
|
|
||
| ```json | ||
| { | ||
| "amount": 29.95, | ||
| "currency": "EUR" | ||
| } | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| - enables you to express monetary amounts in JSON | ||
| - can be used in a REST APIs | ||
| - customized field names | ||
| - localization of formatted monetary amounts | ||
| - allows you to implement RESTful API endpoints that format monetary amounts based on the Accept-Language header | ||
| - is unique and flexible | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - Java 8 or higher | ||
| - Any build tool using Maven Central, or direct download | ||
| - Jackson | ||
| - JavaMoney | ||
|
|
||
| ## Installation | ||
|
|
||
| Add the following dependency to your project: | ||
|
|
||
| ```xml | ||
|
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.datatype</groupId> | ||
| <artifactId>jackson-datatype-moneta</artifactId> | ||
| <version>${jackson-datatype-moneta.version}</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| For ultimate flexibility, this module is compatible with the official version as well as the backport of JavaMoney. The | ||
| actual version will be selected by a profile based on the current JDK version. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Register the module with your `ObjectMapper`: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .addModule(new MonetaMoneyModule()) | ||
| .build(); | ||
| ``` | ||
|
|
||
| Alternatively, you can use the SPI capabilities: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = new ObjectMapper() | ||
| .findAndRegisterModules(); | ||
| ``` | ||
|
|
||
| ### Serialization | ||
|
|
||
| For serialization this module currently supports | ||
| [ | ||
| `javax.money.MonetaryAmount`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmount.java) | ||
| and will, by default, serialize it as: | ||
|
|
||
| ```json | ||
| { | ||
| "amount": 99.95, | ||
| "currency": "EUR" | ||
| } | ||
| ``` | ||
|
|
||
| To serialize number as a JSON string, you have to configure the quoted decimal number value serializer: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .addModule(new MonetaMoneyModule().withQuotedDecimalNumbers()) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ```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 | ||
| have to either enable default formatting: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .addModule(new MonetaMoneyModule().withDefaultFormatting()) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ... or pass in a `MonetaryAmountFormatFactory` implementation to the `MonetaMoneyModule`: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .addModule(new MonetaMoneyModule() | ||
| .withFormatting(new CustomMonetaryAmountFormatFactory())) | ||
| .build(); | ||
| ``` | ||
|
|
||
| The default formatting delegates directly to `MonetaryFormats.getAmountFormat(Locale, String...)`. | ||
|
|
||
| Formatting only affects the serialization and can be customized based on the *current* locale, as defined by the | ||
| [ | ||
| `SerializationConfig`](https://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/SerializationConfig.html#with\(java.util.Locale\)). | ||
| This allows to implement RESTful API endpoints | ||
| that format monetary amounts based on the `Accept-Language` header. | ||
|
|
||
| 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")); | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "amount": 29.95, | ||
| "currency": "EUR", | ||
| "formatted": "29,95 EUR" | ||
| } | ||
| ``` | ||
|
|
||
| The following example uses `en_US`: | ||
|
|
||
| ```java | ||
| ObjectWriter writer = mapper.writer().with(Locale.US); | ||
| writer.writeValueAsString(Money.of(29.95, "USD")); | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "amount": 29.95, | ||
| "currency": "USD", | ||
| "formatted": "USD29.95" | ||
| } | ||
| ``` | ||
|
|
||
| More sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly. | ||
|
|
||
| ### Deserialization | ||
|
|
||
| This module will use `org.javamoney.moneta.Money` as an implementation for `javax.money.MonetaryAmount` by default when | ||
| deserializing money values. | ||
|
|
||
| In addition, this module comes with support for all `MonetaryAmount` implementations from Moneta, the reference | ||
| implementation of JavaMoney: | ||
|
|
||
| | `MonetaryAmount` Implementation | Factory | | ||
| |-------------------------------------|----------------------------------------------| | ||
| | `org.javamoney.moneta.FastMoney` | `new MonetaMoneyModule().withFastMoney()` | | ||
| | `org.javamoney.moneta.Money` | `new MonetaMoneyModule().withMoney()` | | ||
| | `org.javamoney.moneta.RoundedMoney` | `new MonetaMoneyModule().withRoundedMoney()` | | | ||
|
|
||
| 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 `MonetaMoneyModule` uses the field names `amount`, `currency` and | ||
| `formatted` | ||
| by default. Those names can be overridden if desired: | ||
|
|
||
| ```java | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .addModule(new MonetaMoneyModule() | ||
| .withAmountFieldName("value") | ||
| .withCurrencyFieldName("unit") | ||
| .withFormattedFieldName("pretty")) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| After registering and configuring the module you're now free to directly use `MonetaryAmount` in your data types: | ||
|
|
||
| ```java | ||
| import javax.money.MonetaryAmount; | ||
|
|
||
| public class Product { | ||
| private String sku; | ||
| private MonetaryAmount price; | ||
| ... | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <!-- This module was also published with a richer model, Gradle metadata, --> | ||
| <!-- which should be used instead. Do not delete the following line which --> | ||
| <!-- is to indicate to Gradle or any Gradle module metadata file consumer --> | ||
| <!-- that they should prefer consuming it instead. --> | ||
| <!-- do_not_remove: published-with-gradle-metadata --> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.fasterxml.jackson.datatype</groupId> | ||
| <artifactId>jackson-datatypes-misc-parent</artifactId> | ||
| <version>2.19.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>jackson-datatype-moneta</artifactId> | ||
| <name>Jackson datatype: javax-money-moneta</name> | ||
| <packaging>jar</packaging> | ||
| <version>2.19.0-SNAPSHOT</version> | ||
| <description>Support for datatypes of Money API spec from JSR 354 (https://javamoney.github.io/api.html) | ||
| </description> | ||
| <url>https://github.com/FasterXML/jackson-datatypes-misc</url> | ||
| <licenses> | ||
| <license> | ||
| <name>MIT License</name> | ||
| <url>https://opensource.org/licenses/MIT</url> | ||
| <distribution>repo</distribution> | ||
| </license> | ||
| </licenses> | ||
| <properties> | ||
| <!-- Generate PackageVersion.java into this directory. --> | ||
| <packageVersion.dir>com/fasterxml/jackson/datatype/money/moneta</packageVersion.dir> | ||
| <packageVersion.package>${project.groupId}.money.moneta</packageVersion.package> | ||
| <slf4j.version>2.0.6</slf4j.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.datatype</groupId> | ||
| <artifactId>jackson-datatype-money</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>javax.money</groupId> | ||
| <artifactId>money-api</artifactId> | ||
| <version>1.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.javamoney</groupId> | ||
| <artifactId>moneta</artifactId> | ||
| <version>1.4.4</version> | ||
| <type>pom</type> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apiguardian</groupId> | ||
| <artifactId>apiguardian-api</artifactId> | ||
| <version>1.1.2</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.code.findbugs</groupId> | ||
| <artifactId>jsr305</artifactId> | ||
| <version>3.0.2</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>1.18.34</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <!-- test --> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-nop</artifactId> | ||
| <version>${slf4j.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>4.5.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.kjetland</groupId> | ||
| <artifactId>mbknor-jackson-jsonschema_2.12</artifactId> | ||
| <version>1.0.39</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>javax.validation</groupId> | ||
| <artifactId>validation-api</artifactId> | ||
| <version>2.0.1.Final</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.google.code.maven-replacer-plugin</groupId> | ||
| <artifactId>replacer</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.moditect</groupId> | ||
| <artifactId>moditect-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be "Jackson Datatype Moneta"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I have fixed it.