All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.json.MoneySerializer Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

import javax.money.MonetaryAmount;
import java.io.IOException;
import java.math.BigDecimal;

final class MoneySerializer extends StdScalarSerializer {
    static final long serialVersionUID = 0L;

    MoneySerializer() {
        super(MonetaryAmount.class);
    }

    @Override
    public void serialize(final MonetaryAmount monetaryAmount, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException {
        final BigDecimal value = monetaryAmount.getNumber().numberValue(BigDecimal.class);
        final String currencyCode = monetaryAmount.getCurrency().getCurrencyCode();
        final MoneyRepresentation moneyRepresentation = new MoneyRepresentation(value, currencyCode);
        jsonGenerator.writeObject(moneyRepresentation);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy