java 40 lines · 8 steps

Serializing Money to JSON in Spring

A custom Jackson serializer renders JavaMoney amounts as a clean, currency-aware JSON object and wires itself into Spring.

Explained by highlit
1import com.fasterxml.jackson.core.JsonGenerator;
2import com.fasterxml.jackson.databind.JsonSerializer;
3import com.fasterxml.jackson.databind.SerializerProvider;
4import com.fasterxml.jackson.databind.module.SimpleModule;
5import org.javamoney.moneta.Money;
6import org.springframework.context.annotation.Bean;
7import org.springframework.context.annotation.Configuration;
8 
9import java.io.IOException;
10import java.math.RoundingMode;
11 
12public class MoneySerializer extends JsonSerializer<Money> {
13 
14 @Override
15 public void serialize(Money value, JsonGenerator gen, SerializerProvider provider) throws IOException {
16 Money rounded = value.with(java.util.Currency.getInstance(value.getCurrency().getCurrencyCode())
17 .getDefaultFractionDigits() >= 0
18 ? org.javamoney.moneta.function.MonetaryOperators.rounding(RoundingMode.HALF_UP)
19 : org.javamoney.moneta.function.MonetaryOperators.rounding());
20 
21 gen.writeStartObject();
22 gen.writeStringField("currency", rounded.getCurrency().getCurrencyCode());
23 gen.writeStringField("amount", rounded.getNumberStripped().toPlainString());
24 gen.writeNumberField("minorUnits", rounded.getNumberStripped()
25 .movePointRight(rounded.getCurrency().getDefaultFractionDigits())
26 .longValueExact());
27 gen.writeEndObject();
28 }
29 
30 @Configuration
31 public static class MoneyJacksonConfig {
32 
33 @Bean
34 public SimpleModule moneyModule() {
35 SimpleModule module = new SimpleModule("MoneyModule");
36 module.addSerializer(Money.class, new MoneySerializer());
37 return module;
38 }
39 }
40}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Custom JsonSerializers give you full control over how domain types appear in JSON.
  2. 2Rounding money to its currency's fraction digits before output avoids leaking floating-point noise.
  3. 3Registering a SimpleModule as a Spring bean makes Jackson pick up custom serializers automatically.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Serializing Money to JSON in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code