blob: 644d49a37a4a4dad9cfedba60b2d498b16f8801d [file] [log] [blame]
package aQute.lib.json;
import java.lang.reflect.*;
import java.math.*;
import java.util.*;
public class NumberHandler extends Handler {
final Class<?> type;
NumberHandler(Class<?> clazz) {
this.type = clazz;
}
@Override void encode(Encoder app, Object object, Map<Object, Type> visited)
throws Exception {
String s = object.toString();
if ( s.endsWith(".0"))
s = s.substring(0,s.length()-2);
app.append(s);
}
@Override Object decode(boolean s) {
return decode(s ? 1d : 0d);
}
@Override Object decode(String s) {
double d = Double.parseDouble(s);
return decode(d);
}
@Override Object decode() {
return decode(0d);
}
@Override Object decode(Number s) {
double dd = s.doubleValue();
if (type == double.class || type == Double.class)
return s.doubleValue();
if ((type == int.class || type == Integer.class)
&& within(dd, Integer.MIN_VALUE, Integer.MAX_VALUE))
return s.intValue();
if ((type == long.class || type == Long.class) && within(dd, Long.MIN_VALUE, Long.MAX_VALUE))
return s.longValue();
if ((type == byte.class || type == Byte.class) && within(dd, Byte.MIN_VALUE, Byte.MAX_VALUE))
return s.byteValue();
if ((type == short.class || type == Short.class)
&& within(dd, Short.MIN_VALUE, Short.MAX_VALUE))
return s.shortValue();
if (type == float.class || type == Float.class)
return s.floatValue();
if (type == BigDecimal.class)
return BigDecimal.valueOf(dd);
if (type == BigInteger.class)
return BigInteger.valueOf(s.longValue());
throw new IllegalArgumentException("Unknown number format: " + type);
}
private boolean within(double s, double minValue, double maxValue) {
return s >= minValue && s <= maxValue;
}
}