{"id":13485889,"url":"https://github.com/zalando/jackson-datatype-money","last_synced_at":"2025-03-27T19:31:51.402Z","repository":{"id":33249011,"uuid":"36893398","full_name":"zalando/jackson-datatype-money","owner":"zalando","description":"Extension module to properly support datatypes of javax.money","archived":false,"fork":false,"pushed_at":"2024-08-10T18:15:56.000Z","size":670,"stargazers_count":252,"open_issues_count":11,"forks_count":40,"subscribers_count":26,"default_branch":"main","last_synced_at":"2024-10-30T20:45:52.935Z","etag":null,"topics":["currency","jackson","java","json","microservices","monetary","money"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zalando.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-06-04T20:21:24.000Z","updated_at":"2024-10-16T21:55:16.000Z","dependencies_parsed_at":"2024-01-03T01:20:57.043Z","dependency_job_id":"4ba85c91-d47e-4c6e-87f5-8cbff8a42d99","html_url":"https://github.com/zalando/jackson-datatype-money","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zalando%2Fjackson-datatype-money","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zalando%2Fjackson-datatype-money/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zalando%2Fjackson-datatype-money/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zalando%2Fjackson-datatype-money/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zalando","download_url":"https://codeload.github.com/zalando/jackson-datatype-money/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245910935,"owners_count":20692519,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["currency","jackson","java","json","microservices","monetary","money"],"created_at":"2024-07-31T18:00:33.490Z","updated_at":"2025-03-27T19:31:50.641Z","avatar_url":"https://github.com/zalando.png","language":"Java","funding_links":[],"categories":["项目","Projects","Solutions","JSON"],"sub_categories":["JSON"],"readme":"# Jackson Datatype Money\n\n[![Stability: Sustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html)\n![Build Status](https://github.com/zalando/jackson-datatype-money/workflows/build/badge.svg)\n[![Coverage Status](https://img.shields.io/coveralls/zalando/jackson-datatype-money/main.svg)](https://coveralls.io/r/zalando/jackson-datatype-money)\n[![Code Quality](https://img.shields.io/codacy/grade/7fdac4ae509b403eb837b246e288856f/main.svg)](https://www.codacy.com/app/whiskeysierra/jackson-datatype-money)\n[![Javadoc](http://javadoc.io/badge/org.zalando/jackson-datatype-money.svg)](http://www.javadoc.io/doc/org.zalando/jackson-datatype-money)\n[![Release](https://img.shields.io/github/release/zalando/jackson-datatype-money.svg)](https://github.com/zalando/jackson-datatype-money/releases)\n[![Maven Central](https://img.shields.io/maven-central/v/org.zalando/jackson-datatype-money.svg)](https://maven-badges.herokuapp.com/maven-central/org.zalando/jackson-datatype-money)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/zalando/jackson-datatype-money/main/LICENSE)\n\n\n*Jackson Datatype Money* is a [Jackson](https://github.com/codehaus/jackson) module to support JSON serialization and\ndeserialization of [JavaMoney](https://github.com/JavaMoney/jsr354-api) data types. It fills a niche, in that it\nintegrates JavaMoney and Jackson so that they work seamlessly together, without requiring additional\ndeveloper effort. In doing so, it aims to perform a small but repetitive task — once and for all.\n\nThis library reflects our API preferences for [representing monetary amounts in JSON](MONEY.md):\n\n```json\n{\n  \"amount\": 29.95,\n  \"currency\": \"EUR\"\n}\n```\n\n## Features\n- enables you to express monetary amounts in JSON\n- can be used in a REST APIs\n- customized field names\n- localization of formatted monetary amounts\n- allows you to implement RESTful API endpoints that format monetary amounts based on the Accept-Language header\n- is unique and flexible\n\n## Dependencies\n- Java 8 or higher\n- Any build tool using Maven Central, or direct download\n- Jackson\n- JavaMoney\n\n## Installation\n\nAdd the following dependency to your project:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.zalando\u003c/groupId\u003e\n    \u003cartifactId\u003ejackson-datatype-money\u003c/artifactId\u003e\n    \u003cversion\u003e${jackson-datatype-money.version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\nFor 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.\n\n## Configuration\n\nRegister the module with your `ObjectMapper`:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule());\n```\n\nAlternatively, you can use the SPI capabilities:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .findAndRegisterModules();\n```\n\n### Serialization\n\nFor serialization this module currently supports\n[`javax.money.MonetaryAmount`](https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmount.java)\nand will, by default, serialize it as:\n\n```json\n{\n  \"amount\": 99.95,\n  \"currency\": \"EUR\"\n}\n```\n\nTo serialize number as a JSON string, you have to configure the quoted decimal number value serializer:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule().withQuotedDecimalNumbers());\n```\n\n```json\n{\n  \"amount\": \"99.95\",\n  \"currency\": \"EUR\"\n}\n```\n\n### Formatting\n\nA special feature for serializing monetary amounts is *formatting*, which is **disabled by default**. To enable it, you\nhave to either enable default formatting:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule().withDefaultFormatting());\n```\n\n... or pass in a `MonetaryAmountFormatFactory` implementation to the `MoneyModule`:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule()\n        .withFormatting(new CustomMonetaryAmountFormatFactory()));\n```\n\nThe default formatting delegates directly to `MonetaryFormats.getAmountFormat(Locale, String...)`.\n\nFormatting only affects the serialization and can be customized based on the *current* locale, as defined by the\n[`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\nthat format monetary amounts based on the `Accept-Language` header.\n\nThe first example serializes a monetary amount using the `de_DE` locale:\n\n```java\nObjectWriter writer = mapper.writer().with(Locale.GERMANY);\nwriter.writeValueAsString(Money.of(29.95, \"EUR\"));\n```\n\n```json\n{\n  \"amount\": 29.95,\n  \"currency\": \"EUR\",\n  \"formatted\": \"29,95 EUR\"\n}\n```\n\nThe following example uses `en_US`:\n\n```java\nObjectWriter writer = mapper.writer().with(Locale.US);\nwriter.writeValueAsString(Money.of(29.95, \"USD\"));\n```\n\n```json\n{\n  \"amount\": 29.95,\n  \"currency\": \"USD\",\n  \"formatted\": \"USD29.95\"\n}\n```\n\nMore sophisticated formatting rules can be supported by implementing `MonetaryAmountFormatFactory` directly.\n\n### Deserialization\n\nThis module will use `org.javamoney.moneta.Money` as an implementation for `javax.money.MonetaryAmount` by default when\ndeserializing money values. If you need a different implementation, you can pass a different `MonetaryAmountFactory`\nto the `MoneyModule`:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule()\n        .withMonetaryAmount(new CustomMonetaryAmountFactory()));\n```\n\nYou can also pass in a method reference:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule()\n        .withMonetaryAmount(FastMoney::of));\n```\n\n*Jackson Datatype Money* comes with support for all `MonetaryAmount` implementations from Moneta, the reference\nimplementation of JavaMoney:\n\n| `MonetaryAmount` Implementation     | Factory                                                                                                                               |\n|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|\n| `org.javamoney.moneta.FastMoney`    | [`new MoneyModule().withFastMoney()`](src/main/java/org/zalando/jackson/datatype/money/FastMoneyFactory.java)       |\n| `org.javamoney.moneta.Money`        | [`new MoneyModule().withMoney()`](src/main/java/org/zalando/jackson/datatype/money/MoneyFactory.java)               |\n| `org.javamoney.moneta.RoundedMoney` | [`new MoneyModule().withRoundedMoney()`](src/main/java/org/zalando/jackson/datatype/money/RoundedMoneyFactory.java) |                                                                                                                             |\n\nModule supports deserialization of amount number from JSON number as well as from JSON string without any special configuration required.\n\n### Custom Field Names\n\nAs you have seen in the previous examples the `MoneyModule` uses the field names `amount`, `currency` and `formatted`\n by default. Those names can be overridden if desired:\n\n```java\nObjectMapper mapper = new ObjectMapper()\n    .registerModule(new MoneyModule()\n        .withAmountFieldName(\"value\")\n        .withCurrencyFieldName(\"unit\")\n        .withFormattedFieldName(\"pretty\"));\n```\n\n## Usage\n\nAfter registering and configuring the module you're now free to directly use `MonetaryAmount` in your data types:\n\n```java\nimport javax.money.MonetaryAmount;\n\npublic class Product {\n    private String sku;\n    private MonetaryAmount price;\n    ...\n}\n```\n\n## Getting help\n\nIf you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.\n\n## Getting involved\n\nTo contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.\nPlease note that we aim to keep this project straightforward and focused. We are not looking to add lots of features;\nwe just want it to keep doing what it does, as well and as powerfully as possible. For more details check the\n[contribution guidelines](.github/CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzalando%2Fjackson-datatype-money","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzalando%2Fjackson-datatype-money","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzalando%2Fjackson-datatype-money/lists"}