01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19:
20: package de.schlund.pfixcore.webservice.jsonws.deserializers;
21:
22: import java.lang.reflect.Type;
23:
24: import de.schlund.pfixcore.webservice.jsonws.DeserializationContext;
25: import de.schlund.pfixcore.webservice.jsonws.DeserializationException;
26: import de.schlund.pfixcore.webservice.jsonws.Deserializer;
27:
28: /**
29: * @author mleidig@schlund.de
30: */
31: public class NumberDeserializer extends Deserializer {
32:
33: @Override
34: public boolean canDeserialize(DeserializationContext ctx,
35: Object jsonValue, Type targetType) {
36: Class<?> targetClass = (Class<?>) targetType;
37: if (jsonValue instanceof Number
38: && Number.class.isAssignableFrom(targetClass))
39: return true;
40: return false;
41: }
42:
43: @Override
44: public Object deserialize(DeserializationContext ctx,
45: Object jsonValue, Type targetType)
46: throws DeserializationException {
47: Class<?> targetClass = (Class<?>) targetType;
48: if (jsonValue instanceof Number) {
49: if (targetClass == int.class
50: || targetClass == Integer.class) {
51: if (jsonValue.getClass() == Integer.class)
52: return jsonValue;
53: else
54: return new Integer(((Number) jsonValue).intValue());
55: } else if (targetClass == float.class
56: || targetClass == Float.class) {
57: if (jsonValue.getClass() == Float.class)
58: return jsonValue;
59: else
60: return new Float(((Number) jsonValue).floatValue());
61: } else if (targetClass == long.class
62: || targetClass == Long.class) {
63: if (jsonValue.getClass() == Long.class)
64: return jsonValue;
65: else
66: return new Long(((Number) jsonValue).longValue());
67: } else if (targetClass == double.class
68: || targetClass == Double.class) {
69: if (jsonValue.getClass() == Double.class)
70: return jsonValue;
71: else
72: return new Double(((Number) jsonValue)
73: .doubleValue());
74: } else if (targetClass == byte.class
75: || targetClass == Byte.class) {
76: if (jsonValue.getClass() == Byte.class)
77: return jsonValue;
78: else
79: return new Byte(((Number) jsonValue).byteValue());
80: } else if (targetClass == short.class
81: || targetClass == Short.class) {
82: if (jsonValue.getClass() == Short.class)
83: return jsonValue;
84: else
85: return new Short(((Number) jsonValue).shortValue());
86: }
87: return (Number) jsonValue;
88: } else
89: throw new DeserializationException("Wrong type: "
90: + jsonValue.getClass().getName());
91: }
92:
93: }
|