Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.json; |
| 2 | |
| 3 | import java.lang.reflect.*; |
| 4 | import java.math.*; |
| 5 | import java.util.*; |
| 6 | |
| 7 | public class NumberHandler extends Handler { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | final Class< ? > type; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | NumberHandler(Class< ? > clazz) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | this.type = clazz; |
| 12 | } |
| 13 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | @Override |
| 15 | void encode(Encoder app, Object object, Map<Object,Type> visited) throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | String s = object.toString(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | if (s.endsWith(".0")) |
| 18 | s = s.substring(0, s.length() - 2); |
| 19 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 20 | app.append(s); |
| 21 | } |
| 22 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | @Override |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 24 | Object decode(Decoder dec, boolean s) { |
| 25 | return decode(dec,s ? 1d : 0d); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | @Override |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 29 | Object decode(Decoder dec, String s) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | double d = Double.parseDouble(s); |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 31 | return decode(dec, d); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | @Override |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 35 | Object decode(Decoder dec) { |
| 36 | return decode(dec,0d); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | @Override |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 40 | Object decode(Decoder dec, Number s) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | double dd = s.doubleValue(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | if (type == double.class || type == Double.class) |
| 44 | return s.doubleValue(); |
| 45 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 46 | if ((type == int.class || type == Integer.class) && within(dd, Integer.MIN_VALUE, Integer.MAX_VALUE)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | return s.intValue(); |
| 48 | |
| 49 | if ((type == long.class || type == Long.class) && within(dd, Long.MIN_VALUE, Long.MAX_VALUE)) |
| 50 | return s.longValue(); |
| 51 | |
| 52 | if ((type == byte.class || type == Byte.class) && within(dd, Byte.MIN_VALUE, Byte.MAX_VALUE)) |
| 53 | return s.byteValue(); |
| 54 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 55 | if ((type == short.class || type == Short.class) && within(dd, Short.MIN_VALUE, Short.MAX_VALUE)) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | return s.shortValue(); |
| 57 | |
| 58 | if (type == float.class || type == Float.class) |
| 59 | return s.floatValue(); |
| 60 | |
| 61 | if (type == BigDecimal.class) |
| 62 | return BigDecimal.valueOf(dd); |
| 63 | |
| 64 | if (type == BigInteger.class) |
| 65 | return BigInteger.valueOf(s.longValue()); |
| 66 | |
| 67 | throw new IllegalArgumentException("Unknown number format: " + type); |
| 68 | } |
| 69 | |
| 70 | private boolean within(double s, double minValue, double maxValue) { |
| 71 | return s >= minValue && s <= maxValue; |
| 72 | } |
| 73 | } |