Skip to content

Commit 0924e18

Browse files
committed
Merge branch '2.19'
2 parents cde138b + ee65f21 commit 0924e18

File tree

54 files changed

+3372
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3372
-16
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
env:
2525
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
2626
steps:
27-
- uses: actions/checkout@v3
27+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2828
- name: Set up JDK
29-
uses: actions/setup-java@v3
29+
uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
3030
with:
3131
distribution: 'temurin'
3232
java-version: ${{ matrix.java_version }}
@@ -54,7 +54,7 @@ jobs:
5454
run: ./mvnw -B -q -ff -ntp test
5555
- name: Publish code coverage
5656
if: ${{ github.event_name != 'pull_request' && matrix.java_version == '17' }}
57-
uses: codecov/codecov-action@v1
57+
uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v5.4.0
5858
with:
5959
token: ${{ secrets.CODECOV_TOKEN }}
6060
file: ./target/site/jacoco/jacoco.xml

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ datatype modules to support 3rd party libraries.
55

66
Currently included are:
77

8+
* [jackson-datatype-jakarta-mail](jakarta-mail/) for Jakarta Mail (ex-Java Mail) (starting with Jackson 2.13)
9+
* Currently (2.13) just type `jakarta.mail.internet.InternetAddress`
10+
* [jackson-datatype-javax-money](javax-money/) for [JSR 354](https://github.com/JavaMoney/jsr354-api) datatypes (starting with Jackson 2.19)
11+
* [jackson-datatype-moneta](moneta/) for [JavaMoney Moneta RI](https://javamoney.github.io/) datatypes (jsr354 reference implementation) (starting with Jackson 2.19)
812
* [jackson-datatype-joda-money](joda-money/) for [Joda-Money](https://www.joda.org/joda-money/) datatypes
913
* JSR-353/JSON-P: 2 variants (starting with Jackson 2.12.2)
1014
* [jackson-datatype-jsr353](jsr-353/) for older "javax.json" [JSR-353](https://www.jcp.org/en/jsr/detail?id=353) (aka JSON-P) datatypes (package `javax.json`)
1115
* [jackson-datatype-jakarta-jsonp](jakarta-jsonp/) for newer "Jakarta" JSON-P datatypes (package `jakarta.json`)
1216
* [jackson-datatype-json-org](json-org/) for ([org.json](http://json.org/java)) JSON model datatypes (included in Android SDK, as well as stand-alone Java library)
13-
* [jackson-datatype-jakarta-mail](jakarta-mail/) for Jakarta Mail (ex-Java Mail) (starting with Jackson 2.13)
14-
* Currently (2.13) just type `jakarta.mail.internet.InternetAddress`
1517

1618
Note that this repo was created for Jackson 2.11: prior to this, individual datatype
1719
modules had their own repositories.
@@ -20,9 +22,13 @@ modules had their own repositories.
2022

2123
All modules are licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt).
2224

23-
## Build Status
25+
## Status
26+
27+
| Type | Status |
28+
| ---- | ------ |
29+
| Build (CI) | [![Build (github)](https://github.com/FasterXML/jackson-datatypes-misc/actions/workflows/main.yml/badge.svg)](https://github.com/FasterXML/jackson-datatypes-misc/actions/workflows/main.yml) |
30+
| Code coverage (2.18) | [![codecov.io](https://codecov.io/github/FasterXML/jackson-datatypes-misc/coverage.svg?branch=2.18)](https://codecov.io/github/FasterXML/jackson-datatypes-misc?branch=2.18) |
2431

25-
[![Build Status](https://travis-ci.org/FasterXML/jackson-datatypes-misc.svg)](https://travis-ci.org/FasterXML/jackson-datatypes-misc)
2632

2733
## Usage
2834

@@ -35,7 +41,7 @@ To use module (version 2.x) on Maven-based projects, use dependency like
3541
<dependency>
3642
<groupId>com.fasterxml.jackson.datatype</groupId>
3743
<artifactId>jackson-datatype-json-org</artifactId>
38-
<version>2.13.0</version>
44+
<version>2.18.3</version>
3945
</dependency>
4046
```
4147

@@ -63,9 +69,12 @@ ObjectMapper mapper = JsonMapper.builder()
6369
.addModule(new JsonOrgModule())
6470
.addModule(new JodaMoneyModule())
6571
// ONE of these (not both):
72+
.addModule(new JavaxMoneyModule())
73+
.addModule(new MonetaMoneyModule())
74+
// ONE of these (not both):
6675
.addModule(new JSR353Module()) // old (javax) json-p API
6776
.addModule(new JSONPModule()) // new (jakarta) json-P API
6877
.build();
6978
```
7079

71-
after which datatype read/write support is available for all normal Jackson operations,
80+
after which datatype read/write support is available for all normal Jackson operations.

javax-money/MONEY.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Representing Money in JSON
2+
3+
> A large proportion of the computers in this world manipulate money, so it's always puzzled me that money isn't actually a first class data type in any mainstream programming language.
4+
>
5+
> [Martin Fowler](https://martinfowler.com/eaaCatalog/money.html)
6+
7+
Unfortunately JSON is no different. This document tries to change that by proposing and comparing different styles to represent money, some inspired by external sources and some based on our own experience.
8+
9+
## ⚠️ Monetary amounts ≠ floats
10+
11+
Before we dive into details, always keep the following in mind. However you desire to format money in JSON, nothing changes the fact that you should...
12+
13+
> **Never hold monetary values [..] in a float variable.** Floating point is not suitable for this work, and you must use either [fixed-point](#fixed-point) or [decimal](#decimal) values.
14+
>
15+
> [Coinkite: Common Terms and Data Objects](https://web.archive.org/web/20150924073850/https://docs.coinkite.com/api/common.html)
16+
17+
## Styles
18+
19+
We identified the following styles that all of different advantages and disadvantages that are discussed in their respective section.
20+
21+
| Style | Expressive | Arithmetic | Pitfalls / Misuses |
22+
|------------------------------------|------------|------------|--------------------|
23+
| [Decimal](#decimal) ||| Precision |
24+
| [Quoted Decimal](#quoted-decimal) ||| Parsing |
25+
| [Fixed Point](#fixed-point) ||| Mixed scales |
26+
| [Mixed](#mixed) ||| Consistency |
27+
28+
### Decimal
29+
30+
The most straightforward way to represent a monetary amount would be a base-10 decimal number:
31+
32+
```json
33+
{
34+
"amount": 49.95,
35+
"currency": "EUR"
36+
}
37+
```
38+
39+
It's expressive, readable and allows arithmetic operations. The downside is that most [JSON decoders will treat it as a floating point](https://tools.ietf.org/html/rfc7159#section-6) number which is very much undesirable.
40+
41+
Most programming languages have support for arbitrary-precision [decimals](#decimal-implementations) and JSON decoders that can be configured to use them. In general it can be considered to be a problem of the implementation, not the format itself.
42+
43+
### Quoted Decimal
44+
45+
Same as [Decimal](#decimal) but quoted so your JSON decoder treats it as a string:
46+
47+
```json
48+
{
49+
"amount": "49.95",
50+
"currency": "EUR"
51+
}
52+
```
53+
54+
It solves the precision problem of decimals on the expense of performing arithmetic operations on it. It also requires a two-phase parsing, i.e. parsing the JSON text into a data structure and then parsing quoted amounts into decimals.
55+
56+
### Fixed Point
57+
58+
> A value of a fixed-point data type is essentially an integer that is scaled by an implicit specific factor determined by the type.
59+
>
60+
> [Wikipedia: Fixed-point arithmetic](https://en.wikipedia.org/wiki/Fixed-point_arithmetic)
61+
62+
```json
63+
{
64+
"amount": 4995,
65+
"currency": "EUR"
66+
}
67+
```
68+
69+
The implicit scaling factor is defined as (0.1 raised to the power of) the currency's [default number of fraction digits](http://www.localeplanet.com/icu/currency.html).
70+
71+
In rare cases one might need a higher precision, e.g. to have sub-cent. In this case the scale can be defined explicitly:
72+
73+
```json
74+
{
75+
"amount": 499599,
76+
"currency": "EUR",
77+
"scale": 4
78+
}
79+
```
80+
81+
The downside with fixed-point amounts is that reading them is a bit harder and arithmetic with mixed scale amounts can be tricky and error-prone.
82+
83+
### Mixed
84+
85+
As a way to counter all negative aspects of the styles above one idea would be to have a single object that contains all of the formats:
86+
87+
```json
88+
{
89+
"decimal": 49.95,
90+
"quoted_decimal": "49.95",
91+
"fixed_point": 4995,
92+
"scale": 2,
93+
"currency": "EUR"
94+
}
95+
```
96+
97+
Decoders can choose the representation that fits them the best. Encoders on the other hand have the harder task by providing all of them and making sure that all values are in fact consistent.
98+
99+
## Decimal Implementations
100+
101+
| Language | Implementation |
102+
|------------|---------------------------------------------------------------------------------------------|
103+
| C# | [decimal](https://msdn.microsoft.com/en-us/library/364x0z75.aspx) |
104+
| Java | [java.math.BigDecimal](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html) |
105+
| JavaScript | [decimal.js](https://github.com/MikeMcl/decimal.js/) |
106+
| Python | [decimal.Decimal](https://docs.python.org/2/library/decimal.html) |
107+
108+
## Credits and References
109+
110+
- [Coinkite: Currency Amounts](https://web.archive.org/web/20150924073850/https://docs.coinkite.com/api/common.html#currency-amounts)
111+
- [Culttt: How to handle money and currency in web applications](http://culttt.com/2014/05/28/handle-money-currency-web-applications/)
112+
- [Currency codes - ISO 4217](https://www.iso.org/iso-4217-currency-codes.html)
113+
- [LocalePlanet: ICU Currencies](http://www.localeplanet.com/icu/currency.html)
114+
- [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159#section-6)
115+
- [Stackoverflow: What is the standard for formatting currency values in JSON?](http://stackoverflow.com/questions/30249406/what-is-the-standard-for-formatting-currency-values-in-json)
116+
- [Stackoverflow: Why not use Double or Float to represent currency?](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency/3730040#3730040)
117+
- [TechEmpower: Mangling JSON numbers](https://www.techempower.com/blog/2016/07/05/mangling-json-numbers/)
118+
- [Wikipedia: Fixed-point arithmetic](https://en.wikipedia.org/wiki/Fixed-point_arithmetic)

0 commit comments

Comments
 (0)