All Projects → zalando → Jackson Datatype Money

zalando / Jackson Datatype Money

Licence: mit
Extension module to properly support datatypes of javax.money

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Jackson Datatype Money

Countries
Countries - ISO 3166 (ISO3166-1, ISO3166, Digit, Alpha-2 and Alpha-3) countries codes and names (on eng and rus), ISO 4217 currency designators, ITU-T E.164 IDD calling phone codes, countries capitals, UN M.49 regions codes, ccTLD countries domains, IOC/NOC and FIFA letters codes, VERY FAST, NO maps[], NO slices[], NO init() funcs, NO external links/files/data, NO interface{}, NO specific dependencies, Databases/JSON/GOB/XML/CSV compatible, Emoji countries flags and currencies support, full support ISO-3166-1, ISO-4217, ITU-T E.164, Unicode CLDR and ccTLD standarts.
Stars: ✭ 85 (-48.48%)
Mutual labels:  json, currency, money
Javamoney Lib
JavaMoney financial libraries, extending and complementing JSR 354
Stars: ✭ 104 (-36.97%)
Mutual labels:  currency, money
Money Open Exchange Rates
A gem that calculates the exchange rate using published rates from open-exchange-rates. Compatible with the money gem.
Stars: ✭ 87 (-47.27%)
Mutual labels:  currency, money
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (-30.91%)
Mutual labels:  microservices, json
Ngx Currency
📦 Currency mask module for Angular
Stars: ✭ 161 (-2.42%)
Mutual labels:  currency, money
Emfjson Jackson
JSON Binding for Eclipse Modeling Framework
Stars: ✭ 79 (-52.12%)
Mutual labels:  json, jackson
Gwt Jackson
gwt-jackson is a JSON parser for GWT. It uses Jackson 2.x annotations to customize the serialization/deserialization process.
Stars: ✭ 110 (-33.33%)
Mutual labels:  json, jackson
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (-67.27%)
Mutual labels:  json, jackson
Exchanger
🏢 Currency exchange rates framework for PHP
Stars: ✭ 133 (-19.39%)
Mutual labels:  currency, money
Django Prices
Django fields for the prices module
Stars: ✭ 135 (-18.18%)
Mutual labels:  currency, money
Bricks
A standard library for microservices.
Stars: ✭ 142 (-13.94%)
Mutual labels:  microservices, json
Currencyconverter
Utilities for doing currency conversion with the Money library
Stars: ✭ 78 (-52.73%)
Mutual labels:  currency, money
Swap
💱 Currency exchange rates library
Stars: ✭ 1,195 (+624.24%)
Mutual labels:  currency, money
Currency.js
A javascript library for handling currencies
Stars: ✭ 2,214 (+1241.82%)
Mutual labels:  currency, money
Android Money
Simple money and currency converter library for android.
Stars: ✭ 66 (-60%)
Mutual labels:  currency, money
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (-33.33%)
Mutual labels:  json, jackson
Brutusin Rpc
Self-describing JSON-RPC web services over HTTP, with automatic API description based on JSON-Schema
Stars: ✭ 36 (-78.18%)
Mutual labels:  microservices, json
Jsonj
A fluent Java API for manipulating json data structures
Stars: ✭ 42 (-74.55%)
Mutual labels:  json, jackson
Frankfurter
💱 Currency data API
Stars: ✭ 123 (-25.45%)
Mutual labels:  currency, money
Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (-12.12%)
Mutual labels:  currency, money

Jackson Datatype Money

Stability: Sustained Build Status Coverage Status Code Quality Javadoc Release Maven Central License

Jackson Datatype Money is a Jackson module to support JSON serialization and deserialization of JavaMoney data types. It fills a niche, in that it integrates JavaMoney and Jackson so that they work seamlessly together, without requiring additional developer effort. In doing so, it aims to perform a small but repetitive task — once and for all.

This library reflects our API preferences for representing monetary amounts in 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:

<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>jackson-datatype-money</artifactId>
    <version>${jackson-datatype-money.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:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule());

Alternatively, you can use the SPI capabilities:

ObjectMapper mapper = new ObjectMapper()
    .findAndRegisterModules();

Serialization

For serialization this module currently supports javax.money.MonetaryAmount and will, by default, serialize it as:

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

To serialize number as a JSON string, you have to configure the quoted decimal number value serializer:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule().withQuotedDecimalNumbers());
{
  "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:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule().withDefaultFormatting());

... or pass in a MonetaryAmountFormatFactory implementation to the MoneyModule:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule()
        .withFormatting(new CustomMonetaryAmountFormatFactory()));

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. 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:

ObjectWriter writer = mapper.writer().with(Locale.GERMANY);
writer.writeValueAsString(Money.of(29.95, "EUR"));
{
  "amount": 29.95,
  "currency": "EUR",
  "formatted": "29,95 EUR"
}

The following example uses en_US:

ObjectWriter writer = mapper.writer().with(Locale.US);
writer.writeValueAsString(Money.of(29.95, "USD"));
{
  "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. If you need a different implementation, you can pass a different MonetaryAmountFactory to the MoneyModule:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule()
        .withMonetaryAmount(new CustomMonetaryAmountFactory()));

You can also pass in a method reference:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule()
        .withMonetaryAmount(FastMoney::of));

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

MonetaryAmount Implementation Factory
org.javamoney.moneta.FastMoney new MoneyModule().withFastMoney()
org.javamoney.moneta.Money new MoneyModule().withMoney()
org.javamoney.moneta.RoundedMoney new MoneyModule().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 MoneyModule uses the field names amount, currency and formatted by default. Those names can be overridden if desired:

ObjectMapper mapper = new ObjectMapper()
    .registerModule(new MoneyModule()
        .withAmountFieldName("value")
        .withCurrencyFieldName("unit")
        .withFormattedFieldName("pretty"));

Usage

After registering and configuring the module you're now free to directly use MonetaryAmount in your data types:

import javax.money.MonetaryAmount;

public class Product {
    private String sku;
    private MonetaryAmount price;
    ...
}

Getting help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

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 contribution guidelines.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].