com.unzer.payment.communication.JsonBigDecimalConverter Maven / Gradle / Ivy
package com.unzer.payment.communication;
/*-
* #%L
* Unzer Java SDK
* %%
* Copyright (C) 2020 Unzer E-Com GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.google.gson.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class JsonBigDecimalConverter implements JsonDeserializer, JsonSerializer {
@Override
public BigDecimal deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
String jsonValue = json.getAsJsonPrimitive().getAsString();
if (jsonValue == null || "".equalsIgnoreCase(jsonValue)) {
return null;
}
BigDecimal number = new BigDecimal(jsonValue);
return number.setScale(4, BigDecimal.ROUND_HALF_UP);
}
@Override
public JsonElement serialize(BigDecimal src, Type typeOfSrc, JsonSerializationContext context) {
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(4);
df.setMinimumFractionDigits(4);
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
decimalFormatSymbols.setDecimalSeparator('.');
df.setDecimalFormatSymbols(decimalFormatSymbols);
df.setGroupingUsed(false);
return new JsonPrimitive(df.format(src.setScale(4, BigDecimal.ROUND_HALF_UP)));
}
}