Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "amount": 15.1999999999999999,
- "currency": "USD"
- }
- ]
- public static double parseDouble(String s) throws NumberFormatException {
- // [JACKSON-486]: avoid some nasty float representations... but should it be MIN_NORMAL or MIN_VALUE?
- /* as per [JACKSON-827], let's use MIN_VALUE as it is available on all JDKs; normalized
- * only in JDK 1.6. In practice, should not really matter.
- */
- if (NASTY_SMALL_DOUBLE.equals(s)) {
- return Double.MIN_VALUE;
- }
- return Double.parseDouble(s);
- }
- public NumberType getNumberType() throws IOException
- {
- if (_numTypesValid == NR_UNKNOWN) {
- _parseNumericValue(NR_UNKNOWN); // will also check event type
- }
- if (_currToken == JsonToken.VALUE_NUMBER_INT) {
- if ((_numTypesValid & NR_INT) != 0) {
- return NumberType.INT;
- }
- if ((_numTypesValid & NR_LONG) != 0) {
- return NumberType.LONG;
- }
- return NumberType.BIG_INTEGER;
- }
- /* And then floating point types. Here optimal type
- * needs to be big decimal, to avoid losing any data?
- * However... using BD is slow, so let's allow returning
- * double as type if no explicit call has been made to access
- * data as BD?
- */
- if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
- return NumberType.BIG_DECIMAL;
- }
- return NumberType.DOUBLE;
- }
- @JsonDeserialize(using=MyBigDecimalDeserializer.class)
- private BigDecimal amount;
- public class MyBigDecimalDeserializer extends JsonDeserializer<BigDecimal> {
- private NumberDeserializers.BigDecimalDeserializer delegate = NumberDeserializers.BigDecimalDeserializer.instance;
- @Override
- public BigDecimal deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- BigDecimal bd = delegate.deserialize(jp, ctxt);
- // bd = bd.setScale(2, RoundingMode.HALF_UP);
- return bd;
- }
- }
- SimpleModule testModule = new SimpleModule("MyBigDecimalDeserializer", new Version(1, 0, 0, "beta"))
- .addDeserializer(BigDecimal.class, new MyBigDecimalDeserializer());
- mapper.registerModule(testModule);
Add Comment
Please, Sign In to add comment