Skip to content

Commit

Permalink
GH-43 Adds support for NumberValue serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderYastrebov committed May 8, 2017
1 parent 943a060 commit 76d2af4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import javax.money.MonetaryAmount;
import javax.money.format.MonetaryAmountFormat;
import java.io.IOException;
import java.math.BigDecimal;
import javax.money.NumberValue;
import java.util.Locale;

public final class MonetaryAmountSerializer extends JsonSerializer<MonetaryAmount> {
Expand All @@ -34,13 +34,13 @@ public MonetaryAmountSerializer(final MonetaryAmountFormatFactory factory, final
public void serialize(final MonetaryAmount value, final JsonGenerator generator, final SerializerProvider provider)
throws IOException {

final BigDecimal amount = value.getNumber().numberValueExact(BigDecimal.class);
final NumberValue number = value.getNumber();
final CurrencyUnit currency = value.getCurrency();
@Nullable final String formatted = format(value, provider);

generator.writeStartObject();
{
generator.writeNumberField(names.getAmount(), amount);
generator.writeObjectField(names.getAmount(), number);
generator.writeObjectField(names.getCurrency(), currency);

if (formatted != null) {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/zalando/jackson/datatype/money/MoneyModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.jackson.datatype.money;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
Expand All @@ -11,11 +12,13 @@
import javax.money.CurrencyUnit;
import javax.money.MonetaryAmount;
import java.util.Currency;
import javax.money.NumberValue;

import static com.fasterxml.jackson.core.util.VersionUtil.mavenVersionFor;

public final class MoneyModule extends Module {

private final JsonSerializer<NumberValue> numberValueSerializer;
private final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory;
private final MonetaryAmountFormatFactory formatFactory;
private final FieldNames names;
Expand All @@ -42,6 +45,7 @@ public void setupModule(final SetupContext context) {

serializers.addSerializer(Currency.class, new CurrencySerializer());
serializers.addSerializer(CurrencyUnit.class, new CurrencyUnitSerializer());
serializers.addSerializer(NumberValue.class, numberValueSerializer);
serializers.addSerializer(MonetaryAmount.class, new MonetaryAmountSerializer(formatFactory, names));

context.addSerializers(serializers);
Expand Down Expand Up @@ -86,27 +90,31 @@ public MoneyModule(final MonetaryAmountFormatFactory factory) {
@Deprecated
public MoneyModule(final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
final MonetaryAmountFormatFactory formatFactory) {
this(amountFactory, formatFactory, FieldNames.defaults());
this(new NumberValueSerializer(), amountFactory, formatFactory, FieldNames.defaults());
}

private MoneyModule(final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
private MoneyModule(final JsonSerializer<NumberValue> numberValueSerializer, final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory,
final MonetaryAmountFormatFactory formatFactory, final FieldNames names) {

this.numberValueSerializer = numberValueSerializer;
this.amountFactory = amountFactory;
this.formatFactory = formatFactory;
this.names = names;
}

public MoneyModule withAmountFactory(final MonetaryAmountFactory<? extends MonetaryAmount> amountFactory) {
return new MoneyModule(amountFactory, formatFactory, names);
return new MoneyModule(numberValueSerializer, amountFactory, formatFactory, names);
}

public MoneyModule withFormatFactory(final MonetaryAmountFormatFactory formatFactory) {
return new MoneyModule(amountFactory, formatFactory, names);
return new MoneyModule(numberValueSerializer, amountFactory, formatFactory, names);
}

public MoneyModule withFieldNames(final FieldNames names) {
return new MoneyModule(amountFactory, formatFactory, names);
return new MoneyModule(numberValueSerializer, amountFactory, formatFactory, names);
}

public MoneyModule withNumberValueSerializer(final JsonSerializer<NumberValue> numberValueSerializer) {
return new MoneyModule(numberValueSerializer, amountFactory, formatFactory, names);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.zalando.jackson.datatype.money;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.math.BigDecimal;
import javax.money.NumberValue;

public class NumberValueSerializer extends JsonSerializer<NumberValue> {

@Override
public void serialize(final NumberValue value, final JsonGenerator generator, final SerializerProvider serializers) throws IOException {
final BigDecimal amount = value.numberValueExact(BigDecimal.class);
generator.writeNumber(amount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.zalando.jackson.datatype.money;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.math.BigDecimal;
import javax.money.NumberValue;

public class StringNumberValueSerializer extends JsonSerializer<NumberValue> {

@Override
public void serialize(final NumberValue value, final JsonGenerator generator, final SerializerProvider serializers) throws IOException {
final BigDecimal amount = value.numberValueExact(BigDecimal.class);
generator.writeString(amount.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.jackson.datatype.money;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -19,6 +20,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
import javax.money.NumberValue;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -63,23 +65,24 @@ public void shouldSerialize() throws JsonProcessingException {

assertThat(actual, is(expected));
}

@Test
public void defaultConstructorShouldFallbackToNoFormatting() throws IOException {
final ObjectMapper unit = unit(new SimpleModule()
.addSerializer(CurrencyUnit.class, new CurrencyUnitSerializer())
.addSerializer(NumberValue.class, new NumberValueSerializer())
.addSerializer(MonetaryAmount.class, new MonetaryAmountSerializer()));

final String expected = "{\"amount\":29.95,\"currency\":\"EUR\"}";
final String actual = unit.writeValueAsString(amount);

assertThat(actual, is(expected));
}

@Test
public void shouldSerializeWithoutFormattedValueIfFactoryProducesNull() throws JsonProcessingException {
final ObjectMapper unit = unit(module().withFormatFactory(new NoopMonetaryAmountFormatFactory()));

final String expected = "{\"amount\":29.95,\"currency\":\"EUR\"}";
final String actual = unit.writeValueAsString(amount);

Expand Down Expand Up @@ -127,4 +130,25 @@ public void shouldSerializeWithCustomName() throws IOException {
assertThat(actual, is(expected));
}

}
@Test
public void shouldSerializeAmountAsStringValue() throws JsonProcessingException {
final ObjectMapper unit = unit(module().withNumberValueSerializer(new StringNumberValueSerializer()));

final String expected = "{\"amount\":\"29.95\",\"currency\":\"EUR\"}";
final String actual = unit.writeValueAsString(amount);

assertThat(actual, is(expected));
}

@Test
public void shouldRespectJacksonFeatureByDefault() throws JsonProcessingException {
final ObjectMapper unit = unit(module());
unit.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);

final String expected = "{\"amount\":\"29.95\",\"currency\":\"EUR\"}";
final String actual = unit.writeValueAsString(amount);

assertThat(actual, is(expected));
}

}

0 comments on commit 76d2af4

Please sign in to comment.