blob: d8e61602a0eae8b38e9cc79e9b9799ae4accc35e [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.json;
2
3import java.lang.reflect.*;
4import java.math.*;
5import java.util.*;
6
7public class NumberHandler extends Handler {
Stuart McCulloch4482c702012-06-15 13:27:53 +00008 final Class< ? > type;
Stuart McCullochf3173222012-06-07 21:57:32 +00009
Stuart McCulloch4482c702012-06-15 13:27:53 +000010 NumberHandler(Class< ? > clazz) {
Stuart McCullochf3173222012-06-07 21:57:32 +000011 this.type = clazz;
12 }
13
Stuart McCulloch4482c702012-06-15 13:27:53 +000014 @Override
15 void encode(Encoder app, Object object, Map<Object,Type> visited) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000016 String s = object.toString();
Stuart McCulloch4482c702012-06-15 13:27:53 +000017 if (s.endsWith(".0"))
18 s = s.substring(0, s.length() - 2);
19
Stuart McCullochf3173222012-06-07 21:57:32 +000020 app.append(s);
21 }
22
Stuart McCulloch4482c702012-06-15 13:27:53 +000023 @Override
Stuart McCulloch55fbda52012-08-02 13:26:25 +000024 Object decode(Decoder dec, boolean s) {
25 return decode(dec,s ? 1d : 0d);
Stuart McCullochf3173222012-06-07 21:57:32 +000026 }
27
Stuart McCulloch4482c702012-06-15 13:27:53 +000028 @Override
Stuart McCulloch55fbda52012-08-02 13:26:25 +000029 Object decode(Decoder dec, String s) {
Stuart McCullochf3173222012-06-07 21:57:32 +000030 double d = Double.parseDouble(s);
Stuart McCulloch55fbda52012-08-02 13:26:25 +000031 return decode(dec, d);
Stuart McCullochf3173222012-06-07 21:57:32 +000032 }
33
Stuart McCulloch4482c702012-06-15 13:27:53 +000034 @Override
Stuart McCulloch55fbda52012-08-02 13:26:25 +000035 Object decode(Decoder dec) {
36 return decode(dec,0d);
Stuart McCullochf3173222012-06-07 21:57:32 +000037 }
38
Stuart McCulloch4482c702012-06-15 13:27:53 +000039 @Override
Stuart McCulloch55fbda52012-08-02 13:26:25 +000040 Object decode(Decoder dec, Number s) {
Stuart McCullochf3173222012-06-07 21:57:32 +000041 double dd = s.doubleValue();
Stuart McCulloch4482c702012-06-15 13:27:53 +000042
Stuart McCullochf3173222012-06-07 21:57:32 +000043 if (type == double.class || type == Double.class)
44 return s.doubleValue();
45
Stuart McCulloch4482c702012-06-15 13:27:53 +000046 if ((type == int.class || type == Integer.class) && within(dd, Integer.MIN_VALUE, Integer.MAX_VALUE))
Stuart McCullochf3173222012-06-07 21:57:32 +000047 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 McCulloch4482c702012-06-15 13:27:53 +000055 if ((type == short.class || type == Short.class) && within(dd, Short.MIN_VALUE, Short.MAX_VALUE))
Stuart McCullochf3173222012-06-07 21:57:32 +000056 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}