Guest User

Untitled

a guest
Oct 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. [
  2. {
  3. "amount": 15.1999999999999999,
  4. "currency": "USD"
  5.  
  6. }
  7. ]
  8.  
  9. public static double parseDouble(String s) throws NumberFormatException {
  10. // [JACKSON-486]: avoid some nasty float representations... but should it be MIN_NORMAL or MIN_VALUE?
  11. /* as per [JACKSON-827], let's use MIN_VALUE as it is available on all JDKs; normalized
  12. * only in JDK 1.6. In practice, should not really matter.
  13. */
  14. if (NASTY_SMALL_DOUBLE.equals(s)) {
  15. return Double.MIN_VALUE;
  16. }
  17. return Double.parseDouble(s);
  18. }
  19.  
  20. public NumberType getNumberType() throws IOException
  21. {
  22. if (_numTypesValid == NR_UNKNOWN) {
  23. _parseNumericValue(NR_UNKNOWN); // will also check event type
  24. }
  25. if (_currToken == JsonToken.VALUE_NUMBER_INT) {
  26. if ((_numTypesValid & NR_INT) != 0) {
  27. return NumberType.INT;
  28. }
  29. if ((_numTypesValid & NR_LONG) != 0) {
  30. return NumberType.LONG;
  31. }
  32. return NumberType.BIG_INTEGER;
  33. }
  34.  
  35. /* And then floating point types. Here optimal type
  36. * needs to be big decimal, to avoid losing any data?
  37. * However... using BD is slow, so let's allow returning
  38. * double as type if no explicit call has been made to access
  39. * data as BD?
  40. */
  41. if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
  42. return NumberType.BIG_DECIMAL;
  43. }
  44. return NumberType.DOUBLE;
  45. }
  46.  
  47. @JsonDeserialize(using=MyBigDecimalDeserializer.class)
  48. private BigDecimal amount;
  49.  
  50. public class MyBigDecimalDeserializer extends JsonDeserializer<BigDecimal> {
  51.  
  52. private NumberDeserializers.BigDecimalDeserializer delegate = NumberDeserializers.BigDecimalDeserializer.instance;
  53.  
  54. @Override
  55. public BigDecimal deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  56. BigDecimal bd = delegate.deserialize(jp, ctxt);
  57. // bd = bd.setScale(2, RoundingMode.HALF_UP);
  58. return bd;
  59. }
  60. }
  61.  
  62. SimpleModule testModule = new SimpleModule("MyBigDecimalDeserializer", new Version(1, 0, 0, "beta"))
  63. .addDeserializer(BigDecimal.class, new MyBigDecimalDeserializer());
  64. mapper.registerModule(testModule);
Add Comment
Please, Sign In to add comment